vscode-jest: Test suite failed to run - Cannot find module

Environment

  1. node -v: v8.11.3
  2. npm -v: 5.6.0
  3. npm ls jest or npm ls react-scripts (if you haven’t ejected):
   $ npm ls jest
   /Users/hnjoshi/repos/my-app/content/private/react
   └── jest@23.3.0
  1. your vscode-jest settings if customized:
  "jest.runAllTestsFirst": false,
  "jest.debugMode": true,
  "jest.pathToJest": "jest",
  "jest.pathToConfig": "/Users/hnjoshi/repos/my-app/content/private/react/package.json"
  1. Operating system: MacOS High Sierra, 10.13.6 (17G65)

Prerequisite

  • are you able to run jest test from command line? [yes]
  • how do yo run your tests from command line? npm run test

Steps to Reproduce

rootDir/content/private/react/package.json - npm run test works fine from command line

  "scripts": {
    "test": "jest --no-cache",
    "test:coverage": "jest --coverage --no-cache",
    "test-watch": "jest --watch",
    "watch": "webpack --progress --watch"
  },
  "jest": {
    "cacheDirectory": "cache",
    "collectCoverageFrom": [
      "**/*.js",
      "!**/eslint-plugin-control/**",
      "!**/node_modules/**",
      "!**/coverage/**",
      "!webpack.config.js"
    ],
    "moduleNameMapper": {
      "^.+\\.(scss)$": "<rootDir>/__mocks__/styleMock.js"
    },
    "testRegex": "/__tests__/[^.].*js$",
    "setupFiles": [
      "<rootDir>/setup-jest.js",
      "<rootDir>/__mocks__/localStorageMock.js"
    ],
    "setupTestFrameworkScriptFile": "<rootDir>/setup-adapter.js",
    "snapshotSerializers": [
      "<rootDir>/node_modules/enzyme-to-json/serializer"
    ],
    "testEnvironment": "jest-environment-jsdom-global"
  }

rootDir/content/private/react/components/ - Test files are in this directory

Relevant Debug Info

I think there is some issue with the --include-path but I could not tell what script is being executed when I save the test file.

Expected Behavior when test file is saved

Tests should pass or fail for all test suites related to changed files in the Output window.

Actual Behavior

FAIL content/private/react/components/User/Details/__tests__/Details-test.js

  ● Test suite failed to run

    Cannot find module 'components/Name' from 'index.js'

      1 | import PropTypes from 'prop-types';
      2 | import React from 'react';
    > 3 | import Name from 'components/Name';
        | ^
      4 | import Input from 'components/Input';
      5 | import {
      6 |   Form,

      at Resolver.resolveModule (../../../../../../../usr/local/lib/node_modules/jest/node_modules/jest-resolve/build/index.js:221:17)
      at Object.<anonymous> (components/User/Details/index.js:3:1)


Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.542s
Ran all test suites related to changed files.

The fastest (and the most fun) way to resolve the issue is to submit a pull-request yourself. If you are interested, feel free to check out the contribution guide, we look forward to seeing your PR…

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 7
  • Comments: 25 (1 by maintainers)

Most upvoted comments

Try to add this to jest config

"modulePaths": [
  "<rootDir>"
],

@escaton still doesn’t work for me

all of this solution doesn’t work for me!

Good lord thanks @RyanPWalker for the insight! It turned out that this was my issue as well. I renamed a file, but somehow git didn’t register it. I just git rm --cache <file> and commit again.

I was having errors like the following:

Cannot find module ‘src/api/2 - services/services.module’ from ‘api/1 - controllers/wizard-controller/wizard.controller.spec.ts’

And inside my package.json, in Jest config, I had the following property: ... "rootDir": "src"

So I think that my Jest was trying to search the modules starting with ‘src’ already inside ‘src’ folder (due to rootDir property)

I’ve just removed that property and it started working!

Also, the following property inside Jest config was also necessary:

... "modulePaths": [ "<rootDir>" ], ...

How I solved the problem:

Since using absolute paths is a must for ESModules and I need to use .js extension at the end of the path:

export * from './helpers.js';

It broke the unit tests:

 - Test suite failed to run

    Cannot find module './helpers.js' from 'src/index.ts'

I updated Jest config with the following options and it fixed that:

moduleNameMapper: {
    '^(\\.{1,2}/.*)\\.js$': '$1'
  },

This regular expression ^(\\.{1,2}/.*)\\.js$ I found here in the docs. Don’t be afraid, it just matches any string that starts with one or two dots followed by a forward slash, followed by any number of characters, and ending with the “.js” file extension. All the following paths will be a match:

  • ./app.js
  • ../src/utils/helpers.js
  • ../../lib/jquery.min.js
  • ./../scripts/data.js
  • etc

I hope it would help somebody!

For me, it was a Git issue. I had renamed the file locally but I guess Git didn’t register the change in filename. I ran git mv name.js Name.js, repushed, and then Jest was able to find it where I was building it from.

delete node_modules work for me

Guys, ran into this problem I’m using Nest.
My solution is to write these options:

modulePaths": ["<rootDir>"],
"rootDir": ".",

in my package.json, in jest option. Result - command npm run test works and npm run start:dev also does

I ran into a similar problem where the name of a directory had a capital within it. On GitHub Actions, it raised an error when the case didn’t match, but on my local machine (macOS), it ran just fine.

with inspiring from @escaton answer, add the modulePaths to your jest config

"modulePaths": [ "<rootDir>/src" ],

I was having errors like the following:

Cannot find module ‘src/api/2 - services/services.module’ from ‘api/1 - controllers/wizard-controller/wizard.controller.spec.ts’

And inside my package.json, in Jest config, I had the following property: ... "rootDir": "src"

So I think that my Jest was trying to search the modules starting with ‘src’ already inside ‘src’ folder (due to rootDir property)

I’ve just removed that property and it started working!

Also, the following property inside Jest config was also necessary:

... "modulePaths": [ "<rootDir>" ], ...

This worked for me as well… Thanks

Sometimes if you’re working with Typescript and you set the value of "module" in the tsconfig.json file to a different value than “commonjs”, it may cause that issue.