I don't understand how the test coverage is processing this .catch() branch :
newUser.save()
.then((savedUser) => {
res.json(savedUser);
})
.catch(
(e) => {
if (e.name === 'ValidationError' && e.errors.username.kind === 'unique') {
res.status(409);
res.json({ error: { message: 'Existing user', field: e.errors.username.path, value: e.errors.username.value } });
} else {
/* istanbul ignore next */
next(e);
}
});
I am testing correctly the first block:
if (e.name === 'ValidationError' && e.errors.username.kind === 'unique') {...)
and I don't have a test for the second else block, so I added an ignore next statement :
else { /* istanbul ignore next */ next(e); }
the code code coverage reports the line 58. ( if..) as uncovered..
| % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
-|----------|----------|----------|----------|----------------|
| 100 | 93.75 | 100 | 100 | 58 |
but the following test is executing correctly, covering this line ...
describe('# POST /api/v1/users', () => {
it('should NOT create a duplicated user', () => {
return request(app)
.post('/api/v1/users')
.set('Authorization', superAdminJwtToken)
.send(newUser)
.expect(httpStatus.CONFLICT)
.then((res) => {
expect(res.body.error.message).to.equal('Existing user');
expect(res.body.error.field).to.equal('username');
expect(res.body.error.value).to.equal(newUser.username);
});
});
});