jest: Jest not running tests in src/node_modules
Bug Report
What is the current behavior? Jest reports “No tests found” even when there are tests
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal repository on GitHub that we can npm install
and npm test
.
Example repo.
What is the expected behavior?
Tests in src/node_modules
are run
Run Jest again with --debug
and provide the full configuration it prints. Please mention your node and npm version and operating system.
jest version = 16.0.2
test framework = jasmine2
config = {
"moduleFileExtensions": [
"jsx",
"js",
"json"
],
"moduleNameMapper": [
[
"^.+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$",
"/Users/justinfalcone/work/modernserf/test-jest/config/jest/FileStub.js"
],
[
"^.+\\.css$",
"/Users/justinfalcone/work/modernserf/test-jest/config/jest/CSSStub.js"
]
],
"setupFiles": [
"/Users/justinfalcone/work/modernserf/test-jest/config/polyfills.js"
],
"testPathIgnorePatterns": [
"/Users/justinfalcone/work/modernserf/test-jest/(build|config|node_modules)/"
],
"preprocessorIgnorePatterns": [
"/Users/justinfalcone/work/modernserf/test-jest/node_modules"
],
"testEnvironment": "/Users/justinfalcone/work/modernserf/test-jest/node_modules/jest-environment-jsdom/build/index.js",
"rootDir": "/Users/justinfalcone/work/modernserf/test-jest",
"name": "-Users-justinfalcone-work-modernserf-test-jest",
"testRunner": "/Users/justinfalcone/work/modernserf/test-jest/node_modules/jest-jasmine2/build/index.js",
"scriptPreprocessor": "/Users/justinfalcone/work/modernserf/test-jest/node_modules/babel-jest/build/index.js",
"usesBabelJest": true,
"automock": false,
"bail": false,
"browser": false,
"cacheDirectory": "/var/folders/q8/zlh7b1ms7fg2r3cqx6sgbrrr0000gn/T/jest",
"clearMocks": false,
"coveragePathIgnorePatterns": [
"/node_modules/"
],
"coverageReporters": [
"json",
"text",
"lcov",
"clover"
],
"globals": {},
"haste": {
"providesModuleNodeModules": []
},
"mocksPattern": "__mocks__",
"moduleDirectories": [
"node_modules"
],
"modulePathIgnorePatterns": [],
"noStackTrace": false,
"notify": false,
"preset": null,
"resetModules": false,
"testPathDirs": [
"/Users/justinfalcone/work/modernserf/test-jest"
],
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.jsx?$",
"testURL": "about:blank",
"timers": "real",
"useStderr": false,
"verbose": null,
"watch": false,
"cache": true,
"watchman": true,
"testcheckOptions": {
"times": 100,
"maxSize": 200
}
}
No tests found
12 files checked.
testPathDirs: /Users/justinfalcone/work/modernserf/test-jest - 12 matches
testRegex: (/__tests__/.*|\.(test|spec))\.jsx?$ - 0 matches
testPathIgnorePatterns: /Users/justinfalcone/work/modernserf/test-jest/(build|config|node_modules)/ - 5 matches
Node v6.5.0 npm v3.10.3
See https://github.com/facebookincubator/create-react-app/issues/1042 and https://github.com/facebookincubator/create-react-app/issues/607 for related issues.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 13
- Comments: 23 (7 by maintainers)
Commits related to this issue
- Allow Jest to run tests in node_modules Resolves #2145 and #11781. Prevent haste map from automatically discarding node_modules files. By default, node_modules is still excluded via the testPathIgno... — committed to pauldraper/jest by pauldraper 2 years ago
- Allow Jest to run tests in node_modules Resolves #2145 and #11781. Prevent haste map from automatically discarding node_modules files. By default, node_modules is still excluded via the testPathIgno... — committed to pauldraper/jest by pauldraper 2 years ago
Hey @modernserf I ran into the same issue and adding the following to my Jest config helped:
It would be really nice to have this supported properly. For me neither the workaround nor any other suggestion made it working. 🙄
What I’m trying to do:
having directory tree
a-package.json
running jest
what I do simply:
results
results using the workaround
set as part of
a-package/package.json
jest configresults using the workaround & testPathIgnorePatterns
ie I have the following jest config within
a-package/package.json
:Still without any luck. This time the test has been found and properly loaded however:
I’m reporting all the above just to add more context/info on the problem and its possible cause. I have a ton of dependencies within
a-package.json
(obviously omitted) and seemsjest-haste-map
gets confused 😃why
I’m sharing common modules across apps within a monorepo and I cant use symlinks to do the job (thus Lerna isnt an option). The native nodejs require/resolve process works just fine for directory trees as described above, thus I’m able to require/import common modules into apps without ‘magic’ tricks.
All fine until the point which I needed some tests for the common modules of mine which of course are placed within the common modules rendering them under
node_modules
path which seems is causing problems.what I think I need
Jest to be able to run as it runs normally - excluding
./node_modules
subdirectory relative to the current working directory and be able to do that when the path of the current working directory contains ‘node_modules’ in it 😃update: please excuse me if the above doesnt makes sense or isnt helpful / related to the current github issue
To work around it now, I made a wrapper file:
And I run that file instead of the main Jest executable. I’d much rather have a config option if the maintainers are amenable to a pull request…
P.S. I also configured my
testPathIgnorePatterns
like this:Not working on 26.0.1, even with mentioned workarounds. It seems that
haste
field is deprecated now and thus Jest refuses to run tests correctly.Is there any plans to fix this? It is very strange that Jest ignores tests when the package is places inside
node_modules
. I think it would be better to check if the test is placed inside the localnode_modules
rather than just checking if the path includes it.This issue prevents some decoupling that proves useful when building integration tests that can be shared across multiple of my
node
services.The approach I took was to write my integration tests in a new project, and
npm install
it into each service that would use it, placing the jest installation along with the relevant tests into each respectivenode_modules
folder. Sadly jest refuses to locate any of the tests sincenode_modules
would always exist in the directory path of its parent directory.Adding all of these items to my
jest.config.js
apears to be working for me:I believe this is because haste ignores files in all node_modules directories, so files in
src/node_modules
don’t even make it to wheretestPathIgnorePatterns
is used@kuka-radovan I stopped using that style of monorepo and now recommend npm 7 or yarn workspaces to do inter-module linking rather than relying on the
node_modules
resolution quirk. As far as Jest is concerned, maybe it shouldn’t make assumptions aboutnode_modules
directories. There could be a default heuristic, and Jest could allow the user to override it, but I don’t think it’s worth the effort given the alternatives. 🤷♀️I have the problem too.
We have a package
core
, which is used by 6 other packages as a framework.core
has its own jest tests, but eachpackageX
(where X is from 1 to 6) also has its own tests. We want toyarn add --dev
each package to ourcore
package, and run allpacakgeX
tests in ourcore
CI. So, we could be sure, we didnt break any backward compatibility for any ofpackageX
.Not sure how “standard” that approach is, but
packageX
tests are located innode_modules
(of course).@cpojer did you look at the example repo? I’m using
and its still ignoring src/node_modules.