How to Exclude files from jest watch?

2019-04-21 08:05发布

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 re-fire 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, 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

Can anyone shed some light on doing this?

4条回答
再贱就再见
2楼-- · 2019-04-21 08:20

to exclude a directory from jest testing use testPathIgnorePatterns

testPathIgnorePatterns

"testPathIgnorePatterns": ["directory to ignore"]

below in 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": {}
    }
查看更多
Melony?
3楼-- · 2019-04-21 08:21

Based on Jest docs

watchPathIgnorePatterns

should be the way you want to go. That works in Jest > 23.x.x

watchPathIgnorePatterns

查看更多
我只想做你的唯一
4楼-- · 2019-04-21 08:26

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

modulePathIgnorePatterns: ["directoryNameToIgnore"]

or :

modulePathIgnorePatterns: ["<rootDir>/dist/"]
查看更多
做个烂人
5楼-- · 2019-04-21 08:27

I had the same problem when I had a directory that must not be tested, although I still wanted it to be inside of __tests__ directory (e.g. __mocks__).

As @Chris Hawkes has pointed out in the comments 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.

查看更多
登录 后发表回答