Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Jest is not able to find Fitbit packages while calculating code coverage

I have a Clock Face project written in Typescript, tested with Jest and built with NPM. 

By default Jest calculates coverage only for files that have tests. In my case, I have business logic separated from index.ts and covered with tests. However index.ts is not tested and I want it to reflect in the coverage report (right now it shows 100% coverage which is not true). For that reason I added the following configuration to Jest so that it includes all my ts files to the report (including index.ts):

"collectCoverageFrom" : ["app/**/*.ts"],

But now if I run the coverage report:

npm test -- --coverage

 I get the following errors:

app/index.ts:1:32 - error TS2307: Cannot find module 'clock'.
1 import clock, {TickEvent} from "clock";
~~~~~~~
app/index.ts:2:22 - error TS2307: Cannot find module 'document'.
2 import document from "document";
~~~~~~~~~~
app/index.ts:3:27 - error TS2307: Cannot find module 'user-settings'.
3 import {preferences} from "user-settings";

These are the Fitbit APIs and they are somehow not found by Jest.


Question: How to enable code coverage for files that use Fitbit APIs?

Best Answer
0 Votes
4 REPLIES 4

These are external modules that Jest doesn't know about. You can provide fake implementations of them for your tests via Jest's moduleNameMapper option in the Jest config.

Best Answer
0 Votes

Thank you for your answer. Could you please elaborate a bit. I am unfortunately inexperienced in JS/TS and the documentation didn't help much.

Best Answer
0 Votes

Something like this:

module.exports = {
  preset: 'ts-jest/presets/js-with-ts',
  moduleNameMapper: {
    document: '<rootDir>/test/document.js',
  },
};

document.js then has whatever methods/variables you use exported for use in tests.

 
Best Answer
0 Votes

As @LiamFitbit said, you need to mock those modules.

I did that here: https://github.com/SergioMorchon/fitbit-views/tree/master/__mocks__

 

You can look at jest manual mocks documentation: https://jestjs.io/docs/en/manual-mocks 

Best Answer
0 Votes