反应& Jest:无法从测试文件中找到模块

时间:2016-05-18 18:46:11

标签: javascript reactjs redux jestjs

在目录app/__tests__中为Redux操作('App.js')设置Jest测试('App-test.js'):

以下是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。这是在需要的地方导入的。

问题是,当我运行我的Jest测试时,例如App-test.js,它正在寻找配置而没有找到它:

 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

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您应该指定模块位置,否则Node.js会尝试为您猜测位置,例如:

node_modules/config.js
node_modules/config/index.js
node_modules/config/package.json

您的代码中的问题是假设节点将在所需位置查找文件,如您在前面几行中提供的算法中所见。

要解决此问题,您必须在User.js文件中指定位置,例如,检查假设的文件组织:

/
/config.js
/app
/app/User.js

然后,您将在User.js中导入:

import config from '../config.js'

文件config.js与User.js相关,位于父目录中。