• Comparing Jasmine, Mocha, AVA, Tape and Jest
  • Ben Harding
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: ClarenceC
  • Proofreader: Dearpork, Xekin-fe

JavaScript unit testing frameworks: Comparison of Jasmine, Mocha, AVA, Tape and Jest

When starting a new front-end project, I often ask myself two questions: “Which JavaScript unit testing framework should I use?” “And” Should I take the time to add test code?

My colleagues often write about how unit testing can calm the mind and reduce software bugs. So I always take the time to test. But which framework should you use for testing in your project? Before jumping into a decision, I’ve rounded up five of the most popular JavaScript unit testing frameworks and let you decide which one is right for you.

Effective unit testing is part of reducing errors in your software. There are also a few JavaScript debugging tips to make your unit tests more robust.

Note: If you already have a preferred testing framework and it doesn’t appear in the list below, let me know in the comments and I’ll add it to the post.

JavaScript unit testing frameworks: Comparison

Jasmine

One of the most popular Javascript unit testing frameworks, Jasmine provides everything you need right out of the box.

  • Jasmine comes with assertions, Spies (to simulate the execution environment of functions), and mocks (mock tools), perfectly equipped with almost everything you’ll need to start writing unit tests. Jasmine is easy to initialize and you can still add some libraries if you need some unit functionality.
  • The global nature makes it easier to start testing immediately in your application. While I don’t like the global aspect, there’s not a lot of inconsistency with Jasmine giving developers everything they need right out of the box
  • I find that the standalone version makes it easier to understand how everything is set up, and allows you to start using it right away
  • Today it can be used in combination with Angular 1 or Angular 2 or more popular libraries

What I think about Jasmine

I’m not a big fan of possessive globals, so Jasmine’s going to lose points on my little book. On the other hand, it has a lot of nice out-of-the-box features. It will look a little “older” than the other frameworks on this list, but that’s not a bad thing, the pain points that other frameworks may encounter mean they should be easier to fix.

AVA

A minimalist testing library, AVA’s strengths are the asynchronous nature of JavaScript and running tests concurrently, which in turn improves performance.

  • AVA does not create a global environment for you, so you can more easily control what you use. I think it would bring extra clarity to the test to make sure you know what’s going on, right
  • Taking advantage of the asynchronous nature of JavaScript provides additional benefits for testing. The main benefit is optimized wait time in deployment
  • Keep the simple API to give you the functionality you need. Mocking is more user-friendly when you use it, but you must install a separate library
  • Jest-snapshot provides a great snapshot test for when you want to know when your application UI has changed beyond your expectations

What I think about AVA

Ava’s “most perceptive” approach to minimalism and the fact that he doesn’t own the global environment earned him high marks on my little book. Simple apis make testing clearer. The AVA test library is definitely something you should try when you choose a JavaScript unit test framework.

Tape

This is the smallest frame on this list, Tape is the most straight to the point, providing the most basic functions.

  • Like AVA, Tape does not provide global environments, but instead requires you to import them yourself. That’s fine as long as it doesn’t pollute the overall environment
  • Tape does not include install/uninstall methods. Instead, it opts for a multi-module system where you need to explicitly define the installation code in each test to make each test clearer. It also prevents state from being shared between tests
  • Support the Typescript/coffeescript/es6
  • Easy and quick to set up and run, Tape is a JavaScript file that you can run in any environment where JavaScript can be run, without too many configuration options

My opinion on Tape

Tape prides itself on having a lower-level, less featureless API than AVA. Tape makes everything easy and only gives you what you need. That’s why Tape has a high score on my little book and is one of the best JavaScript unit testing frameworks out there, allowing you to focus on product rather than tool selection.

Mocha

Mocha logo

Arguably the most used library, Mocha is a flexible library that provides developers with only a basic testing structure. Then other functional assertions, spies, mocks, and others like them require references to add other libraries/plugins to complete.

  • If you want more flexibility in configuration and importing your specific library needs, then Mocha’s additional installation and required configuration is a must look
  • Unfortunately, there is a real problem with the above argument that you must import additional libraries to implement assertions. This does mean it’s a little harder to set up than others, if not for a long time. They say that Settings are usually a one-time thing, but I prefer to do a “single source fact” (document) instead of jumping from document to document
  • Mocha imports test structures as global variables, saving you the time you no longer needincludeOr request that it be in every file. The downside is that those plugins still require you to use them anywayrequireImport it in. This leads to inconsistencies, and if you’re an OCD like me, it will eventually drive you crazy!

What I think of Mocha

I was impressed by the scalability and the number of different configurations of Mocha. Having to go to Mocha and then having to go to your assertion of choice really freaked me out. Flexibility lies in the high benefits of assertions, spies and Mocks.

Jest

Featured and used by Facebook and various React apps, Jest is well supported. Jest was also found to be a very fast test library in parallel test reports.

  • For small projects you probably don’t have to worry too much at first, but the performance boost is great for larger applications that want to be deployed continuously throughout the day
  • While developers primarily use Jest to test React applications, Jest can be easily integrated into other applications, allowing you to use more unique features elsewhere
  • Snapshot testing is a great tool to ensure that your application UI doesn’t have unexpected errors during product release and replacement. Although most features are specifically designed to work with React, it can work with other frameworks if you can find the right plugin
  • Unlike the other libraries on this list, Jest has an extensive API, so unless you really need some additional functionality, there’s no need to import additional libraries and Jest continues to improve functionality dramatically with each update

What I think about Jest

While global variables are a disadvantage, Jest is an increasingly powerful library. It has a lot of easy-to-understand documentation to help with learning, and supports a variety of different environments that look great when building projects.

Which JavaScript unit testing framework should I choose?

After I looked at a few different frameworks, I came to the conclusion that not all frameworks are black and white.

Most frameworks will eventually (with the exception of Mocha) give you what you need at the end of the day, it’s a testing environment and a mechanism to ensure that given X -> Y always gets returned, and a few will simply give you more “bells and whistles.”

You should be confident in yourself when choosing them, and my choice depends on what you want and need for your particular project.

  • If you require a broad API and specific (possibly unique) functionality then Mocha might be the choice because the extensibility is there
  • AVA or Tape will give you minimum environmental requirements. It’s great to provide you with a solid, basic environment that allows you to test quickly
  • If you have a big project or want to get started quickly and don’t need much configuration, Jest is a great choice

I hope this will be helpful when you choose your JavaScript unit testing framework. If you want me to take a look at other JavaScript unit test boxes, let me know in the comments! I’ll add them to the list later.


The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.