The Jest documentation suggests using npm test
to execute tests.
Is there a way of watching your source and tests to rerun Jest tests automatically when relevant files have been changed?
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.
To speed-up your tests, Jest can run them in parallel. By default, Jest will parallelise tests that are in different files. IMPORTANT: Paralellising tests mean using different threads to run test-cases simultaneously.
It will be executed before every test suite (every test file). Because test runner is already initialised, global functions like beforeAll and afterAll are in the scope just like in your regular test file so you can call them as you like.
Thanks to Erin Stanfill for pointing out, Jest already has support for automatically re-running. The better configuration for package.json
would be
{ "scripts": { "test": "jest" } }
To turn on the watch mode, just use
$ npm run test -- --watch
Or
$ yarn run test --watch
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With