Brew Your First Tests with Mocha, Chai and 3½ Minutes

This post is for front-end developers who want to start writing tests. People have told them that testing their code is beneficial to their sanity. For some reason, though, they never got started — maybe because there are a lot of lenghty articles and different tools to consider. This article is different: It is a copy & paste guide to your first tests with Mocha and Chai, with a steeping time of about 3½ Minutes.

All code is guilty, until proven innocent.


Include mocha.js, mocha.css and chai.js in your tests.html. This example uses behavior-driven development, which is why Mocha is initialized with mocha.setup('bdd').

<!DOCTYPE html>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="//">

    <div id="mocha"></div>

    <script src="//"></script>
    <script src="//"></script>

        var expect = chai.expect;
        var should = chai.should();

    <script src="tests.js"></script>




Write a synchronous and an asynchronous test in your tests.js. If you are done open your tests.html in a browser and look at the test results.

function clamp(number, min, max) {
    return Math.max(min, Math.min(number, max));

function getIPAddress(callback) {
    var request = new XMLHttpRequest();'GET', '', true);

    request.onload = function() {
        if (request.status >= 200 && request.status < 400) {
        } else {
            throw request.status;

    request.onerror = function() {
        throw 'Error';


describe('Synchronous Code', function() {
    describe('#clamp()', function () {
        it('should return the number', function () {
            expect(clamp(5, 0, 10)).to.equal(5);
        it('should return max if the number is larger', function () {
            expect(clamp(20, 0, 10)).to.equal(10);
        it('should return min if the number is smaller', function () {
            expect(clamp(-20, 0, 10)).to.equal(0);

describe('Asynchronous Code', function() {
    describe('#getIPAddress()', function() {
        it('should get and parse without error', function(done) {
            getIP(function(json) {

Next Steps

Read some detailed articles about testing and use Mocha’s and Chai’s documentation. If you want to run your tests in Node.js run npm install -g mocha, write a test.js file and start mocha.

My goal with this article is to get people up and running so they can start to write some tests and see the benefits for themselves. There are already far better articles out there that can tell you the benefits and intricacies of testing so there is no point in writing another.

I also want to mention that this article was heavily inspired by Remy Sharp’s The copy & paste guide to your first Service Worker. Go read it, Service Workers may very well be the next big thing!

You can support via PayPal and buy me a 🍺 or a cup o’ joe.

Manuel Wieser, BSc MA

I’m an experienced front-end developer and lead developer for cross-functional Scrum/Kanban teams. I’ve recently transitioned to engineering management, trying to make everyone on my team an even better developer than myself.

You can contact me via GitHub, Twitter or [email protected].

I’m currently working as the Head of Front-End Development for in Linz, making software that helps people find the job of their dreams.