Skip to content

Commit cfb2820

Browse files
authored
feat: support coverage (#10)
This pull request adds support for code coverage reporting in the Jest integration. You can now use the --coverage parameter when running Jest with Harness to collect data on how much of your code is covered by your test suites.
1 parent d8344f7 commit cfb2820

8 files changed

Lines changed: 27 additions & 19 deletions

File tree

apps/playground/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ module.exports = {
99
setupFilesAfterEnv: ['./src/setupFileAfterEnv.ts'],
1010
},
1111
],
12+
collectCoverageFrom: ['./src/**/*.(ts|tsx)'],
1213
};

packages/babel-preset/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
}
1616
},
1717
"dependencies": {
18-
"@babel/plugin-transform-class-static-block": "^7.27.1"
18+
"@babel/plugin-transform-class-static-block": "^7.27.1",
19+
"babel-plugin-istanbul": "^7.0.1"
1920
},
2021
"peerDependencies": {
2122
"@babel/core": "^7.22.0"

packages/babel-preset/src/preset.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import resolveWeakPlugin from './resolve-weak-plugin';
33
export const rnHarnessPlugins = [
44
'@babel/plugin-transform-class-static-block',
55
resolveWeakPlugin,
6-
];
6+
process.env.RN_HARNESS_COLLECT_COVERAGE ? 'babel-plugin-istanbul' : null,
7+
].filter((plugin): plugin is string => plugin !== null);
78

89
export const rnHarnessPreset = () => {
910
if (!process.env.RN_HARNESS) {

packages/bridge/src/shared/test-runner.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,5 @@ export type TestSuiteResult = {
8080
status: TestResultStatus;
8181
error?: SerializedError;
8282
duration: number;
83+
coverage?: unknown;
8384
};

packages/jest/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ export default class JestHarness implements CallbackTestRunnerInterface {
6666
const { config: harnessConfig } = await getConfig(projectRoot);
6767
const cliArgs = getAdditionalCliArgs();
6868
const selectedRunner = getHarnessRunner(harnessConfig, cliArgs);
69+
70+
if (this.#globalConfig.collectCoverage) {
71+
// This is going to be used by @react-native-harness/babel-preset
72+
// to enable instrumentation of test files.
73+
process.env.RN_HARNESS_COLLECT_COVERAGE = 'true';
74+
}
75+
6976
const harness = await getHarness(selectedRunner);
7077

7178
try {

packages/jest/src/run.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,6 @@ export const runHarnessTestFile: RunHarnessTestFile = async ({
111111
errorMessage,
112112
tests,
113113
jestTestPath: testPath,
114+
coverage: results.coverage as JestTestResult['coverage'],
114115
});
115116
};

packages/runtime/src/runner/factory.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@ export const getTestRunner = (): TestRunner => {
99
return {
1010
events,
1111
run: async (testSuite, testFilePath) => {
12-
return runSuite(testSuite, {
12+
const result = await runSuite(testSuite, {
1313
events,
1414
testFilePath,
1515
});
16+
17+
// If coverage is enabled, there will be a global variable called __coverage__
18+
if ('__coverage__' in global && !!global.__coverage__) {
19+
result.coverage = global.__coverage__;
20+
}
21+
22+
return result;
1623
},
1724
dispose: () => {
1825
events.clearAllListeners();

pnpm-lock.yaml

Lines changed: 5 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)