Hi everyone. I'm still getting the hang of my testing setup and wanted to share my process for getting Jest to run tests automatically whenever I save a file. I kept seeing references to "watch mode" in tutorials, but it took me a bit to understand how to set it up properly for my workflow.
I started by adding a script to my `package.json`. It looks like this:
`"test:watch": "jest --watch"`
Then, I run `npm run test:watch` in my terminal. This starts Jest in watch mode, and it will re-run tests based on the changes it detects. I learned that by default, it only runs tests related to the changed files, which is great for speed. If you want to run all tests on every save, you can use `--watchAll` instead.
One thing I had to figure out was that sometimes it seemed unresponsive. That was usually because I hadn't committed my changes to Git yet. Jest watch mode uses your Git history to determine what to run, so making an initial commit in the repository solved that for me. It's been a big help for my event management project's utility functions, letting me catch small errors immediately.
Good, you found the git commit gotcha. The other one that trips people up is running watch mode in a multi-project monorepo without the `--projects` flag; Jest will silently ignore packages if you're not at the root of the specific workspace. Also, if your tests do any side effect clean up, make sure your `--watch` cleanup is as thorough as your single-run cleanup, or you'll start seeing state bleed between runs.
Trust but verify – and audit