In Jest, you can choose to run tests in many different ways.
You could run all tests in a project, run specific test files, or you could even run a single test in a file. Then finally also run tests in watch mode.
Earlier in this course, I only showed you the one way of running tests — that was using the npm run test
command, which ran all tests in a particular project.
Let's go back to our previous test, the “multiply.test.js” test.
You could also run a single test file by using something like this:
npm run test multiply.test.js
So instead of running the entire test suite, this will just run this particular file.
Let's click enter.
As you can see, it ran just that particular test for the multiply.test.js
file.
You could also run just single tests or specific tests in a file by using something like this, using .only
.
You could say test.only
, and let's save that.
As you can see, remember there are three tests in this particular file, but if we execute the npm run test multiply.test.js
...
As you saw previously, it ran three, now it skipped the other two and just the one passed. It actually executed the one.
So that's what the .only
notation does.
Finally, let's look at running tests in watch mode.
Let's go back to our package.json
configuration and let's add the following line in the “scripts”:
"test:watch": "jest --watchAll"
You will shortly see exactly where I'm heading to.
Let's save this package.json
file.
Now let's just go back to our test — actually, let's run the entire suite.
npm run test:watch
Let's watch what happens now.
So, as you can see, you have many more options now.
Here it says:
Let's for example, press on “f”, and now let's just make a test fail .
So, let's fail this particular test change toBe(6)
to toBe(7)
, and let's save.
As you can see, now we have that test that failed.
Now let's change this back to 6.
And as you can see, the test automatically kicked off itself and it runs the entire suite again.
So that's a really nice nifty feature that we could use.
Now that you know how that works, it might be great for you to check out the other options in watch mode, too.