node -v: v8.11.3npm -v: 5.6.0npm 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]
"jest.runAllTestsFirst": false,
"jest.debugMode": true,
"jest.pathToJest": "jest",
"jest.pathToConfig": "/Users/hnjoshi/repos/my-app/content/private/react/package.json"
MacOS High Sierra, 10.13.6 (17G65)npm run testrootDir/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
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.
Tests should pass or fail for all test suites related to changed files in the Output window.
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...
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.
Most helpful comment
@escaton still doesn't work for me