如何从Jest手表中排除文件?

2022-08-30 04:34:58

我正在使用Jest进行测试,其中我正在将一些东西写入磁盘,从而做了一些稍微奇怪的事情。但是,如果我在Jest中使用该标志,那么我发现(很明显)每次我写一些东西来磁盘时,测试都会再次重新触发。watch

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

在我的情况下,我有一个这样的设置,我只想禁止显示我的结果目录:

  • __tests__
  • __snapshots__(由Jest创建)
  • results(由我创建并排除的目录)
  • 测试文件1.js

我该怎么做?


答案 1

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

modulePathIgnorePatterns [array<string>]#

(默认值:[])一个正则表达式模式字符串数组,这些字符串与所有模块路径匹配,然后这些路径才被视为模块加载程序“可见”。如果给定模块的路径与任何模式匹配,则在测试环境中它不会是 require()-able。这些模式字符串与完整路径匹配。使用<rootDir>字符串标记来包含项目根目录的路径,以防止项目意外忽略可能具有不同根目录的不同环境中的所有文件。示例:['<rootDir>/build/']。

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

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

modulePathIgnorePatterns: ["directoryNameToIgnore"]

艺术

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

答案 2

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