10-30-2019 01:41
10-30-2019 01:41
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 -- --coverageI 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 Answer10-30-2019 02:59
Fitbit Developers oversee the SDK and API forums. We're here to answer questions about Fitbit developer tools, assist with projects, and make sure your voice is heard by the development team.
10-30-2019 02:59
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 Answer10-30-2019 04:54
10-30-2019 04:54
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 Answer10-30-2019 05:20
Fitbit Developers oversee the SDK and API forums. We're here to answer questions about Fitbit developer tools, assist with projects, and make sure your voice is heard by the development team.
10-30-2019 05:20
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 Answer03-06-2020 11:24
03-06-2020 11:24
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