-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
[Bug]: Unable to run a test from whitelisted "node_modules" #11781
Comments
Hey @kettanaito, You are probably hitting a limitation/rationalisation around how Jest's internal crawler/cache system |
Thanks for the quick reply, @sigveio! I see. I was suspecting some internals at play, it's good to know. What would you recommend to run tests from |
If I were to fire from the hip, probably forking Does that sound about right, @cpojer? |
That's still unexpected, is it not? I understand that I'd be willing to submit a pull request to fix this if you indeed find this an issue. A corner case, I agree, but still. |
I'd like to add another use case here. We are running a platform (WordPress > WooCommerce) with an ecosystem of plugins/extensions. As platform developers, we created a package containing a suite of tests (many tests in many files). Now, we would like plugin developers (within our company and from the community) to consume this package. Run the suite, to make sure their plugin integrates well with the platform and serves a consistent UX. The problem is how one can consume test suites from the npm package?:
The only fallback I can think of is to ask consumers, to clone the test file structure, and import every single suite separately. What's extremely fragile for the maintenance updates, requires manual labor, and pollutes the consumer's files structure, impairing DevX. |
I'll chime in with my use case - I'm a professor using GitHub classroom to automatically provision repositories for my students to work on, and I use Jest to help me with grading to ensure students have met specific requirements in their code. I keep my tests in a separate repository, and make it a dependency in Here's an example of how I have my tests listed as a dependency: https://github.com/ProfessorKolodziej/cm523-hello-world/blob/main/package.json#L79 How I configured Jest to run tests from node modules in a hacky way that I know is going to fail next time I update Jest because And then the actual test for this assignment that I run across all my class's repositories is here: https://github.com/ProfessorKolodziej/cm523-unit-tests/blob/main/hello-world/index.test.js Right now, this setup works fine for anyone on a Mac or Ubuntu, but I just had a student report that this is broken on Windows, and I'm not sure how to keep my couple of students on Windows moving now. They run a test command locally before pushing changes to GitHub to more quickly check that their requirements are in place. Being able to pull a test from a specific repository in |
Thanks for the suggestion, @sigveio. By looking at jest-haste-map, I can see two places where it decides how to treat files that are located in
While the It seems that by setting the // jest.config.js
module.exports = {
haste: {
retainAllFiles: true
}
} Unfortunately, Jest seems to be supporting only selective
Please, @sigveio, do you happen to know why not all |
Hey @kettanaito, I have had exactly the same problem. Your last message really helped me to understand and fix it on my side. Here is how I did it: I used const path = require("path");
module.exports = config = {
...
testPathIgnorePatterns: [], // this is also important, by default jest.config ignore `/node_modules/` folder.
haste: {
hasteMapModulePath: path.join(__dirname, "./custom-haste-map.js"),
}
}; custom-haste-map.js const path = require("path");
const JestHasteMap = require("jest-haste-map");
const NODE_MODULES = path.join(__dirname, "/node_modules/");
class CustomHasteMap extends JestHasteMap {
_ignore(filePath) {
const ignorePattern = this._options.ignorePattern;
const ignoreMatched =
ignorePattern instanceof RegExp
? ignorePattern.test(filePath)
: ignorePattern && ignorePattern(filePath);
return (
ignoreMatched ||
(!this._options.retainAllFiles && filePath.includes(NODE_MODULES))
);
}
} And that's it. Hope it works on your side too 😉 |
This is an issue that's been around for a while: #2145 There are clear use cases for running jest embedded from within a parent Is this a feature that will eventually be supported or not? If not it should at least be documented to discourage people from trying to embed jest in the first place (this can save folks quite a bit of time/effort). |
That's exactly why jest has the configuration option The bug, is that no matter what value you set This is due to complicated multi-level architecture around listing file, and took me hours to figure out. One wonders what the purpose of even having the My hack is: 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; Configure that with |
This should be closed as dup of #2145 |
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.
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.
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. |
Version
27.0.6
Steps to reproduce
yarn install
yarn test
Expected behavior
Jest detects the
node_modules/__test/example.test.js
file and runs it.The file matches the
testMatch
pattern, which you can confirm by clicking on that pattern in the terminal and being navigated to the correct existing directory (tested in VS Code).Actual behavior
0 matches
next to the correct pattern pointing to an existing directory undernode_modules
is not expected.Additional context
I distribute a test suite as a node module because it acts as a specification for multiple projects. I wish to run that test suite from the
node_modules
where it's installed.Regardless of my setup, it's confusing that Jest seems to ignore anything as long as it includes "node_modules" even if I've explicitly whitelisted a certain directory in it.
Environment
System: OS: macOS 11.5.1 CPU: (16) x64 Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz Binaries: Node: 12.18.2 - /usr/local/bin/node Yarn: 1.22.10 - /usr/local/bin/yarn npm: 6.14.5 - /usr/local/bin/npm npmPackages: jest: ^27.0.6 => 27.0.6
The text was updated successfully, but these errors were encountered: