From 4d07fb7662aa606f2884a754fb87c229f0021e7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Mon, 9 Dec 2024 05:43:27 -0800 Subject: [PATCH] Use enum for Fantom modes (#48179) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/48179 Changelog: [internal] Migrating this type to an enum, which is safer, because it prevents errors like: ``` // when it's actually 'dev' if (mode === 'development') { } ``` Reviewed By: rshest Differential Revision: D66888985 fbshipit-source-id: 4f3f91fad6ca5256baa2123425b2bad11fe036f9 --- .../runner/getFantomTestConfig.js | 20 +++++++++++++------ packages/react-native-fantom/runner/runner.js | 3 ++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/packages/react-native-fantom/runner/getFantomTestConfig.js b/packages/react-native-fantom/runner/getFantomTestConfig.js index c7d662045d7ce3..3051813c19d3cc 100644 --- a/packages/react-native-fantom/runner/getFantomTestConfig.js +++ b/packages/react-native-fantom/runner/getFantomTestConfig.js @@ -19,7 +19,10 @@ type JsOnlyFeatureFlags = (typeof ReactNativeFeatureFlags)['jsOnly']; type DocblockPragmas = {[key: string]: string | string[]}; -export type FantomTestConfigMode = 'dev' | 'opt'; +export enum FantomTestConfigMode { + Development, + Optimized, +} export type FantomTestConfigCommonFeatureFlags = Partial<{ [key in keyof CommonFeatureFlags]: CommonFeatureFlags[key]['defaultValue'], @@ -37,7 +40,7 @@ export type FantomTestConfig = { }, }; -const DEFAULT_MODE: FantomTestConfigMode = 'dev'; +const DEFAULT_MODE: FantomTestConfigMode = FantomTestConfigMode.Development; const FANTOM_FLAG_FORMAT = /^(\w+):(\w+)$/; @@ -84,10 +87,15 @@ export default function getFantomTestConfig( const mode = maybeMode; - if (mode === 'dev' || mode === 'opt') { - config.mode = mode; - } else { - throw new Error(`Invalid Fantom mode: ${mode}`); + switch (mode) { + case 'dev': + config.mode = FantomTestConfigMode.Development; + break; + case 'opt': + config.mode = FantomTestConfigMode.Optimized; + break; + default: + throw new Error(`Invalid Fantom mode: ${mode}`); } } diff --git a/packages/react-native-fantom/runner/runner.js b/packages/react-native-fantom/runner/runner.js index 18b821393041cd..a1fd8201a8b1bc 100644 --- a/packages/react-native-fantom/runner/runner.js +++ b/packages/react-native-fantom/runner/runner.js @@ -13,6 +13,7 @@ import type {TestSuiteResult} from '../runtime/setup'; import entrypointTemplate from './entrypoint-template'; import getFantomTestConfig from './getFantomTestConfig'; +import {FantomTestConfigMode} from './getFantomTestConfig'; import { getBuckModeForPlatform, getDebugInfoFromCommandResult, @@ -97,7 +98,7 @@ module.exports = async function runTest( const testConfig = getFantomTestConfig(testPath); - const isOptimizedMode = testConfig.mode === 'opt'; + const isOptimizedMode = testConfig.mode === FantomTestConfigMode.Optimized; const metroConfig = await Metro.loadConfig({ config: path.resolve(__dirname, '..', 'config', 'metro.config.js'),