So we use it a lot. And code coverage is the next step for the supports that I want to tell you about. So it has, the Node.js test runner has a native support. You just pass on the flag, which is experimental test coverage. It's gonna use the V8 built-in coverage function to give you an output like this. So it's gonna give you, this is the command that we are gonna run, and this is gonna be the tests that are gonna run. And then we have the usual TAP output that we have on the side, and then we have the start of the coverage report. As you can see, in the coverage result, we have three, two lines that were not covered, which are the ones that we throw here in our function. So it works as expected, right? And then you can also change the coverage reporters. You can use different reporters, and or reporting directly to files, using the test reporter or the test reporter destination. And right now the support, the native test runner supports these reporters. So you can use spec.tap, lcol, and JUnit reporters as well, natively, just put them in the test reporter, equals, and the name of the reporter.
But now let's talk about TypeScript, which is the thing that I love the most. So a bit of a side here, something on the side, but I don't know if you know all of this, but Node already supports you to compile TypeScript on the fly using importers, right? So importers were called loaders previously, and they are functions that execute before a module is loaded and they can also be used to transform a module before it is imported. So Babel has loaders, Jest has loaders, a lot of other tools that we use has loaders because you can import the module and then it's going to change the file that you're in, and then it's going to execute the file that you're in, right? So this means that you can also basically run TypeScript because you can just import a TypeScript file and then convert this to JavaScript, right? And then the common importers that we have today, there are ts-node, ts-sex, and ts-build. You can also build your own if you want, if you don't want to use any external libraries. I like ts-sex, I think it's one of the best ones out there. And for the TypeScript support, I've just changed the file that we had before. I just added some types, and then we can run it with import ts-sex and basically the ts file index.ts, and then boom, magic, right? But what if I told you that you can actually use this to run the test runner? So the TypeScript support, let's just take our previous file extension and we just append dash dash test to it. So boom, it runs, nothing else, like, it's magic. That's run, everything runs, there's no configuration, no nothing, we just run with native stuff, right? And when you look at this, like, 50 line configuration jazz file that you've been maintaining for this, you know, past five years, you're going to see that this is quite of a bliss because you don't have to maintain TypeScript anymore, so it's super, super nice. And there are other supported features for a node test runner so you can actually abort tests via abort signals. You can skip the test, like I said, the it skip or it to do to test that is still to be done. You can flag one test that you want to run, so with it only, it's funny, super nice. And we also have life cycle hooks like Jess does with before, after, before each, after each and stuff like this. And you can also filter the name pattern via rejects. Now you can actually do, I think Node 21 has added the support for globs, so you can actually add globs in the file names and you have a watch mode, which is experimental. But the future of Node.js testing, and funny enough, Colin himself has a list of what he wants for the Node.js test runner. So the first thing I wanted to show you is the module mocking. So basically the module mocking is something that is coming up in the test runner as well.
Comments