Skip to content
Merged
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
1 change: 1 addition & 0 deletions packages/bundler-metro/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { getMetroInstance } from './factory.js';
export type { MetroInstance, MetroFactory, MetroOptions } from './types.js';
export { prewarmMetroBundle } from './prewarm.js';
export type { Reporter, ReportableEvent } from './reporter.js';
36 changes: 36 additions & 0 deletions packages/bundler-metro/src/prewarm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { METRO_PORT } from './constants.js';
import { getResolvedEntryPointWithoutExtension } from './entry-point-utils.js';

type PrewarmOptions = {
projectRoot: string;
entryPoint: string;
platform: string;
dev: boolean;
minify: boolean;
signal: AbortSignal;
};

export const prewarmMetroBundle = async (
options: PrewarmOptions
): Promise<void> => {
const { projectRoot, entryPoint, platform, dev, minify, signal } = options;
const resolvedEntryPoint = getResolvedEntryPointWithoutExtension(
projectRoot,
entryPoint
);
const searchParams = new URLSearchParams({
platform,
dev: String(dev),
minify: String(minify),
});
const url = `http://localhost:${METRO_PORT}/${resolvedEntryPoint}.bundle?${searchParams.toString()}`;

const response = await fetch(url, { signal });

if (!response.ok) {
const snippet = (await response.text()).trim();
throw new Error(
`Metro pre-warm failed (${response.status} ${response.statusText}). ${snippet}`
);
}
};
1 change: 1 addition & 0 deletions packages/config/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const RunnerSchema = z.object({
),
config: z.record(z.any()),
runner: z.string(),
platformId: z.string(),
});

export const ConfigSchema = z
Expand Down
12 changes: 12 additions & 0 deletions packages/jest/src/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import {
} from '@react-native-harness/platforms';
import {
getMetroInstance,
prewarmMetroBundle,
Reporter,
ReportableEvent,
} from '@react-native-harness/bundler-metro';
import { InitializationTimeoutError, MaxAppRestartsError } from './errors.js';
import { Config as HarnessConfig } from '@react-native-harness/config';
import { createCrashMonitor, CrashMonitor } from './crash-monitor.js';
import { createClientLogListener } from './client-log-handler.js';
import { logMetroPrewarmCompleted } from './logs.js';

export type HarnessRunTestsOptions = Exclude<TestExecutionOptions, 'platform'>;

Expand Down Expand Up @@ -176,6 +178,16 @@ const getHarnessInternal = async (
}

try {
await prewarmMetroBundle({
projectRoot,
entryPoint: config.entryPoint,
platform: platform.platformId,
dev: true,
minify: false,
signal,
});
logMetroPrewarmCompleted(platform);

await waitForAppReady({
metroEvents: metroInstance.events,
serverBridge,
Expand Down
6 changes: 6 additions & 0 deletions packages/jest/src/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export const logTestEnvironmentReady = (runner: HarnessPlatform): void => {
log(`${TAG} Runner ${chalk.bold(runner.name)} ready\n`);
};

export const logMetroPrewarmCompleted = (runner: HarnessPlatform): void => {
log(
`${TAG} Metro pre-warm for ${chalk.bold(runner.name)} completed\n`
);
};

export const getErrorMessage = (error: HarnessError): string => {
return `${ERROR_TAG} ${error.message}\n`;
};
1 change: 1 addition & 0 deletions packages/platform-android/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ export const androidPlatform = (
name: config.name,
config,
runner: import.meta.resolve('./runner.js'),
platformId: 'android',
});
1 change: 1 addition & 0 deletions packages/platform-ios/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ export const applePlatform = (
name: config.name,
config,
runner: import.meta.resolve('./runner.js'),
platformId: 'ios',
});
1 change: 1 addition & 0 deletions packages/platform-vega/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export const vegaPlatform = (
name: config.name,
config,
runner: import.meta.resolve('./runner.js'),
platformId: 'vega',
});
1 change: 1 addition & 0 deletions packages/platform-web/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const webPlatform = (
name: config.name,
config,
runner: import.meta.resolve('./runner.js'),
platformId: 'web',
});

export const chromium = (
Expand Down
1 change: 1 addition & 0 deletions packages/platforms/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type HarnessPlatform<TConfig = Record<string, unknown>> = {
name: string;
config: TConfig;
runner: string;
platformId: string;
};

export type RunTarget = {
Expand Down