如何从 Jest 手表中排除文件? [英] How can I exclude files from Jest watch?

查看:64
本文介绍了如何从 Jest 手表中排除文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 refire 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 and 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__(由 Jest 创建)
  • results(由我和要排除的目录创建)
  • __tests__
  • __snapshots__ (created by Jest)
  • results (created by me and the directory to exclude)
  • testfile1.js

我该怎么做?

推荐答案

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

From the documentation you need to add modulePathIgnorePatterns which is an array of string values which will be matched against

modulePathIgnorePatterns [array] #

modulePathIgnorePatterns [array<string>] #

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

(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"]

或:

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

这篇关于如何从 Jest 手表中排除文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