如何从开玩笑手表中排除文件?

时间:2016-11-08 12:08:19

标签: javascript jestjs

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

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

在我的情况下,我有这样的设置,我只想抑制我的结果目录:

  • __tests__
    • __snapshots__(由jest创建)
    • results(由我和要排除的目录创建)
  • testfile1.js

有人能说清楚这一点吗?

9 个答案:

答案 0 :(得分:17)

从文档中你需要添加modulePathIgnorePatterns,这是一个将与

匹配的字符串值数组
  

modulePathIgnorePatterns [array< string>]#

     

(默认值:[])匹配的正则表达式模式字符串数组   在考虑这些路径之前,针对所有模块路径   '可见'到模块加载器。如果给定模块的路径匹配任何路径   对于模式,它不会被要求() - 能够在测试中   环境。这些模式字符串与完整路径匹配。使用   < rootDir>字符串标记,包括项目根目录的路径   目录,以防止它意外忽略您的所有文件   在可能具有不同根目录的不同环境中。   示例:['< rootDir> / build /']。

https://facebook.github.io/jest/docs/configuration.html#modulepathignorepatterns-array-string

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

modulePathIgnorePatterns: ["directoryNameToIgnore"]

或:

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

答案 1 :(得分:5)

要从玩笑测试中排除目录,请使用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": {}
    }

答案 2 :(得分:1)

基于Jest文档

Constant            Value   Description
--------            -----   -----------
adStateClosed       0       Indicates that the object is closed.
adStateOpen         1       Indicates that the object is open.
adStateConnecting   2       Indicates that the object is connecting.
adStateExecuting    4       Indicates that the object is executing a command.
adStateFetching     8       Indicates that the rows of the object are being retrieved.

应该是您想要的方式。在watchPathIgnorePatterns

中有效

watchPathIgnorePatterns

答案 3 :(得分:1)

尽管我仍然希望它位于__tests__目录(例如__mocks__)的内部,但是当我有一个必须进行测试的目录时,我遇到了同样的问题。

正如@Chris Hawkes在评论中指出的那样,在这种情况下(不使用斜杠),您不应使用use相对路径。因此,请在您的package.json文件中添加:

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

那解决了我的问题。

答案 4 :(得分:0)

要排除整个文件夹。在package.json文件的jest属性中添加以下内容

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

排除单个文件

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

答案 5 :(得分:0)

可以使用最新版本的create-react-app jest中的“ collectCoverageFrom”跳过测试案例报告中的文件

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

答案 6 :(得分:0)

嘿,我使用以下模式,它对我有用

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

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

答案 7 :(得分:0)

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

要从测试中忽略/排除文件或文件夹,请修改 package.json

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

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

答案 8 :(得分:0)

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

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