Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React & Jest: Cannot find module from test file

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?

like image 803
Erik Johnson Avatar asked May 18 '16 18:05

Erik Johnson


People also ask

What is React used for?

React is a JavaScript library developed by Facebook which, among other things, was used to build Instagram.com. Its aim is to allow developers to easily create fast user interfaces for websites and applications alike. The main concept of React. js is virtual DOM.

Is React for HTML or js?

To get an overview of what React is, you can write React code directly in HTML. But in order to use React in production, you need npm and Node. js installed.

What is in React means?

1 : to exert a reciprocal or counteracting force or influence —often used with on or upon. 2 : to change in response to a stimulus. 3 : to act in opposition to a force or influence —usually used with against. 4 : to move or tend in a reverse direction. 5 : to undergo chemical reaction.

Is React better or Angular?

React is better than Angular due to it's virtual DOM implementation and rendering optimizations. Migrating between React's versions is quite easy, too; you don't need to install updates one by one, as in the case of Angular. Finally, with React, developers have myriads of existing solutions they can use.

What is react and how do I use it?

React is a JavaScript library for building user interfaces. React is used to build single-page applications. React allows us to create reusable UI components. Our "Show React" tool makes it easy to demonstrate React. It shows both the code and the result. To learn and test React, you should set up a React Environment on your computer.

Why choose React Native for mobile app development?

With React Native, one team can maintain two platforms and share a common technology—React. your app’s code. React Native Debug Menu. React Native lets you create truly native apps and doesn't compromise your users' experiences.

What is react debug menu in React Native?

React Native Debug Menu. React Native lets you create truly native apps and doesn't compromise your users' experiences. It provides a core set of platform agnostic native components like View, Text, and Image that map directly to the platform’s native UI building blocks.

What is web platform ReactJS?

Web platform. Type. JavaScript library. License. MIT License. Website. reactjs .org. React (also known as React.js or ReactJS) is an open-source, front end, JavaScript library for building user interfaces or UI components. It is maintained by Facebook and a community of individual developers and companies.


1 Answers

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.

like image 92
punkbit Avatar answered Oct 05 '22 18:10

punkbit