mercredi 14 octobre 2020

jest: Cannot find module 'react', and other node_modules

I am trying to write some tests for a library I'm making. This is not a creat-react-app project, but something built from scratch with babel. I'm using jest, and I'm having this problem. I saw similar quesitons, but nothing quite like this. I have "test": "jest" in my package.json scripts. I have a __tests__ directory in my root folder. One of my test files, called Component.js, starts with some simple imports:

import React from 'react';
import { MyComponent } from '../src/MyComponent.js';

When I run npm run test, I get this error:

Cannot find module 'react' from '__tests__/MyComponent.js'

    > 1 | import React from 'react';

Even if I comment out the react import, I get this error:

Cannot find module 'react' from 'src/MyComponent.js'

    Require stack:
      src/MyComponent.js
      __tests__/MyComponent.js

    > 1 | import React from 'react';

This is happening not just with react, but with any node modules imported in the test, or imported in any module that the test imports. I don't understand. Does jest not know to look for these modules in the node_modules folder? I do not have any jest.config.js or any jest property in my package.json - I didn't think I needed one to specify that jest should be looking for these modules in the node_moduldes folder. Do I need to set up some config for jest to look in the node_modules for these type of imports? I feel like there's a simple fix here that's alluding me. Thanks for reading.

Aucun commentaire:

Enregistrer un commentaire