javascript - React & Jest: Cannot find module from test file - Stack Overflow

Setting up a Jest test ('App-test.js') for a Redux action ('App.js') in a directory

Setting up a Jest test ('App-test.js') for a Redux action ('App.js') in a directory app/__tests__:

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'

In app/ there is a module config.js. This is being imported where it is needed.

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'

And User.js is importing config like so: import config from 'config'

User.js is another action being used App.js.

Any ideas?

Setting up a Jest test ('App-test.js') for a Redux action ('App.js') in a directory app/__tests__:

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'

In app/ there is a module config.js. This is being imported where it is needed.

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'

And User.js is importing config like so: import config from 'config'

User.js is another action being used App.js.

Any ideas?

Share Improve this question asked May 18, 2016 at 18:46 Erik JohnsonErik Johnson 4111 gold badge5 silver badges11 bronze badges 2
  • 3 Post some more code, kinda confusing without more code to see what needs fixing. Could be issue with how you're using export. – Giant Elk Commented May 29, 2016 at 17:02
  • 4 Maybe it's typo but I'm not sure... Try adding ./ to the config path, so it will be import config from './config'; Maybe jest is trying to reach package named config in node_modules? I don't know what version of jest are you using but automocking is disabled since v15.0.0 - github./facebook/jest/blob/master/CHANGELOG.md#jest-1500 – Dawid Karabin Commented Jan 9, 2017 at 23:21
Add a ment  | 

1 Answer 1

Reset to default 1

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.

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

Then, you'd import inside User.js:

import config from '../config.js'

The file config.js is relative to User.js, located in the parent directory.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745065355a4609222.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信