Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest not running tests in src/node_modules #2145

Closed
modernserf opened this issue Nov 21, 2016 · 23 comments · Fixed by #11084
Closed

Jest not running tests in src/node_modules #2145

modernserf opened this issue Nov 21, 2016 · 23 comments · Fixed by #11084

Comments

@modernserf
Copy link

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 facebook/create-react-app#1042 and facebook/create-react-app#607 for related issues.

@modernserf
Copy link
Author

@shanewilson
Copy link

shanewilson commented Nov 23, 2016

Hey @modernserf
I ran into the same issue and adding the following to my Jest config helped:

  "haste": {
    "providesModuleNodeModules": [".*"]
  }

@cpojer
Copy link
Member

cpojer commented Nov 29, 2016

Yes, Jest excludes node_modules by default. You can adjust testPathIgnorePatterns for your use case: http://facebook.github.io/jest/docs/configuration.html#testpathignorepatterns-array-string

@cpojer cpojer closed this as completed Nov 29, 2016
@modernserf
Copy link
Author

@cpojer did you look at the example repo? I'm using

  "testPathIgnorePatterns": [
    "<rootDir>/(build|config|node_modules)/"
  ]

and its still ignoring src/node_modules.

@modernserf
Copy link
Author

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 where testPathIgnorePatterns is used

@cpojer
Copy link
Member

cpojer commented Nov 29, 2016

Ahh, I'm sorry, yeah you are right! I didn't realize this before, but yes, what @shanewilson says should work for now. I believe to fix this without changing the "haste" config, you'd have to turn this switch to true: https://github.com/facebook/jest/blob/master/packages/jest-haste-map/src/index.js#L207 – if @shanewilson's workaround doesn't work, can you try turning this option to true and reporting back? If that works, we can probably expose it as well as part of the "haste" object – you could send a PR for that :)

I also encourage you to give lerna a try: https://github.com/lerna/lerna – it has worked really well for us!

Are you using Jest at Buzzfeed?

@modernserf
Copy link
Author

Thanks! -- the haste config works for now, as does manually setting retainAllFiles, though when I was testing this earlier it seemed like that was generating a lot of warnings? I'll try this out again and report back.

Regarding BuzzFeed: Each team has a fair amount of control over their own stack, but many of the newer internal tools (including the ones I work on) are using React/Jest/Webpack/Babel; some of our prototypes are even built with create-react-app.

@cpojer
Copy link
Member

cpojer commented Nov 29, 2016

Ok I see, yeah that makes sense. I encourage you to just go with @shanewilson's workaround then.

Also, cool that you are using Jest! :)

@cpojer
Copy link
Member

cpojer commented Dec 2, 2016

Discussion continued here: facebook/create-react-app#1081 (comment)

@ericclemmons
Copy link
Contributor

Related, you need to make sure that files in ./src/node_modules are processed:

I had the same issue, as I'm trying to use ./src/node_modules for internal libs.

Fix:

// .jestrc
 "transformIgnorePatterns": [
    "<rootDir>/(node_modules)/"
  ]

The default regex should have <rootDir> in it:

https://github.com/facebook/jest/blob/e6495647112781b98ff194fe34952a8139f045df/packages/jest-validate/src/__tests__/fixtures/jestConfig.js#L16

@kirkstrobeck
Copy link

Here is an example repo that demonstrates https://github.com/kirkstrobeck/jest-issue-5039

@SimenB SimenB reopened this Dec 11, 2017
@thymikee
Copy link
Collaborator

Original CRA issue was closed, because using src/node_modules is not a standard that community is embracing. Shall we close this as well or do something about it?

@Diokuz
Copy link
Contributor

Diokuz commented Aug 1, 2018

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 each packageX (where X is from 1 to 6) also has its own tests. We want to yarn add --dev each package to our core package, and run all pacakgeX tests in our core CI. So, we could be sure, we didnt break any backward compatibility for any of packageX.

Not sure how "standard" that approach is, but packageX tests are located in node_modules (of course).

@robolivable
Copy link

robolivable commented Aug 17, 2018

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 respective node_modules folder. Sadly jest refuses to locate any of the tests since node_modules would always exist in the directory path of its parent directory.

@outbounder
Copy link

outbounder commented Apr 10, 2019

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

+ monorepo root
| + apps
  | - app1
  | - app2
  | + node_modules
    | + a-package
       | - package.json
       | - index.js
       | - a-test-for-index.test.js
     

a-package.json

{
  ...
  scripts: {
    test: "jest"
  },
  "dependencies": {
    ....
  },
  "devDependencies": {
    "jest": "^24.7.1",
     ....
   }
}

running jest

what I do simply:

$ cd monorepo-root/apps/node_modules/a-package
$ npm run test

results

No tests found, exiting with code 1

results using the workaround

