77

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?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Ian
  • 30,720
  • 20
  • 100
  • 179

7 Answers7

114

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/"]
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Chris Hawkes
  • 9,842
  • 6
  • 47
  • 60
  • 4
    I've given this a try with `"jest": { "modulePathIgnorePatterns": ["__tests__/results/"] },` but it doesn't work unfortunately. – Ian Nov 08 '16 at 13:22
  • can you try shortening it? "jest": { "modulePathIgnorePatterns": ["__tests__"] }, – Chris Hawkes Nov 08 '16 at 13:27
  • `__tests__` is the default directory that `jest` uses to load its tests from. So it'd be awkward to rename, I could move my `results` directory up outside of `__tests__` but I'm reluctant to do so unless I can't find an alternative. – Ian Nov 08 '16 at 13:28
  • 1
    yes you're probably right, I'm just wondering if the pathName is somehow choking due to the slashes. In most cases when I'm using exclude directories in webpack I don't include the forward slashes and just the names of the directories like "node_module" etc... – Chris Hawkes Nov 08 '16 at 13:29
  • true. I've just tried moving the directory up, didn't seem to make any difference. I have worked out it's specifically a JSON file within there that I'm modifying that's causing the problem. Trying to just exclude that still doesn't seem to do much though :( – Ian Nov 08 '16 at 13:32
  • In what file? *package.json*? Can you add it to your answer? – Peter Mortensen Dec 29 '20 at 22:35
39

To exclude a directory from Jest testing, use testPathIgnorePatterns

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": {}
    }
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
manas
  • 4,872
  • 7
  • 39
  • 50
9

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Islam Murtazaev
  • 852
  • 2
  • 11
  • 21
4

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

Nima Soroush
  • 9,824
  • 3
  • 46
  • 50
1

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"
],
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
codegeek
  • 4,865
  • 4
  • 31
  • 58
  • 1
    Thanks, Working Added regular expression sample "collectCoverageFrom": [ "src/**/*.{ts,js}", "!src/db_access/*.{ts,js}", "!src/**/__fixtures/.{ts,js,json}", "!src/**/__removed__/.{ts,js,json}", "!src/models/.{ts,js,json}" ] – Abhijith v Mar 30 '21 at 10:56
1

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Faris Rayhan
  • 3,219
  • 1
  • 18
  • 14
-1

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/"
    ]
},
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123