如何从 Jest watch 中排除文件?

How can I exclude files from Jest watch?

我正在使用 Jest 做一些有点奇怪的事情来测试我正在将一些东西写入磁盘的地方。但是,如果我在 Jest 中使用 watch 标志,那么我发现(很明显)每次我向磁盘写入内容时,测试都会再次触发。

我目前没有任何类型的配置,我已经查看了 the documentation,但我真的不清楚我需要使用哪个选项来禁止监视特定文件。我相信我可以看到从代码覆盖和测试执行中排除代码的选项,但不是实际的观察者。

在我的例子中,我有这样的设置,我只想隐藏我的结果目录:

我该怎么做?

您需要从文档中添加 modulePathIgnorePatterns,它是一个字符串值数组,将被匹配

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

将此添加到您的配置中...

modulePathIgnorePatterns: ["directoryNameToIgnore"]

或:

modulePathIgnorePatterns: ["<rootDir>/dist/"]

要从 Jest 测试中排除目录,请使用 testPathIgnorePatterns

testPathIgnorePatterns

"testPathIgnorePatterns": ["directory to ignore"]

下面的文件package.json,我已经配置忽略“src”目录

{
      "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": {}
    }

根据 Jest 文档,watchPathIgnorePatterns 应该是您想要的方式。这适用于 Jest 23.x.x 及更高版本。

您可以将此配置添加到 jest.config.jspackage.json > jest

当我有一个不能测试的目录时,我遇到了同样的问题,尽管我仍然希望它位于 __tests__ 目录中(例如 __mocks__)。

在这种情况下你不应该使用相对路径(没有斜线)。所以在你的 package.json 文件中添加:

jest: {
  "modulePathIgnorePatterns": ["__mocks__"]
}

这解决了我的问题。

排除一个整个文件夹,在[=16=的“jest”属性中添加以下内容]package.json 文件:

"modulePathIgnorePatterns": [
  "<rootDir>/src/services"
],

排除一个个人文件:

"collectCoverageFrom": [
  "!src/index.js"
],

使用最新版本 create-react-app 中的“collectCoverageFrom”可以跳过测试用例报告集合中的文件,笑话:

"jest": {
    "collectCoverageFrom": [
        "src/**/*.{js,jsx,ts,tsx}",
        "!<rootDir>/src/registerServiceWorker.js",
        "!<rootDir>/src/serviceWorker.js",
        "!<rootDir>/node_modules/"
    ]
},

我使用以下模式并且它适用于我:

collectCoverageFrom: [
    'src/**/*.js',
    '!src/api/graphql/**/*.js',
    '!src/action/**/*.js',
    '!src/utils/dev/*.js',
    '!src/**/*.e2e.js',
],

!表示我们排除文件夹或文件。

在大多数情况下,您会收到错误消息: 您的测试套件必须包含至少一项测试。

要测试ignore/exclude个文件或文件夹,请修改package.json

如果块“jest”不存在,请像这样添加:

...
"jest": {
    "collectCoverage": true,
    "testPathIgnorePatterns" : [
      "__tests__/properties.js",
      "__tests__/properties.js.sample"
    ]
},
...

我在 package.json 中使用此配置从覆盖率报告中排除 index.jsreportWebVitals.js 文件。

  "jest": {
    "collectCoverageFrom": [
      "!<rootDir>/src/reportWebVitals.js",
      "!<rootDir>/src/index.js"
    ],
  }

这是我的 2 美分,因为我觉得上面的答案有些混乱:

以下是 package.json 文件中的笑话片段:

"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"
}

如果您注意到我提到了 3 个忽略项——“testPathIgnorePatterns”、“coveragePathIgnorePatterns”和“modulePathIgnorePatterns”。它发生在你 运行ning 测试的时候,文件被忽略但是当你 运行 覆盖时,jest 不会忽略那些文件,你会在覆盖报告中看到它们。最好包括所有 3 个标签。

我发现这个帖子很有帮助:https://github.com/facebook/jest/issues/1815#issuecomment-684882428

jest version: 27.2.1

为了从测试和覆盖中排除:

"testPathIgnorePatterns" : [
  "tests/login/default/MockStrategy.js",
],
coveragePathIgnorePatterns: [
  "tests/login/default/MockStrategy.js"
],