Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing and code coverage with JEST #446

Closed
otroboe opened this issue Feb 20, 2018 · 4 comments
Closed

Testing and code coverage with JEST #446

otroboe opened this issue Feb 20, 2018 · 4 comments

Comments

@otroboe
Copy link

otroboe commented Feb 20, 2018

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
@otroboe
Copy link
Author

otroboe commented Feb 20, 2018

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.

@shekohex
Copy link
Contributor

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 😄

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.

@otroboe otroboe closed this as completed Feb 20, 2018
@otroboe
Copy link
Author

otroboe commented Feb 22, 2018

FYI, since Jest 22.4.0, the option mapCoverage is deprecated !

https://github.com/facebook/jest/blob/master/CHANGELOG.md#2240
jestjs/jest#5177

@lock
Copy link

lock bot commented Sep 25, 2019

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.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants