From 22002bb70c706d57ae81995d2bb4d229592ea313 Mon Sep 17 00:00:00 2001 From: Andrew Datsenko Date: Tue, 23 Sep 2025 13:40:31 -0700 Subject: [PATCH] Allow to disable coverage via flag & disable it for benchmarks (#53912) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/53912 Changelog: [Internal] Add new fantom flag `fantom_disable_coverage` to be used in tests that are sensitive to things like memory allocation. Also disable coverage for any benchmark test. Reviewed By: arushikesarwani94 Differential Revision: D83070065 fbshipit-source-id: a885e79e3d0c88cfb7adef30cb60ab46a617edac --- .../private/__tests__/MemoryBaseline-itest.js | 1 + .../react-native-fantom/__docs__/README.md | 3 ++ .../runner/coverageUtils.js | 35 +++++++++++++++++++ .../runner/getFantomTestConfigs.js | 1 + private/react-native-fantom/runner/runner.js | 7 +++- 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 private/react-native-fantom/runner/coverageUtils.js diff --git a/packages/react-native/src/private/__tests__/MemoryBaseline-itest.js b/packages/react-native/src/private/__tests__/MemoryBaseline-itest.js index bb4bd3f8cff..dab58d04617 100644 --- a/packages/react-native/src/private/__tests__/MemoryBaseline-itest.js +++ b/packages/react-native/src/private/__tests__/MemoryBaseline-itest.js @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. * * @fantom_mode * + * @fantom_disable_coverage * @flow strict-local * @format */ diff --git a/private/react-native-fantom/__docs__/README.md b/private/react-native-fantom/__docs__/README.md index f14e152bc7c..6b136a1af20 100644 --- a/private/react-native-fantom/__docs__/README.md +++ b/private/react-native-fantom/__docs__/README.md @@ -147,6 +147,9 @@ Available pragmas: - Possible values: - `true`: using Hermes bytecode - `false`: not using Hermes bytecode +- `@fantom_disable_coverage`: used to disable coverage collection for the test. + - Example: `@fantom_disable_coverage` + - Does not require a value. - `@fantom_react_fb_flags`: used to set overrides for internal React flags set in ReactNativeInternalFeatureFlags (Meta use only) diff --git a/private/react-native-fantom/runner/coverageUtils.js b/private/react-native-fantom/runner/coverageUtils.js new file mode 100644 index 00000000000..d800226a02c --- /dev/null +++ b/private/react-native-fantom/runner/coverageUtils.js @@ -0,0 +1,35 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict-local + * @format + */ + +// $FlowExpectedError[untyped-import] +import {extract, parse} from 'jest-docblock'; + +type DocblockPragmas = {[key: string]: string | string[]}; + +const FANTOM_BENCHMARK_FILENAME_RE = /[Bb]enchmark-itest\./g; + +export function shouldCollectCoverage( + testPath: string, + testContents: string, + globalConfig: {collectCoverage: boolean, ...}, +): boolean { + if (FANTOM_BENCHMARK_FILENAME_RE.test(testPath)) { + return false; + } + + const docblock = extract(testContents); + const pragmas = parse(docblock) as DocblockPragmas; + + if (pragmas.fantom_disable_coverage != null) { + return false; + } + + return globalConfig.collectCoverage; +} diff --git a/private/react-native-fantom/runner/getFantomTestConfigs.js b/private/react-native-fantom/runner/getFantomTestConfigs.js index be739cb1882..5c72f1ef1d9 100644 --- a/private/react-native-fantom/runner/getFantomTestConfigs.js +++ b/private/react-native-fantom/runner/getFantomTestConfigs.js @@ -85,6 +85,7 @@ const VALID_FANTOM_PRAGMAS = [ 'fantom_flags', 'fantom_hermes_variant', 'fantom_react_fb_flags', + 'fantom_disable_coverage', ]; export function getOverrides( diff --git a/private/react-native-fantom/runner/runner.js b/private/react-native-fantom/runner/runner.js index ae019a34530..1291f684331 100644 --- a/private/react-native-fantom/runner/runner.js +++ b/private/react-native-fantom/runner/runner.js @@ -24,6 +24,7 @@ import type { import {printBenchmarkResultsRanking} from './benchmarkUtils'; import {createBundle, createSourceMap} from './bundling'; +import {shouldCollectCoverage} from './coverageUtils'; import entrypointTemplate from './entrypoint-template'; import * as EnvironmentOptions from './EnvironmentOptions'; import {run as runHermesCompiler} from './executables/hermesc'; @@ -343,7 +344,11 @@ module.exports = async function runTest( sourceMap: true, sourceMapUrl: sourceMapPath, customTransformOptions: { - collectCoverage: globalConfig.collectCoverage, + collectCoverage: shouldCollectCoverage( + testPath, + testContents, + globalConfig, + ), }, };