Mocha, Chai, Sinon
- Mocha: the core framework that provides standard testing functions, including
describe
and it
.
- It doesn't come bundled with assertion functions or spies like Chai and Sinon, so it can be combined with them to provide a complete testing environment.
- Chai: a library with many assertions. It provides a variety of assertion styles, including "should", "expect", and "assert".
- Chai allows you to write expressive and readable assertions in your test cases, making it easier to understand the expected behavior of your code.
- Sinon: a standalone library used for creating test doubles such as spies, stubs, and mocks.
- It's useful for simulating behavior, like spying on function calls, stubbing dependencies, and mocking HTTP requests.
- Jest: Jest is a comprehensive and all-in-one testing framework developed by Facebook.
- It's designed to be easy to set up and use, and it includes built-in functionalities like assertion, mocking, and code coverage analysis.
- Jest has a powerful mocking library similar to Sinon, eliminating the need for separate mocking libraries.
- It can run tests in parallel, speeding up the testing process significantly.
- Enzyme: Primarily used for testing React components and maintained by Airbnb.
- Enzyme is not a testing framework like Mocha, Jest, or Chai but a testing utility that complements these frameworks.
// This example is using node.js built-in assertion module: <https://nodejs.org/api/assert.html>
mocha.setup('bdd');
describe('pow', function() {
beforeEach(() => {
/* Setup Instruction. */
});
it("raises to n-th power", () => {
assert.equal(pow(2, 3), 8);
});
});
describe("test", function() {
before(() => console.log("Testing started – before all tests"));
after(() => console.log("Testing finished – after all tests"));
beforeEach(() => console.log("Before a test – enter a test"));
afterEach(() => console.log("After a test – exit a test"));
it('test 1', () => console.log(1));
it('test 2', () => console.log(2));
});
// Async
it('should save', function(done) {
var user = new User();
user.save(function(err) {
if (err) {
throw err;
}
done();
});
});
chai.should();
foo.should.be.a('string');
foo.should.equal('bar');
foo.should.have.length(3);
tea.should.have.property('flavors').with.length(3);
const assert = chai.assert;
assert.typeOf(foo, 'string');
assert.equal(foo, 'bar');
assert.lengthOf(foo, 3)
assert.property(tea, 'flavors');
assert.lengthOf(tea.flavors, 3);
const expect = chai.expect;
expect(foo).to.be.a('string');
expect(foo).to.equal('bar');
expect(foo).to.have.lengthOf(3);
expect(tea).to.have.property('flavors').with.lengthOf(3);
- The following function takes a function as its argument and returns a new function.
- You can call the resulting function as many times as you want, but the original function will only be called once:
const once = (fn) => {
var returnValue,
called = false;
return () => {
if (!called) {
called = true;
returnValue = fn.apply(this, arguments);
}
return returnValue;
};
}
- Testing this function can be quite elegantly achieved with a test fake:
it("calls the original function", function () {
const callback = sinon.fake();
const proxy = once(callback);
// The fact that the function was only called once is important:
proxy();
proxy();
assert(callback.called);
assert(callback.calledOnce);
// ...or:
// assert.equals(callback.callCount, 1);
});
- We also care about the
this
value and arguments:
it("calls original function with right this and args", function () {
const callback = sinon.fake();
const proxy = once(callback);
const obj = {};
proxy.call(obj, 1, 2, 3);
assert(callback.calledOn(obj));
assert(callback.calledWith(1, 2, 3));
});
Behavior