Nest: Testing and code coverage with JEST

Created on 20 Feb 2018  路  4Comments  路  Source: nestjs/nest

I'm submitting a...

[ ] Regression <!--(a behavior that used to work and stopped working in a new release)-->
[X] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request

Current behavior

I have weird coverage reports when I'm using Jest. So I took the 01-cats-app to check if it was not from my own project, but I see the same bugs.

Expected behavior

I see some else path not taken on import ... lines, some methods are not tested but are "covered" anyway...
I should have the proper coverage, based on what I'm testing.

Minimal reproduction of the problem with instructions

I took the 01-cats-app here https://github.com/nestjs/nest/tree/master/examples/01-cats-app

npm install
npm run test:coverage

Opening the html report of the controller from

./coverage/lcov-report/src/cats/cats.controller.ts.html

cats-coverage

The only test is:

  describe('findAll', () => {
    it('should return an array of cats', async () => {
      const result = ['test'];
      jest.spyOn(catsService, 'findAll').mockImplementation(() => result);

      expect(await catsController.findAll()).toBe(result);
    });
  });

Environment

- Nest version: 4.6.3
- Node version: 8.9.4
- Platform: Linux

Most helpful comment

All 4 comments

I found a (the ?) fix for my issue. I have to add "mapCoverage": true in the jest.json config file.

More infos here:
https://github.com/kulshekhar/ts-jest#coverage
https://medium.com/@mtiller/debugging-with-typescript-jest-ts-jest-and-visual-studio-code-ef9ca8644132

I'm waiting for another opinion, to close this issue, because if it's the right fix, maybe we should to a PR on the cats example.

Hi @otroboe
This was a problem with Jest and Typescript, I found this solution when i faced this problem later.
and yes, this this definitely the correct solution.

It always about wrong code maps :smile:

anyway, you are free to make a PR to all other examples.
another TIP: these kind of issues should be on stackoverflow not here
Thanks.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rlesniak picture rlesniak  路  3Comments

mishelashala picture mishelashala  路  3Comments

tronginc picture tronginc  路  3Comments

VRspace4 picture VRspace4  路  3Comments

cdiaz picture cdiaz  路  3Comments