Reputation: 93
I'm using
"jest": "^29.5.0",
"jest-fetch-mock": "^3.0.3",
"jest-sonar-reporter": "^2.0.0",
Following is my jest.config.js:
module.exports = {
automock: false,
clearMocks: true,
collectCoverage: true,
coverageDirectory: "<rootDir>/Test/Reports",
coveragePathIgnorePatterns: ["/node_modules/", "/Test/Unit/"],
coverageReporters: ["text", ["lcovonly", { projectRoot: "." }]],
testEnvironment: "node",
testResultsProcessor: "<rootDir>/Test/node_modules/jest-sonar-reporter",
testEnvironmentOptions: {
browsers: ["chrome", "firefox", "safari"],
url: "http://localhost",
},
moduleNameMapper: {
"\\.(css|less|sass|scss)$": "<rootDir>/Test/__mocks__/styleMock.js",
"\\.(gif|ttf|eot|png|svg)$": "<rootDir>/Test/__mocks__/fileMock.js",
"^react$": "<rootDir>/Test/node_modules/react",
"^react-dom$": "<rootDir>/Test/node_modules/react-dom",
},
rootDir: "../",
roots: ["<rootDir>"],
setupFiles: ["<rootDir>/Test/node_modules/mock-local-storage"],
setupFilesAfterEnv: [
"<rootDir>/Test/Unit/setupJest.js",
"<rootDir>/Test/node_modules/mock-local-storage",
],
transform: {
"^.+\\.(js|jsx|mjs)$": "<rootDir>/Test/jest-transformer.js",
},
verbose: true,
watchman: true,
};
The coverage summary table and lcov.info file generated shows that new lines, comment lines, initialized variables, destructured props and non-existing lines are uncovered (example: file with 500 total lines, report says line 700-800 is uncovered).
Example: Source: A function to format an array
export default function formatArray(unformattedArray) {
const removingNewLines = unformattedArray.toString().split(",\n");
const stringArray = removingNewLines.toString().split(",");
const trimmedArray = stringArray.map((s) => s.trim());
trimmedArray.forEach((elem, i) => {
if (elem.includes("\n")) {
trimmedArray.splice(i, 1);
trimmedArray.push(elem.replace(/\n/g, ""));
}
});
const arrLen = trimmedArray.length;
if (trimmedArray[arrLen - 1] === "") {
console.log("Reached?");
trimmedArray.pop();
}
return trimmedArray;
}
Test:
import formatArray from "./formatArray";
describe("formatArray test", () => {
const expectedResp = ["http://localhost:3000/about", "http://localhost:4000/about"];
it("with single string", async () => {
const arrayResp = ["http://localhost:3000/about"];
let resp = formatArray(arrayResp);
expect(resp).toEqual(arrayResp);
});
it("with new line in string", async () => {
const arrayResp = ["http://localhost:3000/about,\nhttp://localhost:4000/about"];
let resp = formatArray(arrayResp);
expect(resp).toEqual(expectedResp);
});
it("with space in string", async () => {
const arrayResp = ["http://localhost:3000/about, http://localhost:4000/about"];
let resp = formatArray(arrayResp);
expect(resp).toEqual(expectedResp);
});
it("with comma towards end in string", async () => {
const arrayResp = ["http://localhost:3000/about,http://localhost:4000/about,"];
let resp = formatArray(arrayResp);
expect(resp).toEqual(expectedResp);
});
it("with space towards end in string", async () => {
const arrayResp = ["http://localhost:3000/about,http://localhost:4000/about "];
let resp = formatArray(arrayResp);
expect(resp).toEqual(expectedResp);
});
});
Coverage summary on running test script: "test": "node --unhandled-rejections=none node_modules/jest/bin/jest.js --updateSnapshot"
console.log
Reached?
at formatArray (Source/formatArray.js:404:13)
PASS Unit/formatArray.test.js (13.036 s)
formatArray test
√ with single string (15 ms)
√ with new line in string (1 ms)
√ with space in string (1 ms)
√ with comma towards end in string (111 ms)
√ with space towards end in string (2 ms)
----------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------------|---------|----------|---------|---------|-------------------
All files | 86.66 | 75 | 100 | 85.71 |
formatArray.js | 86.66 | 75 | 100 | 85.71 | 15-16
----------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 5 passed, 5 total
Snapshots: 0 total
Time: 13.602 s
Ran all test suites matching /formatArray.test.js/i.
Upvotes: 1
Views: 468