set as part of a-package/package.json jest config

...
  "devDependencies": {
    "jest": "^24.7.1",
     ...
  },
  "jest": {
    "haste": {
      "providesModuleNodeModules": [".*"]
    }
  }
...

... a lot of jest-haste-map warnings ...

jest-haste-map: Haste module naming collision: source-map
The following files share their name; please adjust your hasteImpl:

  • /node_modules/snapdragon/node_modules/source-map/package.json
  • /node_modules/source-map/package.json

No tests found, exiting with code 1

results using the workaround & testPathIgnorePatterns

ie I have the following jest config within a-package/package.json:

...
  "devDependencies": {
    "jest": "^24.7.1",
     ....
  },
  "jest": {
    "testPathIgnorePatterns": ["<rootDir>/(node_modules)/"],
    "haste": {
      "providesModuleNodeModules": [".*"]
    }
  }
...

Still without any luck. This time the test has been found and properly loaded however:

FAIL a-test-for-index.test.js
● Test suite failed to run

The name source-map was looked up in the Haste module map. It cannot be resolved, because there exists several different files, or packages, that provide a module for that particular name and platform. The platform is generic (no extension). You must delete or blacklist files until there remains only one of these:

  * `...{full path removed privacy concerns}.../node_modules/@babel/core/node_modules/source-map/package.json` (package)
  * `...{full path removed privacy concerns}.../node_modules/@babel/generator/node_modules/source-map/package.json` (package)
  * `...{full path removed privacy concerns}.../node_modules/snapdragon/node_modules/source-map/package.json` (package)
 * `...{full path removed privacy concerns}.../node_modules/source-map/package.json` (package)

 at ModuleMap._assertNoDuplicates (node_modules/jest-haste-map/build/ModuleMap.js:280:11)

Test Suites: 1 failed, 1 total


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 seems jest-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

@gabefinch
Copy link

Adding all of these items to my jest.config.js apears to be working for me:

module.exports = {
  haste: {
    providesModuleNodeModules: ['.*']
  },
  testPathIgnorePatterns: ['<rootDir>/node_modules/'],
  transformIgnorePatterns: ['<rootDir>/node_modules/'],
  modulePathIgnorePatterns: ['<rootDir>/node_modules/']
};

@volodymyr-ryvak-globallogic

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 local node_modules rather than just checking if the path includes it.

@xandris
Copy link

xandris commented May 28, 2020

To work around it now, I made a wrapper file:

const Runtime = require('jest-runtime');

const origCreateHasteMap = Runtime.createHasteMap;

Runtime.createHasteMap = function(...args) {
  const ret = origCreateHasteMap.call(this, ...args);
  ret._options.retainAllFiles = true;
  return ret;
};

module.exports = require('jest/bin/jest');

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:

  "jest": {
    "testPathIgnorePatterns": [
      "<rootDir>/node_modules"
    ]
  }

@kuka-radovan
Copy link

xandris solution works fine, but I would like to know, if this is a bug and this suppose to work in near future or these tests are excluded by design.

@xandris
Copy link

xandris commented Nov 5, 2020

xandris solution works fine, but I would like to know, if this is a bug and this suppose to work in near future or these tests are excluded by design.

@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 about node_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. 🤷‍♀️

@pauldraper
Copy link

pauldraper commented Feb 20, 2022

Oof....5 years later and testPathIgnorePatterns still doesn't work.

My workaround is use is creating a custom HasteMap implementation.

const  { default: HasteMap } = require("jest-haste-map");

class CustomHasteMap extends HasteMap {
  _ignore(filePath) {
    const ignorePattern = this._options.ignorePattern;
    const ignoreMatched =
      ignorePattern instanceof RegExp
        ? ignorePattern.test(filePath)
        : ignorePattern && ignorePattern(filePath);
    return ignoreMatched;
  }
}

module.exports = CustomHasteMap;

Then in jest configuration I set haste.hasteMapModulePath to point to that file. (As well as set testPathIgnorePatterns to empty array.)

pauldraper added a commit to pauldraper/jest that referenced this issue Feb 20, 2022
Resolves jestjs#2145 and jestjs#11781. Prevent haste map from automatically discarding
node_modules files.

By default, node_modules is still excluded via the testPathIgnorePatterns
option. However, users can now use that option to allow node_modules
without hacks.
pauldraper added a commit to pauldraper/jest that referenced this issue Feb 20, 2022
Resolves jestjs#2145 and jestjs#11781. Prevent haste map from automatically discarding
node_modules files.

By default, node_modules is still excluded via the testPathIgnorePatterns
option. However, users can now use that option to allow node_modules
without hacks.
@SimenB SimenB linked a pull request Feb 21, 2022 that will close this issue
@SimenB
Copy link
Member

SimenB commented Feb 24, 2022

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet