反应与开玩笑:无法从测试文件中找到模块 [英] React & Jest: Cannot find module from test file
问题描述
在目录app/__tests__
中为Redux操作('App.js')设置Jest测试('App-test.js'):
Setting up a Jest test ('App-test.js') for a Redux action ('App.js') in a directory app/__tests__
:
这是App.js的标头:
Here's the header of App.js:
jest.unmock('../../modules/actions/App.js')
import React from 'react'
import ReactDOM from 'react-dom'
import TestUtils from 'react-addons-test-utils'
import * as App from '../../modules/actions/App.js'
在app/
中有一个模块config.js
.这是在需要的地方导入的.
In app/
there is a module config.js
. This is being imported where it is needed.
问题是,当我运行Jest测试(例如App-test.js)时,它正在寻找配置,但找不到它:
The problem is, when I run my Jest tests, such as App-test.js, it is looking for config and not finding it:
FAIL __tests__/actions/App-test.js
Runtime Error
Error: Cannot find module 'config' from 'User.js'
然后User.js
像这样导入config
:
import config from 'config'
User.js
是另一个正在使用的动作App.js
.
有什么想法吗?
推荐答案
您应指定模块位置,否则Node.js将尝试为您猜测位置,例如:
You should specify the module location, otherwise Node.js will try to guess the location for you, like:
node_modules/config.js
node_modules/config/index.js
node_modules/config/package.json
代码中的问题是假设节点将在所需位置查找文件,正如您在前几行提供的算法中所见.
The problem in your code is the assumption that node will look for the file in the desired location, as you can see in the algorithm I provided in the previous lines.
要解决此问题,您必须在User.js文件中指定位置,例如,检查假设的文件组织:
To fix the issue you have to specify the location inside your User.js file, for example, check the hypothetical file organization:
/
/config.js
/app
/app/User.js
然后,您将导入User.js:
Then, you'd import inside User.js:
import config from '../config.js'
文件config.js
相对于位于父目录中的User.js.
The file config.js
is relative to User.js, located in the parent directory.
这篇关于反应与开玩笑:无法从测试文件中找到模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!