nodejs mocha code example
Example 1: nodejs mocha mock
const chai = require("chai");
const expect = chai.expect;
const sinon = require("sinon");
const indexPage = require("../../controllers/app.controller.js");
describe("AppController", function() {
describe("getIndexPage", function() {
it("should send hey when user is logged in", function() {
let user = {
isLoggedIn: function(){}
}
const isLoggedInStub = sinon.stub(user, "isLoggedIn").returns(true);
let req = {
user: user
}
let res = {
send: function(){}
}
const mock = sinon.mock(res);
mock.expects("send").once().withExactArgs("Hey");
indexPage.getIndexPage(req, res);
expect(isLoggedInStub.calledOnce).to.be.true;
mock.verify();
});
});
});
Example 2: 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 3: what is mocha js
Mocha is javascript test framework!