Overview
Common API used for an automated testing:
-
sinon
(sinonjs.com) for spying/stubbing/mocking. -
FakeTimers
for timers manipulation, docs -
chai
(chaijs.com) for asserting withexpect
API enouraged and some plugins:-
chai-as-promised
- Promises support, docs. -
chai-datetime
- Dates support, docs. -
chai-integer
- assirting on Integer type, docs. -
chai-url
- URLs support, docs. -
sinon-chai
- integration withsinon
, docs. -
chai-string
- various handly String assertions, docs. -
chai-dom
- DOM support, docs. -
chai-like
- Partial objects comparison, docs.
-
Usage example
const { expect, sinon, FakeTimers } from '@majus/testing';
describe('My tests', () => {
let clock;
beforeEach(() => {
sinon.stub(console, 'log');
clock = FakeTimers.install();
});
afterEach(() => {
clock.uninstall();
sinon.restore();
});
it('my test for integer', () => {
const result = myIntFunc();
expect(result).to.be.an('integer').and.to.be.least(0);
});
it('my test for promise', async () => {
const result = myAsyncFunc();
await expect(result).eventually.to.be.fulfilled;
});
it('my test for date', () => {
const result = myDateFunc();
expect(result).to.be.a('date').and.beforeDate(new Date());
});
it('my test for date', () => {
const result = myStringFunc();
expect(result).to.be.a('string').and.startWith('[');
});
it('my test for objects', () => {
const result = myObjectFunc();
expect(result).to.be.an('object').and.be.like({ a: 1 });
});
it('my test for arrays', () => {
const result = myArrayFunc();
expect(result).to.be.an('array').and.be.like([{ a: 1 }, { a: 2 }]);
});
it('my test for sinon', () => {
myComplexFunc();
expect(console.log).not.to.be.called;
});
it('my test for timers', () => {
let flag = false;
setTimeout(() => {
flag = true;
}, 10000);
clock.tick(10000);
expect(flag).to.be.true;
});
});