how to get Jest --watch to run on changes to JSON/YAML files?

Viewed 514

I'm running some jest tests with --watch that depend on data fixtures. I want the watch run to trigger when I edit my data files, not just the code. I've added the following to my package.json specifically adding yaml for moduleFileExtensions but still not having any luck. Based on: https://jestjs.io/docs/en/configuration#modulefileextensions-arraystring

Is there a setting I could make to the package.json to see if it's even getting picked up at all? I guess next step is to try with a .js config, throw some errors in and see if I'm barking up the wrong tree!

The yaml files are not part of an "import" or otherwise, they are just loaded by my code. So I'm hoping for a way that "touching" the files would re-trigger the watch to run.

Perhaps the watch command needs to also specify what data files to watch?

  "jest": {
    "verbose": true,
    "modulePaths": [
      "cdn",
      "src"
    ],
    "moduleFileExtensions": [
      "js",
      "json",
      "jsx",
      "ts",
      "tsx",
      "yaml"
    ]
1 Answers

According to this issue moduleFileExtensions is the right place for configuring additional file extensions to watch. However instead of modulePaths you should use roots to configure directories that you want watched.

Jest definition of modulePaths:

An alternative API to setting the NODE_PATH env variable, modulePaths is an array of absolute paths to additional locations to search when resolving modules.

Modules are resolved before the tests are executed and are not expected to change much. If you're wanting to add directories for Jest to watch then you might consider using the roots config.

Jest definition of roots:

A list of paths to directories that Jest should use to search for files in.

package.json

{
  ...
  "jest": {
    "roots": [
      "<rootDir>/cdn",
      "<rootDir>/src"
    ],
    "moduleFileExtensions": [
      "js",
      "json",
      "jsx",
      "ts",
      "tsx",
      "node",
      "yaml",
      "yml"
    ]
  }
}

Note that I also added "yml" to moduleFileExtensions just in case you're using yml/yaml.

Related