4
votes

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.

Edit: Mocha giving me a similar issue

I thought I woul try and bypass this whole issue by using mocha instead. With a test script of "mocha": "mocha --require @babel/register '**/__tests__/*' -R spec", I get a similar error:

Error: Cannot find module 'react'
Require stack:
- /Users/seth/Documents/GitHub/react-esri-leaflet/__tests__/MyComponent.js
- /Users/seth/Documents/GitHub/react-esri-leaflet/node_modules/mocha/lib/esm-utils.js
- /Users/seth/Documents/GitHub/react-esri-leaflet/node_modules/mocha/lib/mocha.js
- /Users/seth/Documents/GitHub/react-esri-leaflet/node_modules/mocha/lib/cli/one-and-dones.js
- /Users/seth/Documents/GitHub/react-esri-leaflet/node_modules/mocha/lib/cli/options.js
- /Users/seth/Documents/GitHub/react-esri-leaflet/node_modules/mocha/bin/mocha
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:713:15)

What the heck is going on here? Shouldn't these testing libraries know to look in my rootFolder/node_modules for these things? Might something in my package.json or (now obsolete) webpack.config.js file be interfering? Rather than copy those files into this post, you can see them, with the whole project directory, at the repo here: https://github.com/slutske22/react-esri-leaflet

1

1 Answers

5
votes

The more I thought about this the more I realized that this is a peerDependencies issue. The dependencies that jest couldn't find were all peerDependencies. If I were to run my package through travisci, it would run npm install and npm test, and it would never install these dependencies or be able to use them in the tests.

Thanks to this question, I copied all my peerDependencies into the devDependencies, reran npm install, and then the tests started working.

Rather than delete the question, I'll leave this here if anyone ever has this specific case happen to them.