How can I exclude files from Jest watch?

前端 未结 7 1610
眼角桃花
眼角桃花 2021-02-01 12:17

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 (quit

相关标签:
7条回答
  • 2021-02-01 12:58

    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.

    0 讨论(0)
  • 2021-02-01 13:03

    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

    0 讨论(0)
  • 2021-02-01 13:04

    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": {}
        }
    
    0 讨论(0)
  • 2021-02-01 13:07

    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/"]
    
    0 讨论(0)
  • 2021-02-01 13:12

    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"
    ],
    
    0 讨论(0)
  • 2021-02-01 13:12

    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.

    0 讨论(0)
提交回复
热议问题