Why you might not need Jest anymore
A good argument for using some new technology is always
it's native
or
it's included in the standard library
Using code which is officially supported by the language rarely has downsides. It's pretty much guaranteed to not become outdated, it's updated when you update your language version and code from the std-library is never going to become a stale project because of open source drama (looking at you 🦀-foundation).
import test from 'node:test';
The node test runner is now stable since node v20.0.0. Usually I just reach for jest to run my tests but seeing how jest has had such a big problem with es-modules I reached out to the native node test runner to give it a shot. Fortunately the node:test api has been heavily inspired by jest's api making a transition quite easy. This also makes sense since jest has been the industry standard for testing in node for a while now.
Creating a test
Creating a test in jest and node looks eerily similar:
The only major difference is the node test runner requires you to import the functions called. Which I prefer since it removes the "magic" part of creating the tests while still keeping them declarative.
Testing async functions
Async functions are first class citizens in the node:test module:
The api works the same, any async function which returns a rejected promise in the it function is a failed test. That means this is also a failed test:
Type assertions
The node assert package also has a neat feature in that it returns the asserts value return type which means you will get first class typescript support in your tests.
In a typical jest test this would not narrow the type to a string even though we clearly have asserted that the type is supposed to be a string
The asserts package in node will correctly infer the narrowed type
Hooks
The node api also has support for the regular lifecycle hooks jest also exposes such as beforeAll, beforeEach, afterAll and afterEach allowing you to setup and destroy resources and mocks needed for tests:
Again, node requires you to import these lifecycle hooks from the node:test module