Skip to content

Latest commit

 

History

History
143 lines (84 loc) · 1.72 KB

README.md

File metadata and controls

143 lines (84 loc) · 1.72 KB

The Node.js & Javascript Course

Table of Contents

  • Testing
    • Mocha
      • Assertions
      • Async Await
      • Hooks
    • Chai
    • Chai HTTP

Testing

Mocha

Execute this command to install mocha

$ npm install mocha
$ mkdir test

Create test.js inside test directory and write this code :

var 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);
    });
  });
});

Back in the terminal:

$ ./node_modules/mocha/bin/mocha

  Array
    #indexOf()
      ✓ should return -1 when the value is not present


  1 passing (9ms)

Set up a test script in package.json:

"scripts": {
  "test": "mocha"
}

Then run tests with:

$ npm test

Assertions

For assertions we will using chai.js


Async Await

Here is an example using Async Await :

beforeEach(async function () {
  await db.clear();
  await db.save([tobi, loki, jane]);
});

describe('#find()', function () {
  it('responds with matching records', async function () {
    const users = await db.find({type: 'User'});
    users.should.have.length(3);
  });
});

Hooks

For helping the development mocha provide hooks :

describe('hooks', function () {
  before(function () {
    // runs once before the first test in this block
  });

  after(function () {
    // runs once after the last test in this block
  });

  beforeEach(function () {
    // runs before each test in this block
  });

  afterEach(function () {
    // runs after each test in this block
  });

  // test cases
});