I am currently testing the functionality of an express router using sinon. The first test in my code below passes without any issues, but I'm having trouble with the second test. It's not passing and I can't seem to figure out why.
When I send an http request to the route, it works as expected.
There seems to be some issue with the catch block causing the problem. Below is the code snippet that I have narrowed it down to, along with the error message:
books.js
import express from 'express';
import models from '../db/models';
const router = express.Router();
var indexPost = async (req, res, next) => {
try {
let savedBook = await models.Book.create({
title: req.body.title || null,
isbn: req.body.isbn || null,
author: req.body.author || null
});
res.status(201).json({ book: savedBook.id });
} catch (err) {
res.status(400).send('');
}
};
router.post('/', indexPost);
export default router;
export { indexPost };
books.test.js
import { indexPost } from '../../../src/routes/books';
import models from '../../../src/db/models';
import sinon from 'sinon';
import { expect } from 'chai';
import sinonTestFactory from 'sinon-test';
const sinonTest = sinonTestFactory(sinon);
describe('Books router', () => {
describe('indexPost', () => {
it('should save the book to the database', sinonTest(async function () {
let req = {
body: {
title: 'Book Title',
isbn: '123asera23',
author: 123
}
};
let res = {
status: status => {},
json: json => {}
};
this.stub(res, 'status').returns(res);
this.stub(res, 'json').returns(res);
indexPost(req, res);
let book = await models.Key.findById(1);
expect(book.title).to.equal('Book Title');
expect(book.isbn).to.equal('123asera23');
expect(book.author).to.equal(123);
sinon.assert.calledWith(res.status, 201);
sinon.assert.calledWith(res.json, { book: 1 });
}));
it('should throw an error if data is not all there', sinonTest(async function () {
let req = {
body: {
title: 'Book Title',
author: 123
}
};
let res = {
status: status => {},
send: send => {}
};
this.stub(res, 'status').returns(res);
this.stub(res, 'send').returns(res);
indexPost(req, res);
sinon.assert.calledWith(res.status, 400);
sinon.assert.calledWith(res.send, '');
}));
});
});
Error
1) Books router
indexPost
should throw an error if data is not all there:
AssertError: expected status to be called with arguments
at Object.fail (/var/app/node_modules/sinon/lib/sinon/assert.js:96:21)
at failAssertion (/var/app/node_modules/sinon/lib/sinon/assert.js:55:16)
at Object.assert.(anonymous function) [as calledWith] (/var/app/node_modules/sinon/lib/sinon/assert.js:80:13)
at Context.<anonymous> (tests/src/routes/books.test.js:58:20)
at Generator.next (<anonymous>)
at step (tests/src/routes/books.test.js:21:191)
at tests/src/routes/keys.test.js:21:437
at new Promise (<anonymous>)
at Context.<anonymous> (tests/src/routes/books.test.js:21:99)
at callSandboxedFn (/var/app/node_modules/sinon-test/lib/test.js:94:25)
at Context.sinonSandboxedTest (/var/app/node_modules/sinon-test/lib/test.js:114:24)