mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Add module mock for DefaultToolLauncher and apply in tests (#55728)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55728 Fixes unwanted side effect launches of the debugger shell in tests (likely introduced in D88161573!). Also adds error guard on `DefaultAppLauncher` to ensure that the implementation is always mocked. Changelog: [Internal] Reviewed By: vzaidman Differential Revision: D94230761 fbshipit-source-id: 966f6f09c38aa55594091bd7d34a5c78dc6eee93
This commit is contained in:
1 parent
4206c12180
commit
703fe8b222
6 files changed
+67
-14
No files matched your search
@@ -12,8 +12,8 @@ import type {
|
||||
JsonPagesListResponse,
|
||||
JsonVersionResponse,
|
||||
} from '../inspector-proxy/types';
|
||||
import type {DevToolLauncher} from '../types/DevToolLauncher';
|
||||
|
||||
import DefaultToolLauncher from '../utils/DefaultToolLauncher';
|
||||
import {fetchJson, requestLocal} from './FetchUtils';
|
||||
import {createDeviceMock} from './InspectorDeviceUtils';
|
||||
import {withAbortSignalForEachTest} from './ResourceUtils';
|
||||
@@ -24,12 +24,19 @@ const PAGES_POLLING_DELAY = 2100;
|
||||
|
||||
jest.useFakeTimers();
|
||||
|
||||
const mockDevToolLauncher: DevToolLauncher = {
|
||||
launchDebuggerAppWindow: jest
|
||||
.fn<[string], Promise<void>>()
|
||||
.mockImplementation(() => Promise.resolve()),
|
||||
};
|
||||
|
||||
describe('inspector proxy HTTP API', () => {
|
||||
const serverRef = withServerForEachTest({
|
||||
logger: undefined,
|
||||
unstable_experiments: {
|
||||
enableStandaloneFuseboxShell: false,
|
||||
},
|
||||
unstable_toolLauncher: mockDevToolLauncher,
|
||||
});
|
||||
const autoCleanup = withAbortSignalForEachTest();
|
||||
afterEach(() => {
|
||||
@@ -397,11 +404,6 @@ describe('inspector proxy HTTP API', () => {
|
||||
]);
|
||||
jest.advanceTimersByTime(PAGES_POLLING_DELAY);
|
||||
|
||||
// Hook into `DefaultToolLauncher.launchDebuggerAppWindow` to ensure debugger was launched
|
||||
const launchDebuggerSpy = jest
|
||||
.spyOn(DefaultToolLauncher, 'launchDebuggerAppWindow')
|
||||
.mockResolvedValueOnce();
|
||||
|
||||
try {
|
||||
// Fetch the target information for the device
|
||||
const pageListResponse = await fetchJson<JsonPagesListResponse>(
|
||||
@@ -426,7 +428,9 @@ describe('inspector proxy HTTP API', () => {
|
||||
// Ensure the request was handled properly
|
||||
expect(response.statusCode).toBe(200);
|
||||
// Ensure the debugger was launched
|
||||
expect(launchDebuggerSpy).toHaveBeenCalledWith(expect.any(String));
|
||||
expect(
|
||||
mockDevToolLauncher.launchDebuggerAppWindow,
|
||||
).toHaveBeenCalledWith(expect.any(String));
|
||||
} finally {
|
||||
device.close();
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@ type CreateServerOptions = {
|
||||
};
|
||||
type ConnectApp = ReturnType<typeof connect>;
|
||||
|
||||
jest.mock('../utils/DefaultToolLauncher');
|
||||
|
||||
export function withServerForEachTest(options: CreateServerOptions): Readonly<{
|
||||
serverBaseUrl: string,
|
||||
serverBaseWsUrl: string,
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
import type {JsonPagesListResponse} from '../inspector-proxy/types';
|
||||
import type {DevToolLauncher} from '../types/DevToolLauncher';
|
||||
|
||||
import DefaultToolLauncher from '../utils/DefaultToolLauncher';
|
||||
import {fetchJson, requestLocal} from './FetchUtils';
|
||||
import {createDeviceMock} from './InspectorDeviceUtils';
|
||||
import {withAbortSignalForEachTest} from './ResourceUtils';
|
||||
@@ -24,7 +23,7 @@ jest.useFakeTimers();
|
||||
|
||||
describe('enableStandaloneFuseboxShell experiment', () => {
|
||||
const ToolLauncherWithFuseboxShell: DevToolLauncher = {
|
||||
...DefaultToolLauncher,
|
||||
launchDebuggerAppWindow: async (url: string) => {},
|
||||
launchDebuggerShell: () => {
|
||||
throw new Error('Not implemented');
|
||||
},
|
||||
|
||||
@@ -25,6 +25,10 @@ const open = require('open');
|
||||
*/
|
||||
const DefaultToolLauncher = {
|
||||
launchDebuggerAppWindow: async (url: string): Promise<void> => {
|
||||
if (process.env.NODE_ENV === 'test') {
|
||||
assertMockedInTests();
|
||||
}
|
||||
|
||||
let chromePath;
|
||||
|
||||
try {
|
||||
@@ -66,6 +70,10 @@ const DefaultToolLauncher = {
|
||||
},
|
||||
|
||||
async launchDebuggerShell(url: string, windowKey: string): Promise<void> {
|
||||
if (process.env.NODE_ENV === 'test') {
|
||||
assertMockedInTests();
|
||||
}
|
||||
|
||||
return await unstable_spawnDebuggerShellWithArgs([
|
||||
'--frontendUrl=' + url,
|
||||
'--windowKey=' + windowKey,
|
||||
@@ -75,8 +83,21 @@ const DefaultToolLauncher = {
|
||||
async prepareDebuggerShell(
|
||||
prebuiltBinaryPath?: ?string,
|
||||
): Promise<DebuggerShellPreparationResult> {
|
||||
if (process.env.NODE_ENV === 'test') {
|
||||
assertMockedInTests();
|
||||
}
|
||||
|
||||
return await unstable_prepareDebuggerShell();
|
||||
},
|
||||
};
|
||||
|
||||
function assertMockedInTests(): void {
|
||||
if (process.env.NODE_ENV === 'test') {
|
||||
throw new Error(
|
||||
'DefaultToolLauncher must be mocked or overridden in tests. ' +
|
||||
"Add jest.mock('../utils/DefaultAppLauncher') to test setup.",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default DefaultToolLauncher;
|
||||
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @noflow
|
||||
* @format
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mock implementation of DefaultToolLauncher to prevent actual browser
|
||||
* and debugger-shell launches during tests.
|
||||
*/
|
||||
const DefaultToolLauncher = {
|
||||
launchDebuggerAppWindow: jest
|
||||
.fn()
|
||||
.mockImplementation(() => Promise.resolve()),
|
||||
launchDebuggerShell: jest.fn().mockImplementation(() => Promise.resolve()),
|
||||
prepareDebuggerShell: jest.fn().mockResolvedValue({code: 'not_implemented'}),
|
||||
};
|
||||
|
||||
export default DefaultToolLauncher;
|
||||
@@ -8,6 +8,7 @@
|
||||
* @format
|
||||
*/
|
||||
|
||||
import type {DevToolLauncher} from '@react-native/dev-middleware';
|
||||
import type {ConfigT} from 'metro-config';
|
||||
|
||||
import {isOSS, validateEnvironmentVariables} from '../EnvironmentOptions';
|
||||
@@ -48,16 +49,19 @@ async function startMetroServer() {
|
||||
// $FlowExpectedError[cannot-write]
|
||||
metroConfig.server.port = Number(process.env.__FANTOM_METRO_PORT__);
|
||||
|
||||
// No-op app launcher to prevent debugger-shell launches during tests
|
||||
const devToolLauncher: DevToolLauncher = {
|
||||
launchDebuggerAppWindow: async (_url: string) => {},
|
||||
launchDebuggerShell: async () => {},
|
||||
prepareDebuggerShell: async () => ({code: 'not_implemented'}),
|
||||
};
|
||||
|
||||
const {
|
||||
middleware: devMiddleware,
|
||||
websocketEndpoints: debuggerWebsocketEndpoints,
|
||||
} = createDevMiddleware({
|
||||
serverBaseUrl: `http://localhost:${metroConfig.server.port}`,
|
||||
// Disable standalone DevTools shell preparation to avoid launching the
|
||||
// Electron app during tests. This prevents crashes.
|
||||
unstable_experiments: {
|
||||
enableStandaloneFuseboxShell: false,
|
||||
},
|
||||
unstable_toolLauncher: devToolLauncher,
|
||||
});
|
||||
|
||||
const enhanceMiddleware: ConfigT['server']['enhanceMiddleware'] = (
|
||||
|
||||
Reference in new issue
Block a user