mocha and chai code example
Example 1: mocha js
Mocha is a JavaScript test framework!
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);
});
});
});
Example 2: what is mocha js
Mocha is javascript test framework!
Example 3: how to run mocha tests on asynchronous functions in script
// async/await
it('responds with matching records', async function() {
const users = await db.find({ type: 'User' });
users.should.have.length(3);
});
// promise.then()
it('should save without error', function(done) {
var user = new User('Luna');
user.save(function(err) {
if (err) done(err);
else done();
});
});
Example 4: chai js
Chai is an assertion library with 3 different assert styles,
namely "should", "expect" and "assert".
foo.should.equal('bar'); // Should style
expect(foo).to.equal('bar'); // Expect style
assert.equal(foo, 'bar'); // Assert style
It can be used to replace the default assertion styles of JavaScript testing frameworks.