Skip to content

Commit eff4872

Browse files
andrewdacenkofacebook-github-bot
authored andcommitted
Add support for JS coverage (facebook#53410)
Summary: Pull Request resolved: facebook#53410 Changelog: [Internal] Adding babel-istanbul-plugin to instrument bundle code with coverage reporting. Metro will transform source code only when coverage flag is set up globally in jest. Coverage map is then provided by runner as part of test result. Differential Revision: D80716433
1 parent 5e9b565 commit eff4872

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

private/react-native-fantom/config/metro-babel-transformer.flow.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ const transform: BabelTransformer['transform'] = (
2626
...(args.plugins ?? []),
2727
// $FlowExpectedError[untyped-import]
2828
require('./babel-plugins/inject-debugger-statements-in-tests'),
29+
...(args.options.customTransformOptions?.collectCoverage != null
30+
? [
31+
[
32+
require.resolve('babel-plugin-istanbul'),
33+
{
34+
include: [
35+
'packages/react-native/Libraries/**/*.js',
36+
'packages/react-native/src/**/*.js',
37+
'packages/virtualized-lists/**/*.js',
38+
],
39+
exclude: [
40+
'packages/react-native/Libraries/Renderer/**',
41+
'**/__tests__/**',
42+
],
43+
},
44+
],
45+
]
46+
: []),
2947
],
3048
};
3149
return MetroBabelTransformer.transform(processedArgs);

private/react-native-fantom/runner/bundling.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import path from 'path';
1515

1616
type BundleOptions = {
1717
...RunBuildOptions,
18+
customTransformOptions: ?{
19+
collectCoverage: boolean,
20+
},
1821
out: $NonMaybeType<RunBuildOptions['out']>,
1922
testPath: string,
2023
};
@@ -94,6 +97,7 @@ function getBundleBaseURL({
9497
dev,
9598
sourceMap,
9699
sourceMapUrl,
100+
customTransformOptions,
97101
}: BundleOptions): URL {
98102
const requestPath = path.relative(PROJECT_ROOT, entry).replace(/\.js$/, '');
99103
const port = getMetroPort();
@@ -120,6 +124,15 @@ function getBundleBaseURL({
120124
baseURL.searchParams.append('sourceMapUrl', sourceMapUrl);
121125
}
122126

127+
if (customTransformOptions != null) {
128+
Object.keys(customTransformOptions).forEach(key => {
129+
const value = customTransformOptions[key];
130+
if (value != null) {
131+
baseURL.searchParams.append(`transform.${key}`, JSON.stringify(value));
132+
}
133+
});
134+
}
135+
123136
return baseURL;
124137
}
125138

private/react-native-fantom/runner/global-setup/build.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ async function warmUpMetro(isOptimizedMode: boolean): Promise<void> {
7878
platform: 'android',
7979
minify: isOptimizedMode,
8080
dev: !isOptimizedMode,
81+
customTransformOptions: undefined,
8182
});
8283

8384
try {

private/react-native-fantom/runner/runner.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
import type {
12+
CoverageMap,
1213
FailureDetail,
1314
TestCaseResult,
1415
TestSuiteResult,
@@ -193,6 +194,7 @@ function generateBytecodeBundle({
193194
module.exports = async function runTest(
194195
globalConfig: {
195196
updateSnapshot: 'all' | 'new' | 'none',
197+
collectCoverage: boolean,
196198
...
197199
},
198200
config: {
@@ -205,6 +207,7 @@ module.exports = async function runTest(
205207
runtime: {...},
206208
testPath: string,
207209
): mixed {
210+
let coverageMap: CoverageMap | void;
208211
const snapshotResolver = await buildSnapshotResolver(config);
209212
const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath);
210213
const snapshotState = new SnapshotState(snapshotPath, {
@@ -339,6 +342,9 @@ module.exports = async function runTest(
339342
dev: !testConfig.isJsOptimized,
340343
sourceMap: true,
341344
sourceMapUrl: sourceMapPath,
345+
customTransformOptions: {
346+
collectCoverage: globalConfig.collectCoverage,
347+
},
342348
};
343349

344350
await createBundle({
@@ -456,6 +462,8 @@ module.exports = async function runTest(
456462
}
457463

458464
testResultsByConfig.push(testResults);
465+
466+
coverageMap = processedResult.coverageMap;
459467
}
460468

461469
const endTime = Date.now();
@@ -481,6 +489,7 @@ module.exports = async function runTest(
481489
globalConfig,
482490
testPath,
483491
),
492+
coverage: coverageMap,
484493
leaks: false,
485494
openHandles: [],
486495
perfStats: {

private/react-native-fantom/runtime/setup.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ export type FailureDetail = {
3636
cause?: FailureDetail,
3737
};
3838

39+
export opaque type CoverageMap = mixed;
40+
3941
export type TestSuiteResult =
4042
| {
4143
testResults: Array<TestCaseResult>,
44+
coverageMap?: CoverageMap,
4245
}
4346
| {
4447
error: FailureDetail,
@@ -465,6 +468,7 @@ global.$$RunTests$$ = () => {
465468
} else {
466469
reportTestSuiteResult({
467470
testResults: runTest(),
471+
coverageMap: global.__coverage__,
468472
});
469473
}
470474
};

0 commit comments

Comments
 (0)