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

Created on 20 Sep 2018  ·  10Comments  ·  Source: jest-community/vscode-jest

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
   └── [email protected]
  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...

Most helpful comment

@escaton still doesn't work for me

All 10 comments

Try to add this to jest config

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

@escaton Yes, it worked, thank you!!

@escaton still doesn't work for me

@escaton This worked for me! Thanks for saving me hours of more searching.

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.

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.

@RyanPWalker Thank you and @hanchiang as well for your solution to the problem, helped me out in my own project!

where jest config on my package or on the node_modules jest config? thanks

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>" ], ...

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

connectdotz picture connectdotz  ·  24Comments

Cael6 picture Cael6  ·  19Comments

Drakota picture Drakota  ·  16Comments

connectdotz picture connectdotz  ·  37Comments

bnwan picture bnwan  ·  28Comments