Subscribe

Running single Jest tests

✍️

Did you know you can run only one test in a Jest suite?

6 Apr, 2022 Β· 2 min read

I found out this only after doing some research for this Jest series.

Apparently, we get the test.only function, which we can use to only test one test in a suite.

This can be particularly handy if one test in a whole file fails.

To use it, you can set up the following structure.

test.only('Test one', () => {
  console.log(`First test`);
});

test('Test two', () => {
  console.log(`Second test`);
});

It will only run the first test and succeed the whole file.

Some common issues

There are some common failures with Jest test that can give you headaches.

The most simple one is the test failing because you changed something in your functions.

Another one might be that you included a time-based check or something related to that.

You can quickly use the .only method to determine if a test fails when it runs isolated.

If that is the case, you can start looking at that specific test and debug it.

However, more often, we might find the individual test succeeding, but it fails when run with all the other tests.

That means something else is interfering with this test, and often you can look at the before or after functions you set up. There might be something you are resetting or not resetting that you might have missed.

Conclusion

Try to run them individually with test.only when hitting failing tests.

If they succeed, try and evaluate your recurring actions. If they fail, it’s most likely a mistake in your test or function you are testing.

I hope this test.only function helps others, as I was not aware of it before.

Thank you for reading, and let’s connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next πŸ“–

Testing library asFragment tests

18 Apr, 2022 Β· 2 min read

Testing library asFragment tests

Changing the default testing container

17 Apr, 2022 Β· 3 min read

Changing the default testing container

Join 2099 devs and subscribe to my newsletter