From the course: Node.js: Testing and Code Quality

Survey of Node.js testing frameworks

From the course: Node.js: Testing and Code Quality

Survey of Node.js testing frameworks

- [Instructor] We've set up linting tools to scan our application for suspicious code, but that doesn't address how our application works. Let's validate the correctness of our application with unit testing. Throughout this chapter, our focus is going to be on unit testing tools and techniques. We'll start with this survey of testing frameworks and their approaches to testing. With an informed decision, we'll install a testing framework into project. Once the tools are in place, we'll write some unit tests to prevent and detect bugs in our applications. Finally, we'll test two types of asynchronous code, including callbacks and promises. Sounds exciting. Let's get it started with a survey to compare Node.js testing frameworks. Some of the most popular Node.js testing frameworks in alphabetical order include, Ava, Jasmine, Jest, Mocha, and tape. We'll take a brief look at each. AVA available from avajs.dev, is a minimalist testing framework in both its overhead and its approach. It includes both an assertion library out of the box and TypeScript definitions which reduces outside dependencies. Is primary claim to fame is that it's incredibly fast supporting concurrent test execution with separate processes. AVA does not add to the global namespace. The downside is, you have to explicitly require it, but the upside is that it's not arbitrarily injecting itself. AVA supports a simplified setup mechanism for groups of tests, but also includes a more advanced tear down if the environment needs to be reset afterwards. Jasmine available from Jasmine.github.io, is a more popular testing framework. Unlike AVA, Jasmine executes tests synchronously but it doesn't mean it's slow. Of course, Jasmine supports asynchronous tests. Jasmine has no external dependencies, which is actually really impressive because it includes, assertions, test doubles and a lot more. Jasmine adds itself to the global namespace when testing, so you don't have to require any modules when writing tests. Jest from jestjs.io has become incredibly popular as of this publication. Jest can run tests asynchronously like AVA, which gives it fast execution. Jest also includes react support though it's not required. One of the features of the react support is snapshot testing, where you can compare state changes over time of a webpage. Jest also includes test double support as well out of the box. Like Jasmine, Jest adds components to the global namespace for convenience. Finally, Jest includes an interactive watch mode, so developers can constantly run tests as they work. Mocha from mochajs.org is very popular and one of the oldest of the JavaScript frameworks. Mocha executes the tests synchronously, which is slower than some of the other frameworks, but it does support async and a number of other modern standards. It's incredibly flexible and supports plugins. That's good because it does not include either assertions or mocks. The downside is, it increases the number of tools needed, but the upside is that Mocha can focus on being a great test framework. Mocha adds itself to the global namespace for convenience. Like Jest, Mocha includes a file watcher for development. Tape from github.com/substack/tape, is also a minimalist framework like AVA, however it's synchronous, so execution is not in parallel. Also like AVA taped does not provide any setup or tear down methods, and it doesn't add itself to the namespace. In tape, you need to explicitly state when tests are complete, this can be a good thing for preventing hanging tests. So, out of these options, which one will we be using? In this course, we're going to be using Jest for a few reasons. It's the most popular which also means there's a lot of documentation and support for it. Jest is also incredibly fast which in the scope of this course, isn't as important as a real-world application. Jest also provides a lot of out of the box functionality which reduces outside dependencies which in turn reduces the maintenance burden. Finally, if you don't like it, the tests can be easily ported to other frameworks. I recommend trying the others when you get a moment there's no one right way. Now we've made our core selection, let's install Jest in our project and start testing.

Contents