Skip to content

Commit 84376af

Browse files
committed
fix(runtime): stabilize runtime test environment
1 parent 780a669 commit 84376af

4 files changed

Lines changed: 57 additions & 27 deletions

File tree

packages/runtime/src/__tests__/runner-context.test.ts

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
import {
2-
type HarnessTestContext,
32
afterEach,
43
beforeEach,
54
describe as harnessDescribe,
65
getTestCollector,
76
it as harnessIt,
87
} from '../collector/index.js';
8+
import type { HarnessTestContext } from '@react-native-harness/bridge';
99
import { getTestRunner } from '../runner/index.js';
1010
import { describe, expect, it, vi } from 'vitest';
1111

1212
vi.mock('../symbolicate.js', async () => {
13-
const actual = await vi.importActual<typeof import('../symbolicate.js')>(
14-
'../symbolicate.js',
15-
);
16-
1713
return {
18-
...actual,
1914
getCodeFrame: vi.fn(async () => null),
2015
};
2116
});
@@ -28,6 +23,14 @@ const getTask = (context?: HarnessTestContext) => {
2823
return context.task;
2924
};
3025

26+
const getTaskContext = (context?: HarnessTestContext) => {
27+
if (!context) {
28+
throw new Error('Expected test context to be defined');
29+
}
30+
31+
return context;
32+
};
33+
3134
describe('runner task context', () => {
3235
it('passes minimal task metadata to tests and per-test hooks', async () => {
3336
const observedTasks: Array<{
@@ -46,15 +49,15 @@ describe('runner task context', () => {
4649
try {
4750
const collection = await collector.collect(() => {
4851
harnessDescribe('Task Context Suite', () => {
49-
beforeEach((context) => {
52+
beforeEach((context: HarnessTestContext | undefined) => {
5053
observedTasks.push({ source: 'beforeEach', task: getTask(context) });
5154
});
5255

53-
afterEach((context) => {
56+
afterEach((context: HarnessTestContext | undefined) => {
5457
observedTasks.push({ source: 'afterEach', task: getTask(context) });
5558
});
5659

57-
harnessIt('exposes task metadata', (context) => {
60+
harnessIt('exposes task metadata', (context: HarnessTestContext | undefined) => {
5861
observedTasks.push({ source: 'test', task: getTask(context) });
5962
});
6063
});
@@ -157,7 +160,9 @@ describe('runner task context', () => {
157160
calls.push('afterEach');
158161
});
159162

160-
harnessIt('skips from context', ({ skip }) => {
163+
harnessIt('skips from context', (context: HarnessTestContext | undefined) => {
164+
const { skip } = getTaskContext(context);
165+
161166
calls.push('before-skip');
162167
skip('skip this test');
163168
calls.push('after-skip');
@@ -199,7 +204,9 @@ describe('runner task context', () => {
199204
try {
200205
const collection = await collector.collect(() => {
201206
harnessDescribe('Conditional Skip Suite', () => {
202-
harnessIt('continues when condition is false', ({ skip }) => {
207+
harnessIt('continues when condition is false', (context: HarnessTestContext | undefined) => {
208+
const { skip } = getTaskContext(context);
209+
203210
calls.push('before');
204211
skip(false, 'do not skip');
205212
calls.push('after');
@@ -233,7 +240,9 @@ describe('runner task context', () => {
233240
calls.push('afterEach');
234241
});
235242

236-
harnessIt('runs finished callbacks', ({ onTestFinished }) => {
243+
harnessIt('runs finished callbacks', (context: HarnessTestContext | undefined) => {
244+
const { onTestFinished } = getTaskContext(context);
245+
237246
onTestFinished(() => {
238247
calls.push('onTestFinished:first');
239248
});
@@ -277,14 +286,19 @@ describe('runner task context', () => {
277286
calls.push('afterEach');
278287
});
279288

280-
harnessIt('runs finished callback after skip', ({ onTestFinished, skip }) => {
289+
harnessIt(
290+
'runs finished callback after skip',
291+
(context: HarnessTestContext | undefined) => {
292+
const { onTestFinished, skip } = getTaskContext(context);
293+
281294
onTestFinished(() => {
282295
calls.push('onTestFinished');
283296
});
284297

285298
calls.push('before-skip');
286299
skip();
287-
});
300+
},
301+
);
288302
});
289303
}, 'runtime/on-test-finished-skip.test.ts');
290304

@@ -314,14 +328,19 @@ describe('runner task context', () => {
314328
calls.push('afterEach');
315329
});
316330

317-
harnessIt('runs finished callback after failure', ({ onTestFinished }) => {
331+
harnessIt(
332+
'runs finished callback after failure',
333+
(context: HarnessTestContext | undefined) => {
334+
const { onTestFinished } = getTaskContext(context);
335+
318336
onTestFinished(() => {
319337
calls.push('onTestFinished');
320338
});
321339

322340
calls.push('test');
323341
throw new Error('expected failure');
324-
});
342+
},
343+
);
325344
});
326345
}, 'runtime/on-test-finished-failure.test.ts');
327346

@@ -351,7 +370,9 @@ describe('runner task context', () => {
351370
calls.push('afterEach');
352371
});
353372

354-
harnessIt('runs failed callbacks', ({ onTestFailed }) => {
373+
harnessIt('runs failed callbacks', (context: HarnessTestContext | undefined) => {
374+
const { onTestFailed } = getTaskContext(context);
375+
355376
onTestFailed(() => {
356377
calls.push('onTestFailed:first');
357378
});
@@ -396,14 +417,19 @@ describe('runner task context', () => {
396417
calls.push('afterEach');
397418
});
398419

399-
harnessIt('does not run failed callbacks on skip', ({ onTestFailed, skip }) => {
420+
harnessIt(
421+
'does not run failed callbacks on skip',
422+
(context: HarnessTestContext | undefined) => {
423+
const { onTestFailed, skip } = getTaskContext(context);
424+
400425
onTestFailed(() => {
401426
calls.push('onTestFailed');
402427
});
403428

404429
calls.push('before-skip');
405430
skip();
406-
});
431+
},
432+
);
407433
});
408434
}, 'runtime/on-test-failed-skip.test.ts');
409435

@@ -434,13 +460,18 @@ describe('runner task context', () => {
434460
throw new Error('afterEach failure');
435461
});
436462

437-
harnessIt('runs failed callback after afterEach failure', ({ onTestFailed }) => {
463+
harnessIt(
464+
'runs failed callback after afterEach failure',
465+
(context: HarnessTestContext | undefined) => {
466+
const { onTestFailed } = getTaskContext(context);
467+
438468
onTestFailed(() => {
439469
calls.push('onTestFailed');
440470
});
441471

442472
calls.push('test');
443-
});
473+
},
474+
);
444475
});
445476
}, 'runtime/on-test-failed-after-each.test.ts');
446477

packages/runtime/src/client/getWSServer.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ vi.mock('../utils/dev-server.js', () => ({
1010
getDevServerUrl: mocks.getDevServerUrl,
1111
}));
1212

13-
vi.mock('react-native-url-polyfill', () => ({
14-
URL,
15-
}));
16-
1713
describe('getWSServer', () => {
1814
beforeEach(() => {
1915
mocks.getDevServerUrl.mockReset();

packages/runtime/src/client/getWSServer.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { HARNESS_BRIDGE_PATH } from '@react-native-harness/bridge';
2-
import { URL } from 'react-native-url-polyfill';
32
import { getDevServerUrl } from '../utils/dev-server.js';
43

54
export const getWSServer = (): string => {
65
const devServerUrlString = getDevServerUrl();
7-
const devServerUrl = new URL(devServerUrlString);
6+
const devServerUrl = new globalThis.URL(devServerUrlString);
87

98
if (!devServerUrl.host) {
109
throw new TypeError(`Invalid URL: ${devServerUrlString}`);

packages/runtime/src/react-native.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ declare module 'react-native/Libraries/Core/Devtools/parseErrorStack' {
1717
export default function parseErrorStack(errorStack?: string): StackFrame[];
1818
}
1919

20+
declare module 'react-native-url-polyfill' {
21+
export const URL: typeof globalThis.URL;
22+
}
23+
2024
declare module '*.png' {
2125
import type { ImageSourcePropType } from 'react-native';
2226

0 commit comments

Comments
 (0)