Exploring Node.js Test Runner

Rate this content
Bookmark

The talk "Exploring Node.js Test Runner" delves into the concept of a test runner, shedding light on its essential role within the Node.js ecosystem. It provides an overview of why the development of a test runner for Node.js took considerable time, and presents an exploration of its inner workings.

This talk has been presented at TestJS Summit 2023, check out the latest edition of this JavaScript Conference.

FAQ

The new feature discussed is the Node.js test runner, a CLI tool for executing tests and integrating a reporter to export test outcomes.

Marco Ippolito is one of the maintainers of Node.js, focusing on security and the open-source ecosystem.

A test runner is a CLI tool that executes tests, integrates a reporter to export test outcomes, and goes through the source code to pick the test files to run.

Popular test runners and testing frameworks in Node.js include Mocha, Jest, TAP, Cypress, and Chai.

Node.js follows the minimal core principle, encouraging the creation of NPM packages for additional features instead of adding them to the core. This has led to a large ecosystem, which can be overwhelming for new developers.

The Node.js test runner is heavily inspired by Node.tap, a popular testing framework and test runner created by Isaac, the creator of NPM.

Features include filtering, sub-testing, reporting, mocking, watch mode, and code coverage. It also supports BDD keywords and has experimental features like watch mode and code coverage.

To execute tests, run `node --test` from Node 20. It automatically selects the test folder inside your application and runs all JavaScript files inside. You can also specify the folder and file extensions.

Yes, but it requires a transpiler like TSNode or TSX. You can use the `--require` flag to run TypeScript tests easily.

The built-in code coverage feature uses V8 internals, making it faster and more accurate than other libraries that add checks during execution.

Marco Ippolito
Marco Ippolito
28 min
07 Dec, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Today's Talk introduces the new Node.js test runner and its features, including filtering, sub-testing, and reporting. It also discusses executing and writing tests in Node.js, as well as the features of the Node.js testing library. The advantages of the Node.js test runner include the ability to create custom test reporters and use TypeScript. However, there are limitations such as a small ecosystem and limited libraries. Upcoming features include test planning, faster test running, and continuous evolution. The Q&A session covers topics like test runner speed, reporters, sharding, and parallelization.
Available in Español: Explorando Node.js Test Runner

1. Introduction to Node.js Test Runner

Short description:

Today we're going to talk about the new feature of Node.js, the test runner. A test runner is a CLI tool that executes tests and integrates a reporter to export the outcome. Mocha, Cypress, and Chai are popular testing frameworks built on top of Mocha, providing different functionalities. These components enable testing in our applications.

Hello, everyone. Thank you for having me today. So today we're going to talk about the new feature of Node.js, the test runner. First let me introduce myself. I'm Marco Ippolito. I'm one of the maintainers of Node.js, mostly focusing on security. And I work a lot in the open source ecosystem of Node.js.

So let's start by definitions. So what's a test runner? So the test runner is actually the CLI tool that executes the test and integrates a reporter to export the outcome of the test. So it goes through your source code and picks the test files to run. So let's see a bit of classification. So Mocha, for example, is a very popular test runner and testing framework because the two things go together. And the test runner always includes the testing framework because otherwise, what you're going to pick the file to execute but what else are you going to execute? So testing framework provides the content that is inside the file while the test runner actually finds the file to execute. So Cypress is another popular testing framework that is built on top of Mocha. So Mocha takes care of going through the source code and finding which files you want to execute, and then Cypress is built on top of the BDD libraries of Mocha. While Chai is only an assertion library that's also built on top of Mocha. So we have three main components that allow us to perform testing in our application.

2. Node.js Test Runner Features

Short description:

In the Node.js ecosystem, there are many test runner testing frameworks due to the minimal core principle. Node.js prefers to keep its core minimal and rely on NPM packages for additional features. However, this has led to a large ecosystem, making it difficult for new developers to find resources and increasing the risk of NPM supply chain attacks. To address this, Node.js introduced its own native test runner and testing framework, inspired by Node.tap. The Node.js test runner provides features such as filtering, sub-testing, and reporting.

Okay. So now we talked a lot about libraries but what about Node.js itself? So in the Node.js ecosystems there are a lot of libraries and I want to, like, let's see the most popular ones. So we have Mocha that we talked about, Jest, TAP. We have like a huge amount of libraries. And while this is good to have a lot of variety, it's actually bad for new developers that want to learn how to do tests in Node.js because there is an incredible amount of information and it's hard to find a guide and something to begin testing with.

So what's the reason why there are so many test runner testing frameworks in the Node.js ecosystem? Well it's because Node.js always follows the minimal core principle and we can talk about this is also related to the whole JavaScript ecosystem. So Node.js doesn't want to add to its core nothing that can be a NPM package. So when someone requests a feature to Node.js what we say, just create a package. But with years this led to a huge ecosystem and it's deteriorated the developer experience. So people who wanted to learn how to use Node.js found it very hard because of the huge amount of packages and also having too many packages increased the surface for NPM supply chain attacks. And I'm one of the team members of the Node.js security team and we have been trying to decrease the risk for NPM supply chain attack with the Node.js permission model and many things. So having something built in the core is one package less, one potential vulnerability less.

Alright, so Node.js was actually shipping only an assertion library. Since Node 12 there was no test runner, no testing framework, just a very very small assertion library which was not very useful. So Node.js needed a native test runner and a native testing framework so you could execute tests in Node.js without installing any other dependency. So it took some time. It was marked as stable in Node 20. So from Node 20 which is the LTS and if you're not using Node 20 you should. You can use the Node.js test runner. So yeah, it took some time. It took like a couple of years to make it but we finally have it. And now we're going to explore some of the features. So the Node.js test runner was actually heavily inspired from Node.tap. Node.tap is one of the, in my opinion, best testing frameworks and test runners in Node.js. It's been created by Isaac, the creator of NPM, so it's quite popular and we took big inspiration for this library and it's actually one to one. You can just replace the import and it's the same. It uses the same functions. So let's see some of the features of the Node.js test runner. So we will see like filtering and how to execute certain tests and skip other ones. We will talk about sub-testing, reporting.

QnA