Oh the Testing You Can Do!

A Brief Intro to Testing with Javascript

What’s the difference between Mocha and Chai? How about Mocha and Jasmine? Where does Instanbul come into play? The answer to these questions become pretty apparent if we look at the way writing tests are structured.

Let’s begin with testing frameworks. Frameworks are used to organize and execute tests. They allow for test runners, assertion libraries, and testing plugins to hook into them. Mocha and Jasmine are two of the most popular testing frameworks for Javascript and they are actually quite similar to each other. Both include assertion libraries, take a similar approach to asynchronous testing, and allow for other testing technologies to easily hook into their testing structure.

Assertion libraries are the tools that provide verification for your tests; they determine whether things are correct or not. Chai.js is a very popular assertion library in that it can be used with any testing framework and supports BDD/TDD styles of testing syntax. Interestingly, many testing frameworks provide built-in assertions which can get developers tripped-up at first. Jest is an example in that it’s a testing framework for React front-end code and it provides built-in describe assertions.

Sometimes, the code we write can be particularly difficult to test, especially when utilizing new technologies. Testing plugins are tools that hook into testing frameworks to extend their functionality. A great example of a plugin is Enzyme, which makes it easier to assert, manipulate, and traverse React components. There are also tools that provide code coverage. Istanbul tests all lines of code in your codebase and works with all the most popular Javascript test frameworks.

Testing your code can be annoying, frustrating, or seemingly unhelpful if you do not take the time to take deep dives into documentation. Hopefully, this article gives you a brief understanding of the overview of testing in Javascript. Testing is fun, helpful, and essential, and you’ll be better at writing tests if you know about the wider world of testing.

Like what you read? Give Michael Martinez a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.