jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.07k stars 6.44k forks source link

Jest not running tests in src/node_modules #2145

Closed modernserf closed 2 years ago

modernserf commented 7 years ago

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.

modernserf commented 7 years ago

I think this is the offending section: https://github.com/facebook/jest/blob/cd4d97b30d73dbb2350dc77a409e39d8b8caca90/packages/jest-haste-map/src/index.js#L681-L692

shanewilson commented 7 years ago

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

  "haste": {
    "providesModuleNodeModules": [".*"]
  }
cpojer commented 7 years ago

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

modernserf commented 7 years ago

@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 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

Discussion continued here: https://github.com/facebookincubator/create-react-app/pull/1081#issuecomment-264351297

ericclemmons commented 7 years ago

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 commented 6 years ago

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

thymikee commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 5 years ago

It would be really nice to have this supported properly. For me neither the workaround nor any other suggestion made it working. :roll_eyes:

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 2 years ago

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.)

SimenB commented 2 years ago

https://github.com/facebook/jest/releases/tag/v28.0.0-alpha.5

github-actions[bot] commented 2 years ago

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.