There's also a CLI runner, which is enabled with dash, dash, test. And if you're using an older version of Node that, you know, this hasn't been ported to, like right now Node 14 does not have support for it.
Some kind of people were nice enough to take the code from Core, put it into a package on npm, and you can actually install it from there and get almost all the same functionality. So it supports a lot of the, you know, things that you would support a test, expect a test runner to support out of the box.
So it has subtests. It can skip over tests. You can specify only tests, so you know, you only want to run a subset of your test suite. Lifecycle hooks, such as before, before each, after, and after each, and things like that. And it does also support mocking out of the box.
So it doesn't currently support mocking of entire modules. Because there are some limitations around ES modules there. But you can mock functions, methods, and things like that. It does have built-in code coverage out of the box, which you can enable with the dash-dash experimental test coverage, CLI flag. And you can now also generate your own, or, I'm sorry, write your own custom reporters.
So there are a few that are built into Node. By default, it's going to be TAP, which stands for the test anything protocol, which is a format for expressing test results, which is used, among other ecosystems, not just Node.js. And that's going to be sent over standard out, but you can customize it. You can use SPEC, which is a more human-readable format. TAP, the little format where it just shows dots with Xs. I don't know what the name of that is. And then you can use the test reporter destination flag, where you can actually say where you want your results to be sent to. As I said by default, it's standard out, but you can use standard error and other things.
This is just a quick picture of what a test looks like. We support synchronous tests, asynchronous tests, callbacks, etc. On the left, here is what you would get if you ran the previous test and generated TAP output. On the right is what you get with the SPEC reporter. If you wanted colors and things that are a little more human-friendly, you could use that.
From the TestRunner also came another new feature called node-prefix-only core modules. The difference here, if you look at the top, we're importing the node colon test and node colon assert. Every core module can be imported using the node colon prefix, and that's nice because it lets tools and people reading the code know right off the bat when something is a core module.
Comments