Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/react-native-fantom/runner/entrypoint-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ module.exports = function entrypointTemplate({
*/

import {registerTest} from '${setupModulePath}';
import {setConstants} from '@react-native/fantom';
import {setConstants} from '@react-native/fantom/src/Constants';

${
Object.keys(testConfig.flags.jsOnly).length > 0
? `import * as ReactNativeFeatureFlags from '${featureFlagsModulePath}';
Expand Down
8 changes: 6 additions & 2 deletions packages/react-native-fantom/runtime/patchWeakRef.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
* @format
*/

import * as Fantom from '@react-native/fantom';

let initialized = false;

/**
Expand All @@ -31,6 +29,9 @@ export default function patchWeakRef(): void {

// $FlowExpectedError[cannot-write]
WeakRef.prototype.deref = function patchedDeref<T>(this: WeakRef<T>): T {
// Lazily require the module to avoid loading it before the test logic.
const Fantom = require('@react-native/fantom');

if (!Fantom.isInWorkLoop()) {
throw new Error(
'Unexpected call to `WeakRef.deref()` outside of the Event Loop. Please use this method within `Fantom.runTask()`.',
Expand All @@ -43,6 +44,9 @@ export default function patchWeakRef(): void {
const OriginalWeakRef = WeakRef;

global.WeakRef = function WeakRef(...args) {
// Lazily require the module to avoid loading it before the test logic.
const Fantom = require('@react-native/fantom');

if (!Fantom.isInWorkLoop()) {
throw new Error(
'Unexpected instantiation of `WeakRef` outside of the Event Loop. Please create the instance within `Fantom.runTask()`.',
Expand Down
9 changes: 8 additions & 1 deletion packages/react-native-fantom/runtime/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import expect from './expect';
import {createMockFunction} from './mocks';
import patchWeakRef from './patchWeakRef';
import {setupSnapshotConfig, snapshotContext} from './snapshotContext';
import NativeFantom from 'react-native/src/private/testing/fantom/specs/NativeFantom';

export type TestCaseResult = {
ancestorTitles: Array<string>,
Expand Down Expand Up @@ -380,6 +379,10 @@ function runSuite(suite: Suite): TestCaseResult[] {
}

function reportTestSuiteResult(testSuiteResult: TestSuiteResult): void {
// Force the import of the native module to be lazy
const NativeFantom =
require('react-native/src/private/testing/fantom/specs/NativeFantom').default;

NativeFantom.reportTestSuiteResultsJSON(
JSON.stringify({
type: 'test-result',
Expand All @@ -389,6 +392,10 @@ function reportTestSuiteResult(testSuiteResult: TestSuiteResult): void {
}

function validateEmptyMessageQueue(): void {
// Force the import of the native module to be lazy
const NativeFantom =
require('react-native/src/private/testing/fantom/specs/NativeFantom').default;

NativeFantom.validateEmptyMessageQueue();
}

Expand Down
27 changes: 27 additions & 0 deletions packages/react-native-fantom/src/Constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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
*/

type FantomConstants = $ReadOnly<{
isRunningFromCI: boolean,
fantomConfigSummary: string,
}>;

let constants: FantomConstants = {
isRunningFromCI: false,
fantomConfigSummary: '',
};

export function getConstants(): FantomConstants {
return constants;
}

export function setConstants(newConstants: FantomConstants): void {
constants = newConstants;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* 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
*/

describe('FantomModuleInit', () => {
it('should not load the Fantom module eagerly', () => {
expect(global.__FANTOM_PACKAGE_LOADED__).toBeUndefined();
});

it('should load the Fantom module lazily', () => {
require('@react-native/fantom');

expect(global.__FANTOM_PACKAGE_LOADED__).toBe(true);
});
});
23 changes: 5 additions & 18 deletions packages/react-native-fantom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type ReactNativeDocument from 'react-native/src/private/webapis/dom/nodes

import ReactNativeElement from '../../react-native/src/private/webapis/dom/nodes/ReadOnlyNode';
import * as Benchmark from './Benchmark';
import {getConstants} from './Constants';
import getFantomRenderedOutput from './getFantomRenderedOutput';
import {LogBox} from 'react-native';
import {createRootTag} from 'react-native/Libraries/ReactNative/RootTag';
Expand All @@ -38,6 +39,8 @@ export type RootConfig = {
devicePixelRatio?: number,
};

export {getConstants} from './Constants';

// Defaults use iPhone 14 values (very common device).
const DEFAULT_VIEWPORT_WIDTH = 390;
const DEFAULT_VIEWPORT_HEIGHT = 844;
Expand Down Expand Up @@ -520,24 +523,6 @@ export function enqueueModalSizeUpdate(

export const unstable_benchmark = Benchmark;

type FantomConstants = $ReadOnly<{
isRunningFromCI: boolean,
fantomConfigSummary: string,
}>;

let constants: FantomConstants = {
isRunningFromCI: false,
fantomConfigSummary: '',
};

export function getConstants(): FantomConstants {
return constants;
}

export function setConstants(newConstants: FantomConstants): void {
constants = newConstants;
}

/**
* Quick and dirty polyfills required by tinybench.
*/
Expand Down Expand Up @@ -662,3 +647,5 @@ function runLogBoxCheck() {
throw new Error(message);
}
}

global.__FANTOM_PACKAGE_LOADED__ = true;
Loading