How can I exclude files from Jest watch?
JavascriptJestjsJavascript Problem Overview
I'm doing some slightly bizarre stuff using Jest for testing where I'm writing some stuff to disk. If I use the watch
flag in Jest however then I'm finding (quite obviously) that each time I write something to disk the tests refire again.
I don't currently have any sort of configuration, and I've taken a look at the documentation, but it's really not clear to me which option I need to be using to suppress watching particular files. I believe I can see options to exclude code from code-coverage and test execution, but not the actual watcher.
In my case I've got a setup like this and I just want to suppress my results directory:
__tests__
__snapshots__
(created by Jest)results
(created by me and the directory to exclude)
- testfile1.js
How can I do this?
Javascript Solutions
Solution 1 - Javascript
From the documentation you need to add modulePathIgnorePatterns which is an array of string values which will be matched against
> modulePathIgnorePatterns [array<string>] # > > (default: []) An array of regexp pattern strings that are matched > against all module paths before those paths are to be considered > 'visible' to the module loader. If a given module's path matches any > of the patterns, it will not be require()-able in the test > environment. These pattern strings match against the full path. Use > the <rootDir> string token to include the path to your project's root > directory to prevent it from accidentally ignoring all of your files > in different environments that may have different root directories. > Example: ['<rootDir>/build/'].
https://facebook.github.io/jest/docs/configuration.html#modulepathignorepatterns-array-string
Add this to your configuration...
modulePathIgnorePatterns: ["directoryNameToIgnore"]
or:
modulePathIgnorePatterns: ["<rootDir>/dist/"]
Solution 2 - Javascript
To exclude a directory from Jest testing, use testPathIgnorePatterns
"testPathIgnorePatterns": ["directory to ignore"]
Below in file package.json, I have configured to ignore the "src" directory
{
"name": "learn-test",
"jest": {
"testPathIgnorePatterns": ["src"]
},
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-scripts": "1.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "jest --watch",
"eject": "react-scripts eject",
},
"devDependencies": {}
}
Solution 3 - Javascript
I had the same problem when I had a directory that must not be tested, although I still wanted it to be inside of the __tests__
directory (e.g., __mocks__
).
You should not use a use relative paths in such case (no slashes). So inside of your package.json
file add:
jest: {
"modulePathIgnorePatterns": ["__mocks__"]
}
That solved my problem.
Solution 4 - Javascript
Based on Jest documentation, watchPathIgnorePatterns should be the way you want to go. That works in Jest 23.x.x and later.
You can add this config to jest.config.js
or package.json > jest
Solution 5 - Javascript
To exclude an entire folder, add the following in the "jest" property of the package.json file:
"modulePathIgnorePatterns": [
"<rootDir>/src/services"
],
To exclude an individual file:
"collectCoverageFrom": [
"!src/index.js"
],
Solution 6 - Javascript
I use the following pattern and it works for me:
collectCoverageFrom: [
'src/**/*.js',
'!src/api/graphql/**/*.js',
'!src/action/**/*.js',
'!src/utils/dev/*.js',
'!src/**/*.e2e.js',
],
! means we exclude the folder or file.
Solution 7 - Javascript
> jest version: 27.2.1
In order to exclude from test and coverage:
"testPathIgnorePatterns" : [ "tests/login/default/MockStrategy.js",],
coveragePathIgnorePatterns: [ "tests/login/default/MockStrategy.js"],
Solution 8 - Javascript
In most of cases you will get the error: Your test suite must contain at least one test.
To ignore/exclude file or folders from the test, please modify the package.json
If the block "jest" does not exist add it like that:
...
"jest": {
"collectCoverage": true,
"testPathIgnorePatterns" : [
"__tests__/properties.js",
"__tests__/properties.js.sample"
]
},
...
Solution 9 - Javascript
here are my 2 cents on this, as I feel there is some confusion in above answers:
Following is the jest segment in package.json file :
"jest": {
"testPathIgnorePatterns": ["wallet-svc/src/db/"],
"coveragePathIgnorePatterns": ["wallet-svc/src/db/"],
"modulePathIgnorePatterns": ["wallet-svc/src/db/"],
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
If you notice I have mentioned 3 ignores - "testPathIgnorePatterns", "coveragePathIgnorePatterns" and "modulePathIgnorePatterns". It so happens at time when you are running tests, the files are ignored but when you run the coverage, jest does not ignore those file and you see them in the coverage report. Better to include all 3 tags.
I found this thread helpful : https://github.com/facebook/jest/issues/1815#issuecomment-684882428
Solution 10 - Javascript
Skipping files in the test cases report collection is possible using "collectCoverageFrom" in the latest version of create-react-app, jest:
"jest": {
"collectCoverageFrom": [
"src/**/*.{js,jsx,ts,tsx}",
"!<rootDir>/src/registerServiceWorker.js",
"!<rootDir>/src/serviceWorker.js",
"!<rootDir>/node_modules/"
]
},
Solution 11 - Javascript
I used this config in my package.json
to exclude index.js
and reportWebVitals.js
files from coverage report.
"jest": {
"collectCoverageFrom": [
"!<rootDir>/src/reportWebVitals.js",
"!<rootDir>/src/index.js"
],
}