React & Jest: cannot find module from test file

Setting up the Jest test ("App-test.js") for the Redux action ("App.js") in the app/__tests__ :

Here is the App.js header:

 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' 

There is a config.js module in app/ . It is imported where necessary.

The problem is that when I run my Jest tests, such as App-test.js, it searches for the configuration and does not find it:

  FAIL __tests__/actions/App-test.js Runtime Error Error: Cannot find module 'config' from 'User.js' 

And User.js imports config like this: import config from 'config'

User.js uses another App.js action.

Any ideas?

+8
javascript reactjs redux jestjs
source share
1 answer

You must specify the location of the module, otherwise Node.js will try to guess the location for you, for example:

 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 search for the file in the right place, as you can see in the algorithm presented in the previous lines.

To fix the problem, you must specify the location in the User.js file, for example, check the hypothetical organization of the file:

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

Then you import inside User.js:

 import config from '../config.js' 

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

0
source share

All Articles