1

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);
            });
        });
      });

1 Answers1

0

the solution is to insert an

/* istanbul ignore else */

as following

.catch(
  (e) => {
    /* istanbul ignore else */
    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 {
      next(e);
    }
  })

then the code coverage output is 100%

| % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines | -|----------|----------|----------|----------|----------------| | 100 | 100 | 100 | 100 | 100 |

  • 1
    You should actually test the else, don't just ignore it. Inject an error that is not `ValidationError` and see if executing the else block work. – slebetman Jun 24 '17 at 17:46
  • yes.. I should .. but any other error would be related to a save() document in MongoDB and I don't know what and how to write a test for it.... any link ? –  Jun 26 '17 at 06:29
  • @erwin I came across this SO question as I face the same issue incorrect code coverage being reported by `istanbul`. Please let me know if you have found some solution. I agree with @slebetman: you should not ignore for the sake of coverage. If you are using `sinon` for stubbing/spying/mocking you can look at [sinon-mongoose](https://github.com/underscopeio/sinon-mongoose) under `examples/`. That should give you some idea to do what @slebetman is recommending – Vikram Aug 12 '17 at 15:11