4 tools to supercharge your Jest Testing and increase your productivity while testing
Speed up the feedback loop from your tests to quickly identify and fix issues. (3 min)
The importance of having tests in place is undeniable.
However, the importance of having a fast feedback loop from your tests is usually neglected.
A fast feedback loop from tests helps developers to quickly identify and fix issues.
It improves the overall throughput of a developer.
1. Add a `test:watch` command for the tests
The first thing I do, when I start a new project or join an older one, is to add a jest watch
command inside the package.json
.
It can look something like:
{
...
"scripts": {
...
"test:watch": "jest --watch --verbose",
...
},
...
}
You can learn more about the --watch
and --verbose
jest flags on the official Jest CLI Documentation.
When you run the test:watch
command, it provides a real-time feedback and detailed output of your tests.
It will automatically rerun tests related to the changed files and speed up the feedback loop from your tests.
Usually, when I start refactoring a piece of code or start working on a new feature, I run the test:watch
command and monitor how the changes impact the tests and vice versa.
2. Add a `jest-watch-typeahead` plugin to Jest
jest-watch-typeahead
is a plugin to speed up your testing workflow.
It allows you to filter tests by file name and test name which makes it easier to run specific tests while developing.
The tool is very useful for large projects where you have hundreds of tests.
Instead of remembering specific filenames, with the jest-watch-typeahead
plugin you can quickly find and run the test you need.
You can find more info about the plugin and its installation here.
3. Add additional custom jest matchers
I’ve seen many codebases where several jest matchers are used to assert for something.
For example:
expect(<something>).toHaveBeenCalledTimes(1);
expect(<something>).toHaveBeenCalledWith(<xyz>);
You can enhance the capabilities of Jest’s default matchers by adding custom jest matchers.
jest-extended
is a great package adding a set of additional matchers to make assertions more expressive and code more readable.
I’ve personally used the following custom matchers:
toHaveBeenCalledExactlyOnceWith
expect(<something>).toHaveBeenCalledExactlyOnceWith(<xyz>);
toThrowWithMessage
await expect(
register.execute(registerInput),
).rejects.toThrowWithMessage(
ValidationError,
`Must contain 8-64 characters, 1 uppercase, 1 lowercase,...`,
);
toHaveBeenCalledAfter
expect(connector.verifyMfa).toHaveBeenCalledExactlyOnceWith(
'at',
'abc123',
);
expect(connector.enableMfa).toHaveBeenCalledExactlyOnceWith('at');
expect(connector.enableMfa).toHaveBeenCalledAfter(connector.verifyMfa);
Here is the complete set of the additional matchers from jest-extended
.
4. (optional) Automatically fail tests on console.log
Sometimes we add console.log()
, console.error()
, etc. while debugging, testing or even developing new stuff.
We can later forget to remove these logs and pollute the console.
In a large codebase, we can end up with the test output overloaded by a lot of errors, warnings, etc.
We can automate that through the jest-fail-on-console
utility and make our jest tests fail when console.error()
, console.warn()
, etc. are used.
It’s crucial to keep the console clean because it helps us identify real issues quickly.
You can learn more about the package here.
⭐ TL;DR
Add a
"test:watch": "jest --watch --verbose"
command inside your package.json to speed up the feedback loop from your tests.Add a
jest-watch-typeahead
plugin to additionally speed up your testing workflow.Consider adding additional custom jest matchers if needed to make assertions more expressive and code more readable through
jest-extended
.Optionally, add
jest-fail-on-console
to prevent adding code withconsole
statements and have a clear console output.
👋 Get in touch
You can find me on LinkedIn or Twitter.
This newsletter is funded by paid subscriptions from readers like yourself.
If you aren’t already, consider becoming a paid subscriber to receive the full experience!
Think of it as buying me a coffee twice a month, with the bonus that you also get all my products for FREE.
You can also hit the like ❤️ button at the bottom to help support me or share this with a friend to get referral rewards. It helps me a lot! 🙏
👏 Weekly Shoutouts
The Resume Ghostbuster course from Taha Hussain, an Engineering Career Coach. You can claim a 85% discount until Sep 30th.
It’s $40 off making it $7 only. Code: NEWLIFE 🎉 🎉 🎉
Wow, awesome tips Petar. I didn't know about (2) and (3). Going to check them out.
Also, appreciate your shout-out!
Thank you so much for the shoutout on the Resume Ghostbuster!