Skip to content

Commit 1e74359

Browse files
committed
fix: isolate android tests from host adb
1 parent 8537488 commit 1e74359

3 files changed

Lines changed: 70 additions & 0 deletions

File tree

packages/platform-android/src/__tests__/adb.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import path from 'node:path';
55
import { PassThrough } from 'node:stream';
66
import { beforeEach, describe, expect, it, vi } from 'vitest';
77
import { SubprocessError } from '@react-native-harness/tools';
8+
9+
vi.unmock('../adb.js');
10+
811
import {
912
createAvd,
1013
deleteAvd,
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { PassThrough } from 'node:stream';
2+
import { vi } from 'vitest';
3+
import type { ChildProcessByStdio } from 'node:child_process';
4+
5+
const createMockChildProcess = (): ChildProcessByStdio<
6+
null,
7+
PassThrough,
8+
PassThrough
9+
> => {
10+
const stdout = new PassThrough();
11+
const stderr = new PassThrough();
12+
13+
return {
14+
stdout,
15+
stderr,
16+
unref: vi.fn(),
17+
once: vi.fn(),
18+
removeAllListeners: vi.fn(),
19+
} as unknown as ChildProcessByStdio<null, PassThrough, PassThrough>;
20+
};
21+
22+
const createMockSubprocess = () => ({
23+
nodeChildProcess: Promise.resolve({
24+
kill: vi.fn(),
25+
}),
26+
[Symbol.asyncIterator]: async function* () {},
27+
});
28+
29+
vi.mock('../src/adb.js', async (importOriginal) => {
30+
const actual = await importOriginal<typeof import('../src/adb.js')>();
31+
32+
return {
33+
...actual,
34+
emulatorProcess: {
35+
startDetachedProcess: vi.fn(createMockChildProcess),
36+
},
37+
verifyAndroidEmulatorSdk: vi.fn().mockResolvedValue(undefined),
38+
isAppInstalled: vi.fn().mockResolvedValue(true),
39+
reversePort: vi.fn().mockResolvedValue(undefined),
40+
stopApp: vi.fn().mockResolvedValue(undefined),
41+
startApp: vi.fn().mockResolvedValue(undefined),
42+
getDeviceIds: vi.fn().mockResolvedValue([]),
43+
getEmulatorName: vi.fn().mockResolvedValue(''),
44+
getShellProperty: vi.fn().mockResolvedValue(''),
45+
getDeviceInfo: vi.fn().mockResolvedValue(null),
46+
isBootCompleted: vi.fn().mockResolvedValue(true),
47+
stopEmulator: vi.fn().mockResolvedValue(undefined),
48+
installApp: vi.fn().mockResolvedValue(undefined),
49+
uninstallApp: vi.fn().mockResolvedValue(undefined),
50+
hasAvd: vi.fn().mockResolvedValue(false),
51+
createAvd: vi.fn().mockResolvedValue(undefined),
52+
deleteAvd: vi.fn().mockResolvedValue(undefined),
53+
startEmulator: vi.fn().mockResolvedValue(undefined),
54+
waitForEmulator: vi.fn().mockResolvedValue('emulator-5554'),
55+
waitForEmulatorDisconnect: vi.fn().mockResolvedValue(undefined),
56+
waitForBoot: vi.fn().mockResolvedValue('emulator-5554'),
57+
isAppRunning: vi.fn().mockResolvedValue(false),
58+
getAppUid: vi.fn().mockResolvedValue(0),
59+
setHideErrorDialogs: vi.fn().mockResolvedValue(undefined),
60+
getLogcatTimestamp: vi.fn().mockResolvedValue('01-01 00:00:00.000'),
61+
startLogcat: vi.fn(createMockSubprocess),
62+
getAvds: vi.fn().mockResolvedValue([]),
63+
getConnectedDevices: vi.fn().mockResolvedValue([]),
64+
grantPermissions: vi.fn().mockResolvedValue(undefined),
65+
};
66+
});

packages/platform-android/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default defineConfig(() => ({
88
watch: false,
99
globals: true,
1010
environment: 'node',
11+
setupFiles: ['./test/setup.ts'],
1112
include: ['{src,tests}/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
1213
reporters: ['default'],
1314
coverage: {

0 commit comments

Comments
 (0)