This article was originally published in 2017 and has been updated for 2020.
My colleagues often write about how unit tests are great for peace of mind and reducing software errors. So I always make the time to test.
- Comes with assertions, spies, and mocks, so pretty much everything you may need to start developing your unit tests. Jasmine makes the initial setup easy and you can still add libraries if you really require unit functionality
- Globals make it easy to start adding tests to your app right away. Although I dislike globals, Jasmine provides developers with everything you need out-of-the box, and there isn’t much inconsistency
- I found the standalone version made it easy to see just how everything is setup and you can start playing around with it right away
- Integrates with Angular 1 and Angular 2 alongside many popular libraries today
My thoughts on Jasmine
I’m not a fan of having the globals populating the environment, so Jasmine does lose a few points in my book there. Otherwise, it has good variety of features out of the box. It does seem slightly “older” than the other frameworks on this list but that is not necessarily a bad thing and any pain points would have been encountered by others, meaning they should be easy to resolve.
- AVA doesn’t create any globals for you, therefore you can control more easily what you use. I think this brings extra clarity to the tests ensuring that you know exactly what is happening
- Contains a simple API which provides you with only what you need. This can be nice if you would like mocking support, but you’ll have to install a separate library
- Snapshot testing is provided via jest-snapshot which is great when you’d like to know when your application’s UI changes unexpectedly.
My thoughts on AVA
The most minimal of all the frameworks on the list, Tape is straight to the point and provides you with the bare essentials.
- Just like AVA, Tape doesn’t support globals, instead requiring you to include them yourself. This is nice as it doesn’t pollute the global environment
- Tape contains no setup/teardown methods. Instead it opts for a more modular system where you will need to define setup code explicitly in each test making each test more clear. It also stops the state being shared between tests
- Typescript/coffeescript/es6 support
My thoughts on Tape
Arguably the most used library, Mocha is a flexible library providing developers with just the base test structure. Functionality for assertions, spies, mocks, and the like are then added via other libraries/plugins.
- If you want a flexible configuration, including the libraries that you particularly need, then the additional set up and configuration required for Mocha is something you definitely need to check out
- Unfortunately the above point does have a downside, which is having to include additional libraries for assertions. This does mean that it’s a little harder, if not longer, to set up than others. That said, setting up is generally a one-time deal, but I do like being able to go a “single source truth” (documentation) instead of jumping around the show
- Mocha includes the test structure as globals, saving you time by not having to
requireit in every file. The downside is that plugins just might require you to include these anyway, leading to inconsistencies, and if you have a high attention to detaill like me it can be frustrating!
My thoughts on Mocha
The extensibility and sheer number of different ways you can configure Mocha impresses me. Having to learn Mocha, then also having to learn the assertion library you choose does scare me a little though. Flexibility in it’s assertions, spies and mocks is highly beneficial. Overall, Mocha covers the basics, and allows developers to extend it with other frameworks.
- For smaller projects you might not worry about this too much initially, having increased performance is great for larger projects wanting to continuously deploy their app throughout the day
- Whilst developers primarily use Jest to test React applications, Jest can easily integrate into other applications allowing you to use it’s more unique features elsewhere
- Snapshot testing is a great tool to ensure that your application’s UI doesn’t unexpectedly change between releases. Although more specifically designed and used in React, it does work with other frameworks if you can find the correct plugins
- Unlike other libraries on the list, Jest comes with a wide API, not requiring you to include additional libraries unless you really need to
- Jest continues to improve considerably with every update they make
My thoughts on Jest
Whilst the globals are a downside, Jest is a feature rich library constantly being developed. It has a number of easily accessible guides to help out, and supports a variety of different environments which is great to see when building any project.
Puppeteer is a Node library developed by Chrome’s Development Team. It is a framework for test execution, that enables users to control a headless Chrome.
- Most people use Puppeteer to perform several different tests on web applications. These tests may include page structure test, crawl test, and even the capture of screenshots. Puppeteer supports features from ES6such as async and await. It offers automation support for UI testing, form submission, and keyboard inputs.
My thoughts on Puppeteer
Puppeteer is the de facto standard tool for Chrome headless. Due to being backed by a powerful tech giant, Puppeteer has gained some traction. However, the userbase is still small compared to tools like Selenium, which continue being the standard when it comes to browser automation.
- John Resig developed QUnit originally as part of the jQuery library.
- QUnit gained its own identity as early as 2008, obtaining a dedicated name, home, and documentation.
- That way, other developers could use QUnit for unit testing, but it still depended on the jQuery library. That was fixed in a 2009 rewrite, allowing QUnit to run independently of jQuery.
My thoughts on QUnit
QUnit’s main advantages are the extremely easy setup and great performance when testing the DOM. It’s also very well supported, being around for quite a long time. However, QUnit is not without its downsides. It can be somewhat troublesome to write asynchronous tests. It also doesn’t play super well with third-party libraries, such as assertion libraries.
After looking into only a few of the many different frameworks out there I find myself coming to the conclusion that choosing a framework is not black and white.
Most frameworks (Mocha being the exception) provide you with what you need at the end of the day, which is a testing environment along with the mechanisms to ensure that given the
X -> Y is always returned, with a few simply giving you more “bells and whistles.”
You should feel pretty confident in choosing any of them, and the choice in my mind depends what you and your particular project wants or needs.
- If you require a broad API along with specific (perhaps unique) features then Mocha would be your choice as the extensibility is there
- AVA or Tape gives you the minimum requirements. Great for providing a solid minimal foundation for you to get going fast
- If you have a large project, or would like to quickly get started without much configuration, then Jest would be a solid choice