Project example using Mocha, chai to do test driven development for Node.js.
Install Mocha globally by running:
$ npm install -g mocha
Install mongodb and run at port 27017
During filling npm init, input mocha for "test command", Your package.json file should contain the following field:
"script": {
"test": "mocha"
}
Use supertest to test api from express
describe('Feed API', () => {
describe('Get Image list', () => {
it('should return images of link in array', (done) => {
api.get('/images')
.expect(200)
.end((err, res) => {
if (err) done(err);
expect(res.body).to.have.property('images');
expect(res.body.images).to.be.an('array');
done();
});
})
});
});
Test logging with sandbox
describe('Sanbox Winston Log', () => {
describe('Log an info level message in sandbox', () => {
beforeEach(() => {
this.sandbox = sinon.createSandbox();
});
it('should logged "hello world"', () => {
sinon.stub(logger, 'info');
logger.info('hello world');
expect(logger.info).to.have.been.calledWith('hello world');
});
afterEach(() => {
this.sandbox.restore()
});
})
});
describe('User Controller', () => {
describe('Create a new user', () => {
before(() => {
db.connect(()=> {
});
});
it('should return saved document with property "name"', () => {
let tom = new User({
name: "Tom"
});
return expect(userController.create(tom)).to.eventually.has.property('name');
});
})
});