Jest watch restart fails for gatsby site with fs/watchers failure

Viewed 196

I'm working on a Gatsby site. I have test in my package.json set to "test": "jest --watch --no-cache",. Mostly, when I save, watch works with no issue. However, occasionally, perhaps with a syntax error, my watch errors, fails and exits. I'd like to fix this but don't really know what direction to head based on this error.

My Jest console output is the following:

Test Suites: 0 of 8 total
Tests: 0 total
Snapshots: 0 total
Time: 0 sinternal / fs / watchers.js: 173
throw error;
    ^

  Error: ENOENT: no such file or directory, watch '/home/[USER]/backup/myGatsbySite/.cache/redux.bak'
at FSWatcher.start(internal / fs / watchers.js: 165: 26)
at Object.watch(fs.js: 1340: 11)
at NodeWatcher.watchdir(/home/[USER] / backup / myGatsbySite / node_modules / sane / src / node_watcher.js: 159: 22)
at NodeWatcher.<anonymous>(/home/[USER] / backup / myGatsbySite / node_modules / sane / src / node_watcher.js: 295: 18)
at FSReqCallback.oncomplete(fs.js: 170: 5) {
  errno: -2,
    syscall: 'watch',
      code: 'ENOENT',
        path: '/home/[USER]/backup/myGatsbySite/.cache/redux.bak',
          filename: '/home/[USER]/backup/myGatsbySite/.cache/redux.bak'
    
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! gatsby-starter-default@0.2.0 test: `jest --watch --no-cache`
npm ERR! Exit status 1

There's also a link to an npm log which I've pasted below.

0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli   '/home/[USER]/.nvm/versions/node/v12.11.1/bin/node',
1 verbose cli   '/home/[USER]/.nvm/versions/node/v12.11.1/bin/npm',
1 verbose cli   'run',
1 verbose cli   'test'
1 verbose cli ]
2 info using npm@6.11.3
3 info using node@v12.11.1
4 verbose run-script [ 'pretest', 'test', 'posttest' ]
5 info lifecycle gatsby-starter-default@0.2.0~pretest: gatsby-starter-default@0.2.0
6 info lifecycle gatsby-starter-default@0.2.0~test: gatsby-starter-default@0.2.0
7 verbose lifecycle gatsby-starter-default@0.2.0~test: unsafe-perm in lifecycle true
8 verbose lifecycle gatsby-starter-default@0.2.0~test: PATH: /home/[USER]/.nvm/versions/node/v12.11.1/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/home/[USER]/backup/myGatsbySite/node_modules/.bin:/home/[USER]/.shopify-app-cli/bin/user:/home/[USER]/.nvm/versions/node/v12.11.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
9 verbose lifecycle gatsby-starter-default@0.2.0~test: CWD: /home/[USER]/backup/myGatsbySite
10 silly lifecycle gatsby-starter-default@0.2.0~test: Args: [ '-c', 'jest --watch --no-cache' ]
11 silly lifecycle gatsby-starter-default@0.2.0~test: Returned: code: 1  signal: null
12 info lifecycle gatsby-starter-default@0.2.0~test: Failed to exec test script
13 verbose stack Error: gatsby-starter-default@0.2.0 test: `jest --watch --no-cache`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/home/[USER]/.nvm/versions/node/v12.11.1/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:332:16)
13 verbose stack     at EventEmitter.emit (events.js:210:5)
13 verbose stack     at ChildProcess.<anonymous> (/home/[USER]/.nvm/versions/node/v12.11.1/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:210:5)
13 verbose stack     at maybeClose (internal/child_process.js:1021:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5)
14 verbose pkgid gatsby-starter-default@0.2.0
15 verbose cwd /home/[USER]/backup/myGatsbySite
16 verbose Linux 5.4.0-7642-generic
17 verbose argv "/home/[USER]/.nvm/versions/node/v12.11.1/bin/node" "/home/[USER]/.nvm/versions/node/v12.11.1/bin/npm" "run" "test"
18 verbose node v12.11.1
19 verbose npm  v6.11.3
20 error code ELIFECYCLE
21 error errno 1
22 error gatsby-starter-default@0.2.0 test: `jest --watch --no-cache`
22 error Exit status 1
23 error Failed at the gatsby-starter-default@0.2.0 test script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

My jest.config:

module.exports = {
  transform: {
    "^.+\\.jsx?$": `<rootDir>/jest-preprocess.js`,
  },
  moduleNameMapper: {
    ".+\\.(css|styl|less|sass|scss)$": `identity-obj-proxy`,
    ".+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": `<rootDir>/__mocks__/file-mock.js`,
  },
  testPathIgnorePatterns: [`node_modules`, `.cache`, `public`],
  transformIgnorePatterns: [`node_modules/(?!(gatsby)/)`],
  globals: {
    __PATH_PREFIX__: ``,
  },
  testURL: `http://localhost`,
  setupFiles: [`<rootDir>/loadershim.js`],
  setupFilesAfterEnv: ["<rootDir>/setup-test-env.js"],
}

I don't really know where to start. Answers that solve the problem are welcome, but those that explain what part of these two errors you read so I can trouble shoot this myself going forward would be especially appreciated.

1 Answers

Problem

When running jest —watch Jest keeps track of all files in the directory for updates via fsevents. When an item it was tracking in the directory is removed it notices a change but is unable to reload the file no longer exists resulting in “no file or directory found error”.

When gatsby develop starts it creates “.cache” directory–which as you would expect is subject to constant modifications during development process.

I’m not entirely certain about the exact mechanism, but Gatsby is likely removing the .bak file in the “.cache” directory and Jest is likely encountering a “file not found” error when that occurs.

Solution

Configure Jest to ignore “.cache” directory.

Example

"jest": {
    "testPathIgnorePatterns": [".cache"]
 }

Note

This is an tracked issue that may have been resolved in the latest version of Jest: https://github.com/facebook/jest/issues/8265

Related