From 0580e88aa5510dddd35ef780086580f1a41bb6cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Mon, 2 Dec 2024 06:06:26 -0800 Subject: [PATCH] Allow tests to specify opt/dev mode (#48022) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/48022 Changelog: [internal] This adds support for Fantom options in tests to configure different aspects of the test execution. For now, it only supports specifying the mode (dev or opt) so we can try things without having to change the runner (watch mode still works if you change mode :D). Fantom options are specified as pragmas in the docblock of the test. E.g.: ``` /** * flow strict-local * format * fantom_mode opt */ ``` We expect this is mostly going to be used for one-time tests and that regular tests won't specify the mode (they'll just run in dev mode). Maybe we can evolve this in the future to specify that you want a test to be executed in both modes, to ensure the behavior is consistent in dev/prod. Reviewed By: rshest Differential Revision: D66597626 fbshipit-source-id: b12325fc2235740cc2a3e0283d6a556091c1794c --- jest/integration/runner/runner.js | 6 ++-- jest/integration/runner/utils.js | 50 +++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/jest/integration/runner/runner.js b/jest/integration/runner/runner.js index b8da6171a26..2baaaceb849 100644 --- a/jest/integration/runner/runner.js +++ b/jest/integration/runner/runner.js @@ -15,6 +15,7 @@ import entrypointTemplate from './entrypoint-template'; import { getBuckModeForPlatform, getDebugInfoFromCommandResult, + getFantomTestConfig, getShortHash, runBuck2, symbolicateStackTrace, @@ -28,7 +29,6 @@ import path from 'path'; const BUILD_OUTPUT_PATH = path.resolve(__dirname, '..', 'build'); -const ENABLE_OPTIMIZED_MODE: false = false; const PRINT_FANTOM_OUTPUT: false = false; function parseRNTesterCommandResult(result: ReturnType): { @@ -95,7 +95,9 @@ module.exports = async function runTest( ): mixed { const startTime = Date.now(); - const isOptimizedMode = ENABLE_OPTIMIZED_MODE; + const testConfig = getFantomTestConfig(testPath); + + const isOptimizedMode = testConfig.mode === 'opt'; const metroConfig = await Metro.loadConfig({ config: path.resolve(__dirname, '..', 'config', 'metro.config.js'), diff --git a/jest/integration/runner/utils.js b/jest/integration/runner/utils.js index 3671f636afb..9e43a68f16d 100644 --- a/jest/integration/runner/utils.js +++ b/jest/integration/runner/utils.js @@ -12,10 +12,60 @@ import {spawnSync} from 'child_process'; import crypto from 'crypto'; import fs from 'fs'; +// $FlowExpectedError[untyped-import] +import {extract, parse} from 'jest-docblock'; import os from 'os'; // $FlowExpectedError[untyped-import] import {SourceMapConsumer} from 'source-map'; +type DocblockPragmas = {[key: string]: string | string[]}; +type FantomTestMode = 'dev' | 'opt'; +type FantomTestConfig = { + mode: FantomTestMode, +}; + +const DEFAULT_MODE: FantomTestMode = 'dev'; + +/** + * Extracts the Fantom configuration from the test file, specified as part of + * the docblock comment. E.g.: + * + * ``` + * /** + * * @flow strict-local + * * @fantom mode:opt + * * + * ``` + * + * So far the only supported option is `mode`, which can be 'dev' or 'opt'. + */ +export function getFantomTestConfig(testPath: string): FantomTestConfig { + const docblock = extract(fs.readFileSync(testPath, 'utf8')); + const pragmas = parse(docblock) as DocblockPragmas; + + const config = { + mode: DEFAULT_MODE, + }; + + const maybeMode = pragmas.fantom_mode; + + if (maybeMode != null) { + if (Array.isArray(maybeMode)) { + throw new Error('Expected a single value for @fantom_mode'); + } + + const mode = maybeMode; + + if (mode === 'dev' || mode === 'opt') { + config.mode = mode; + } else { + throw new Error(`Invalid Fantom mode: ${mode}`); + } + } + + return config; +} + export function getBuckModeForPlatform(enableRelease: boolean = false): string { const mode = enableRelease ? 'opt' : 'dev';