paint-brush
Introduction to Mocha Testing Framework by@kshirish
262 reads

Introduction to Mocha Testing Framework

by kJune 12th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

npm install --save-dev mocha.const assert = require('assert');.require('assert') describe('#indexOf()', function() {.describe('Array')); describe('SetTimeOut', function('why everyone is scared of me')); // here timeout might give error, since default timeout is 2 sec. // flag --timeout 13000 or --delay 13,000 or this.timeout(13000) // programatically - this. timeout(13,000) is set to 13,500.
featured image - Introduction to Mocha Testing Framework
k HackerNoon profile picture
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser.

Getting started

npm install --save-dev mocha

Let's create a file named

test.js
.

const assert = require('assert');

describe('Array', function() {
  describe('#indexOf()', function() {
    it('should return -1 when the value is not present', function() {
      assert.equal([1, 2, 3].indexOf(4), -1);
    });
  });
});

Baby steps!

describe('Array', function() {  // suite

        describe('#indexOf()', function() { // suite

            it('should not return -1 when the value is present', function() {   // test
                assert.notEqual(-1, [1, 2, 3].indexOf(3));
            });

            it('should return -1 when the value is not present', function() {   // test
                assert.equal(-1, [1, 2, 3].indexOf(5));
                assert.equal(-1, [1, 2, 3].indexOf(0));
            });

        });
    });

Diving into async storm!

// it uses `done`, just call it after you think your async will be finished
describe('SetTimeOut', function() {

    it('why everyone is scared of me', function(done) {
        setTimeout(function() {
            assert.equal(1, 1);
            done();
        }, 3000);   
        // here timeout might give error, since the default timeout is 2 sec
        // though good news is you can alter it yourself using
        // flag --timeout 13000 or
        // programatically - this.timeout(13000)
    });
});

Delay the root suite!

setTimeout(function() {

        describe('master of all suites', function() {
            // write your cases here ..    
        });

        run();
        // again use flag --delay to use `run` function.
    }, 3000);

    console.log('Timer begins for 3 seconds atleast');

Hooks

describe('mocha hooks', function() {

    before('description here', function(){
        // runs before all test in this block
    });

    after('description here', function(){
        // runs after all test in this block
    });

    beforeEach('description here', function(){
        // runs before each test in this block
    });

    afterEach('description here', function(){
        // runs after each test in this block
    });

    // your test cases here ..
});

Juggling async Promises

// using chai-as-promised
// bad
doSomethingAsync().then(function(result) {
    result.should.equal('foo')
    done();
}, function(err) {
    done(err);
});

// good
return doSomethingAsync().should.eventually.equal('foo');

describe('Promises', function() {

    it('deal with promises', function() {
        // (2+2).should.equal(4)
        // or
        return Promise.resolve(2 + 2).should.eventually.equal(4);
    });
});

Dynamically generating test suites

// adds only two numbers
function add() {
    return arguments[0] + arguments[1];
}

describe('add()', function() {
    var tests = [{
        args: [1, 21],
        expected: 22
    }, {
        args: [11, 3],
        expected: 14
    }, {
        args: [3, 24],
        expected: 27
    }];

    tests.forEach(function(test) {
        it('correctly adds ' + test.args.length + ' args', function() {
            var res = add.apply(null, test.args);
            assert.equal(res, test.expected);
        });
    });
});

Checkout more here.