These frameworks/libraries are node plugins
-
Chai -- BDD/TDD assertion library
-
Mocha -- Mocha is a feature-rich JavaScript test framework
-
Sinon -- Standalone test spies, stubs and mocks for JavaScript. No dependencies, works with any unit testing framework.
-
Sinon-chai -- Sinon–Chai provides a set of custom assertions for using the Sinon.JS spy, stub, and mocking framework with the Chai assertion library.
Chai has 3 , keywords should , expect , assert , types of tests.
See examples in './examples/chai'
, run node should.js
Mocha is the test runner runs Chai and some other tests, which has keywords and hooks.
The "hooks" are: before(), after(), beforeEach(), afterEach()
Mocha can also run tests asynchronously:
-
Just add callback function to it() and usually named the function done.
-
Just return a promise after the test runs
Add only to describe or it, i.e. describe.only() it.only() to explicitly
state which test to run. Or use word skip describe.skip() it.skip() to
explicitly state which test to omit.
There are 3 type of testing interfaces
-
BDD: interface provides : describe(), it(), before(), after(), beforeEach(), and afterEach().
describe('Array', function(){ before(function(){ // ... }); describe('#indexOf()', function(){ it('should return -1 when not present', function(){ [1,2,3].indexOf(4).should.equal(-1); }); }); }); -
TDD: interface provides : suite(), test(), setup(), and teardown().
suite('Array', function(){ setup(function(){ // ... }); suite('#indexOf()', function(){ test('should return -1 when not present', function(){ assert.equal(-1, [1,2,3].indexOf(4)); }); }); }); -
Exports: keys before, after, beforeEach, and afterEach are special-cased, object values are suites, and function values are test-cases. We don't really use it, so we can ignore it for now.
module.exports = { before: function(){ // ... }, 'Array': { '#indexOf()': { 'should return -1 when not present': function(){ [1,2,3].indexOf(4).should.equal(-1); } } } };
See a simple example in './examples/mocha', run mocha test.js
Go read the docs if you want to know more.
Sinon is like python Mock module.
Oh, if you don't know what Mock is, please read this doc
There are so many things Sinon can mock of, for example:
-
Spies, are functions that monitors the target function. Creates an anonymous function that records arguments, this value, exceptions and return values for all calls.
-
Stubs, are functions (spies) with pre-programmed behavior.
-
Mocks, Mocks (and mock expectations) are fake methods (like spies) with pre-programmed behavior (like stubs) as well as pre-programmed expectations . A mock will fail your test if it is not used as expected.
-
Fake timers, is a synchronous implementation of setTimeout and friends that Sinon.JS can overwrite the global functions with to allow you to more easily test code using them.
More : Fake timers, Fake XMLHttpRequest, Fake JSON-P, Assertions
Sinon-chai (Sinon.JS Assertions for Chai) is a plugin for Sinon.
It provides a set of custom assertions for using the Sinon.JS spy, stub, and mocking framework with the Chai assertion library.
Currently the tests use the Mocha BDD interface. Notes about installing and running the tests in DV2 can be found at the bottom of the ui readme https://github.com/synappio/DataValidation/blob/master/ui/README.md