jest --watch triggered by temp files

863 Views Asked by At

I'm running Jest 26.4.2 on MacOS Catalina using Emacs as my text editor. When the watcher is running, any time Emacs creates a temp file jest will trigger a run. This leads to many runs triggering that tend to miss the actual file save event.

I'm running jest with npx jest --watch

I've tried various jest configs, the one that looked promising, testPathIgnorePatterns, did not help.

Emacs creates temp files by making a symlink with the pattern .#<target file name> that points to a files that ends with 5 digits. I've tried ignoring all these but it didn't help.

jest.config.js:

  watchPathIgnorePatterns: ['/[0-9]{5}/', '/#/'],
  testPathIgnorePatterns: ['/[0-9]{5}/', '/#/'],
  modulePathIgnorePatterns: ['/[0-9]{5}/', '/#/'],

3

There are 3 best solutions below

0
On BEST ANSWER

The files created by my editor, Emacs / Spacemacs, are lock files. They are created and removed as soon as files are edited, saved or reverted.

I could not get jest to ignore them so I configured my Spacemacs TypeScript layer to not create them in TypeScript mode via (typescript :variables create-lockfiles nil).

0
On

Looking at a recent jest (26.6.1 in april 2021), the createHasteMap() function calls sane with an ignored composed from config.modulePathIgnorePatterns and config.watchPathIgnorePatterns (src). watchPathIgnorePatterns seems appropriate but I'm welcome to counter-advice.

jest.config.js

module.exports = {
  modulePathIgnorePatterns: [
    "\\/\\.#"
  ]
}

If for some reason, you have .../.#... paths in your directory hierarchy that you don't want to skip, you can create a more specific pattern using the keyword <rootDir>, which will be substituted to point to the root of your project. (If you also have a directory called <rootDir>, quit screwin' around.)

1
On

Looks like you forgot to add a dot and remove the slash at the very end (since it's a file not a folder)? So instead of /#/ it should be /.#? I'm not a regex master, so just created two files

  • .#test.test.js
  • test.test.js

and looks like the first one is ignored correctly (npm t -- --watch) with my package.json config. You can also try playing with a <rootDir>, or pass the config file directly using npm t -- --config jest.config.js

"jest": {
    "testPathIgnorePatterns": [
        "<rootDir>/.#",
        "/.#"
    ]
}