Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Watch and rerun Jest JS tests

Tags:

npm

jestjs

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?

like image 770
Martin Dow Avatar asked Aug 24 '14 14:08

Martin Dow


People also ask

How does Jest watch work?

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.

Do Jest tests run concurrently?

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.

Does Jest cleanup after each test?

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.


1 Answers

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 
like image 58
wuct Avatar answered Oct 12 '22 15:10

wuct