Skip to content

Commit

Permalink
fix: coverage from sub folders
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Mar 21, 2021
1 parent ca9daf3 commit b9098fa
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 57 deletions.
20 changes: 14 additions & 6 deletions core/jest-testing/src/run-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path';
import { runCLI } from 'jest';
import { Config } from '@jest/types';
import { AssertionResult, AggregatedResult } from '@jest/test-result';
import { FileCoverage, CoverageSummary } from 'istanbul-lib-coverage';
import { FileCoverage, CoverageSummaryData } from 'istanbul-lib-coverage';

export const getUniqueFolder = (folder: string): string => {
let fnId = 1;
Expand All @@ -23,21 +23,22 @@ export interface JestResults {
/**
* coverage summary data, by file
*/
coverage: Record<string, CoverageSummary>;
coverage: Record<string, CoverageSummaryData>;
}
export const run = async (
testFilePath: string,
jestConfig?: Partial<Config.Argv>,
jestConfig: Partial<Config.Argv> = {},
): Promise<JestResults | undefined> => {
const testFolder = path.dirname(testFilePath);
const testFile = path.basename(testFilePath);
const configFolder = getUniqueFolder(testFolder);
const configFile = path.resolve(configFolder, 'jest.config.js');
const { rootDir = '..', ...config } = jestConfig;
fs.writeFileSync(
configFile,
`module.exports = ${JSON.stringify(
{
rootDir: jestConfig?.rootDir || '..',
rootDir,
preset: 'ts-jest',
transform: {
'^.+\\.(ts|tsx)?$': 'ts-jest',
Expand Down Expand Up @@ -69,7 +70,7 @@ export const run = async (
coverage: true,
coverageReporters: ['none'],
watchman: false,
...jestConfig,
...config,
} as Config.Argv,
[configFolder],
);
Expand All @@ -91,7 +92,14 @@ export const run = async (
if (cov) {
Object.keys(cov.data).forEach(file => {
const fc = cov.data[file] as FileCoverage;
result.coverage[path.relative(testFolder, file)] = fc.toSummary();
const summary = fc.toSummary().toJSON();
const totals = Object.values(summary).reduce(
(total, value) => total + value.covered + value.skipped,
0,
);
if (totals) {
result.coverage[path.relative(testFolder, file)] = summary;
}
});
}

Expand Down
29 changes: 28 additions & 1 deletion core/jest-testing/test/tests/run-related/react-link.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,33 @@ describe('react link related tests', () => {
const result = results.find(
({ testFileName }) => testFileName === '__tests__/some_more_tests.js',
);
expect(result?.coverage).toMatchObject({});
expect(result?.coverage).toMatchObject({
'../Link.react.js': {
lines: {
total: 8,
covered: 6,
skipped: 0,
pct: 75,
},
functions: {
total: 4,
covered: 2,
skipped: 0,
pct: 50,
},
statements: {
total: 8,
covered: 6,
skipped: 0,
pct: 75,
},
branches: {
total: 2,
covered: 1,
skipped: 0,
pct: 50,
},
},
});
});
});
48 changes: 23 additions & 25 deletions core/jest-testing/test/tests/run/react-component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,29 @@ describe('react link component', () => {
it('coverage ', () => {
expect(results?.coverage).toMatchObject({
'Link.react.js': {
data: {
lines: {
total: 8,
covered: 8,
skipped: 0,
pct: 100,
},
functions: {
total: 4,
covered: 4,
skipped: 0,
pct: 100,
},
statements: {
total: 8,
covered: 8,
skipped: 0,
pct: 100,
},
branches: {
total: 2,
covered: 1,
skipped: 0,
pct: 50,
},
lines: {
total: 8,
covered: 8,
skipped: 0,
pct: 100,
},
functions: {
total: 4,
covered: 4,
skipped: 0,
pct: 100,
},
statements: {
total: 8,
covered: 8,
skipped: 0,
pct: 100,
},
branches: {
total: 2,
covered: 1,
skipped: 0,
pct: 50,
},
},
});
Expand Down
48 changes: 23 additions & 25 deletions core/jest-testing/test/tests/run/small.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,29 @@ describe('small test', () => {
it('coverage ', () => {
expect(results?.coverage).toMatchObject({
'sum.js': {
data: {
lines: {
total: 1,
covered: 1,
skipped: 0,
pct: 100,
},
functions: {
total: 1,
covered: 1,
skipped: 0,
pct: 100,
},
statements: {
total: 2,
covered: 2,
skipped: 0,
pct: 100,
},
branches: {
total: 0,
covered: 0,
skipped: 0,
pct: 100,
},
lines: {
total: 1,
covered: 1,
skipped: 0,
pct: 100,
},
functions: {
total: 1,
covered: 1,
skipped: 0,
pct: 100,
},
statements: {
total: 2,
covered: 2,
skipped: 0,
pct: 100,
},
branches: {
total: 0,
covered: 0,
skipped: 0,
pct: 100,
},
},
});
Expand Down

0 comments on commit b9098fa

Please sign in to comment.