I'm currently working on a project where I'm using Jest for unit testing and code coverage.
Everything is working fine, except coverage for mocked classes/methods. I don't seem to get the desired coverage results. I've tried to find something in the Jest docs and searched online for an answer, but I can't seem to find anything about it.
The thing is that when I use a mocked implementation (for example ./services/__mocks__/UserService.js
), the actual implementation (./services/UserService.js
) results in having 0% coverage. This is a logical outcome, since the implementation is overwritten by the mock.
I can get around this by using /* istanbul ignore next */
on every method in the actual service or simply add the actual services to the coveragePathIgnorePatterns
property in the Jest setup file and let it generate coverage for all mocked classes instead, but I wonder if there is any way to have Jest use the mocked implementation automatically for generating coverage results.
What is the way to go for mocked classes/functions and code coverage?
Thanks in advance!