Hi everyone! Thanks for coming to my talk. Today I'm going to be talking about zero dependency testing with Node.js, which essentially means that you can start writing your unit tests, integration tests, without having to install anything from NPM.
So before getting into the the nature of Node's new test runner, I wanted to talk a little bit about why a test runner was desired in the first place. So almost all projects need a test runner. So whether you're building an application or a module that you're planning to publish on NPM or whatever, if you're planning to have other people use your code, you almost certainly need tests for it. And then Node.js for years now has shipped with a really good assertion library that's just import assert. This is the assertion library that I've been using for years now. I like it, so that's one less dependency.
And then most test runners overlap a lot in terms of functionality anyway. So, you know, every test runner runs some tests. They generally have features like timeouts, you know, reporting which tests passed and failed, skipping tests, things like that. So, you know, there are differences, so some test runners are more suited to front-end development, some do things like injecting globals into your code without your knowledge, some execute their tests inside of different contexts, so, you might have surprising results whenever you check for equality and things like that. But, you know, so there are these rough edges, but in general, a lot of test runners have a lot of overlap.
And then on top of that, NPM is just really a dangerous place. There's, you know, over the years, there's been a number of incidents, things like left pad, the colors JS thing, even more recently, the minimist package, which I think has like 50 million downloads or something like that, nothing happened to it on NPM, but the GitHub repository went away. So, all of these third party dependencies that you're taking on come with some risk and some cost. And so, that's just, you know, one reason why having a test runner built in, I feel, is useful. And also, there's just a general trend to have more of these batteries included in the runtimes. So, you know, now Node has a built-in test runner. I'm pretty sure Bun has one, I know Deno has one. So, this is becoming more and more common.
And then, you know, here's my tweet from over a year ago, I believe that Node should ship a test runner and, you know, I feel pretty strongly about that. So, some of the features in the new test runner, you can run the test runner through the CLI interface that has the Node chips now with the dash dash test flag. Or you can actually just execute a standalone file containing tests. So, say you have your file foo.js, you can say Node foo.js and if you're using the test runner in there, it will still just work. When it comes to actually writing tests themselves, there's, you know, we support synchronous code, promises or async await based code. And even, you know, because Node still does have a lot of callback-based APIs, we support callback-based tests as well. If you're coming from a test runner like tap or tape, then we do support test style tests, using the test function. If you're coming from a test runner like Mocha or Jest, we have describe and IT functions. So, under the hood, everything uses test, describe and IT are just kind of implemented on top of test.
Comments