Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I exclude files from 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__ (created by Jest)
  • results (created by me and the directory to exclude)
  • testfile1.js

How can I do this?

like image 789
Ian Avatar asked Nov 08 '16 12:11

Ian


People also ask

How do I exclude files from Jest coverage?

To ignore a file pattern for Jest code coverage, we can add the option in the Jest config. to calculate the code coverage from the paths that match patterns listed in collectCoverageFrom that don't have the exclamation mark before it. As a result, we see code coverage calculated from the src/**/*.

How does Jest watch work?

By default, Jest will run tests against all changed files since the last commit, but watch mode also exposes many options. By hitting the w key, you can see the different ways to operate Jest in watch mode. The options are dynamic, so it's worth playing around within watch mode to see what's available.

How does Jest know which files to run?

Jest will look for test files with any of the following popular naming conventions: Files with . js suffix in __tests__ folders. Files with .


1 Answers

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/"] 
like image 57
Chris Hawkes Avatar answered Sep 30 '22 17:09

Chris Hawkes