@react-native-harness/jest 1.0.0 → 1.1.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-native-harness/jest",
3
- "version": "1.0.0",
3
+ "version": "1.1.0-rc.1",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -40,11 +40,11 @@
40
40
  "p-limit": "^7.1.1",
41
41
  "tslib": "^2.3.0",
42
42
  "yargs": "^17.7.2",
43
- "@react-native-harness/platforms": "1.0.0",
44
- "@react-native-harness/bridge": "1.0.0",
45
- "@react-native-harness/bundler-metro": "1.0.0",
46
- "@react-native-harness/tools": "1.0.0",
47
- "@react-native-harness/config": "1.0.0"
43
+ "@react-native-harness/bridge": "1.1.0-rc.1",
44
+ "@react-native-harness/bundler-metro": "1.1.0-rc.1",
45
+ "@react-native-harness/config": "1.1.0-rc.1",
46
+ "@react-native-harness/platforms": "1.1.0-rc.1",
47
+ "@react-native-harness/tools": "1.1.0-rc.1"
48
48
  },
49
49
  "devDependencies": {
50
50
  "@types/yargs": "^17.0.32",
@@ -0,0 +1,200 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import {
3
+ type AppMonitor,
4
+ type AppCrashDetails,
5
+ type AppMonitorEvent,
6
+ type AppMonitorListener,
7
+ type HarnessPlatformRunner,
8
+ } from '@react-native-harness/platforms';
9
+ import { createCrashSupervisor } from '../crash-supervisor.js';
10
+
11
+ type MockCrashDetailsGetter = (
12
+ options: {
13
+ processName?: string;
14
+ pid?: number;
15
+ occurredAt: number;
16
+ }
17
+ ) => Promise<AppCrashDetails | null>;
18
+
19
+ const createMockAppMonitor = (options?: {
20
+ getCrashDetails?: MockCrashDetailsGetter;
21
+ }) => {
22
+ const listeners = new Set<AppMonitorListener>();
23
+ const noop = async () => undefined;
24
+
25
+ const appMonitor: AppMonitor & {
26
+ getCrashDetails?: MockCrashDetailsGetter;
27
+ } = {
28
+ start: noop,
29
+ stop: noop,
30
+ dispose: noop,
31
+ addListener: (listener) => {
32
+ listeners.add(listener);
33
+ },
34
+ removeListener: (listener) => {
35
+ listeners.delete(listener);
36
+ },
37
+ };
38
+
39
+ if (options?.getCrashDetails) {
40
+ appMonitor.getCrashDetails = options.getCrashDetails;
41
+ }
42
+
43
+ const emit = (event: AppMonitorEvent) => {
44
+ for (const listener of listeners) {
45
+ listener(event);
46
+ }
47
+ };
48
+
49
+ return {
50
+ appMonitor,
51
+ emit,
52
+ };
53
+ };
54
+
55
+ const createPlatformRunner = ({
56
+ isAppRunning,
57
+ getCrashDetails,
58
+ }: {
59
+ isAppRunning: () => Promise<boolean>;
60
+ getCrashDetails?: HarnessPlatformRunner['getCrashDetails'];
61
+ }): HarnessPlatformRunner => ({
62
+ startApp: async () => undefined,
63
+ restartApp: async () => undefined,
64
+ stopApp: async () => undefined,
65
+ dispose: async () => undefined,
66
+ isAppRunning,
67
+ createAppMonitor: () => {
68
+ throw new Error('Not used in unit tests');
69
+ },
70
+ getCrashDetails,
71
+ });
72
+
73
+ describe('crash-supervisor', () => {
74
+ it('rejects the active test when the app exits during launch', async () => {
75
+ const monitor = createMockAppMonitor();
76
+ const supervisor = createCrashSupervisor({
77
+ appMonitor: monitor.appMonitor,
78
+ platformRunner: createPlatformRunner({
79
+ isAppRunning: vi.fn().mockResolvedValue(false),
80
+ }),
81
+ });
82
+
83
+ supervisor.beginLaunch('/tmp/startup.harness.ts');
84
+
85
+ const crashPromise = supervisor.waitForCrash('/tmp/startup.harness.ts');
86
+ const expectation =
87
+ expect(crashPromise).rejects.toMatchObject({
88
+ name: 'NativeCrashError',
89
+ phase: 'startup',
90
+ details: {
91
+ signal: 'SIGSEGV',
92
+ processName: 'com.harnessplayground',
93
+ },
94
+ });
95
+ monitor.emit({
96
+ type: 'app_exited',
97
+ source: 'logs',
98
+ crashDetails: {
99
+ source: 'logs',
100
+ signal: 'SIGSEGV',
101
+ processName: 'com.harnessplayground',
102
+ rawLines: ['Fatal signal 11 (SIGSEGV)'],
103
+ },
104
+ });
105
+
106
+ await expectation;
107
+ await supervisor.dispose();
108
+ });
109
+
110
+ it('treats polling exit events as immediately confirmed crashes', async () => {
111
+ const monitor = createMockAppMonitor();
112
+ const isAppRunning = vi.fn().mockResolvedValue(true);
113
+ const supervisor = createCrashSupervisor({
114
+ appMonitor: monitor.appMonitor,
115
+ platformRunner: createPlatformRunner({
116
+ isAppRunning,
117
+ }),
118
+ });
119
+
120
+ supervisor.beginLaunch('/tmp/polling-exit.harness.ts');
121
+
122
+ const crashPromise = supervisor.waitForCrash('/tmp/polling-exit.harness.ts');
123
+ monitor.emit({ type: 'app_exited', source: 'polling' });
124
+
125
+ await expect(crashPromise).rejects.toMatchObject({
126
+ name: 'NativeCrashError',
127
+ phase: 'startup',
128
+ });
129
+ expect(isAppRunning).not.toHaveBeenCalled();
130
+ await supervisor.dispose();
131
+ });
132
+
133
+ it('does not reject when monitoring is stopped', async () => {
134
+ const monitor = createMockAppMonitor();
135
+ const supervisor = createCrashSupervisor({
136
+ appMonitor: monitor.appMonitor,
137
+ platformRunner: createPlatformRunner({
138
+ isAppRunning: vi.fn().mockResolvedValue(false),
139
+ }),
140
+ });
141
+
142
+ supervisor.beginTestRun('/tmp/restart.harness.ts');
143
+ await supervisor.stop();
144
+
145
+ const reject = vi.fn();
146
+ void supervisor.waitForCrash('/tmp/restart.harness.ts').catch(reject);
147
+
148
+ monitor.emit({ type: 'app_exited', source: 'polling' });
149
+ await Promise.resolve();
150
+
151
+ expect(reject).not.toHaveBeenCalled();
152
+ await supervisor.dispose();
153
+ });
154
+
155
+ it('prefers crash details from the started app monitor over the runner', async () => {
156
+ const monitorGetCrashDetails = vi.fn().mockResolvedValue({
157
+ source: 'logs',
158
+ summary: 'full crash block',
159
+ rawLines: ['full crash block'],
160
+ });
161
+ const runnerGetCrashDetails = vi.fn().mockResolvedValue({
162
+ source: 'logs',
163
+ summary: 'runner details',
164
+ rawLines: ['runner details'],
165
+ });
166
+ const monitor = createMockAppMonitor({
167
+ getCrashDetails: monitorGetCrashDetails,
168
+ });
169
+ const supervisor = createCrashSupervisor({
170
+ appMonitor: monitor.appMonitor,
171
+ platformRunner: createPlatformRunner({
172
+ isAppRunning: vi.fn().mockResolvedValue(false),
173
+ getCrashDetails: runnerGetCrashDetails,
174
+ }),
175
+ });
176
+
177
+ supervisor.beginTestRun('/tmp/monitor-details.harness.ts');
178
+
179
+ const crashPromise = supervisor.waitForCrash('/tmp/monitor-details.harness.ts');
180
+ monitor.emit({
181
+ type: 'possible_crash',
182
+ source: 'logs',
183
+ crashDetails: {
184
+ source: 'logs',
185
+ processName: 'com.harnessplayground',
186
+ pid: 1234,
187
+ rawLines: ['partial line'],
188
+ },
189
+ });
190
+
191
+ await expect(crashPromise).rejects.toMatchObject({
192
+ details: {
193
+ summary: 'full crash block',
194
+ },
195
+ });
196
+ expect(monitorGetCrashDetails).toHaveBeenCalled();
197
+ expect(runnerGetCrashDetails).not.toHaveBeenCalled();
198
+ await supervisor.dispose();
199
+ });
200
+ });
@@ -0,0 +1,70 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { NativeCrashError } from '../errors.js';
3
+
4
+ describe('NativeCrashError', () => {
5
+ it('formats the extracted stack trace in the error message', () => {
6
+ const error = new NativeCrashError('/tmp/crash.harness.ts', {
7
+ phase: 'execution',
8
+ processName: 'HarnessPlayground',
9
+ pid: 1234,
10
+ signal: 'SIGTRAP',
11
+ exceptionType: 'EXC_BREAKPOINT',
12
+ stackTrace: [
13
+ '0 AppDelegate.crashIfRequested() (AppDelegate.swift:31)',
14
+ '1 AppDelegate.application(_:didFinishLaunchingWithOptions:) (AppDelegate.swift:56)',
15
+ ],
16
+ });
17
+
18
+ expect(error.message).toContain(
19
+ ' 0 AppDelegate.crashIfRequested() (AppDelegate.swift:31)'
20
+ );
21
+ expect(error.message).toContain(
22
+ ' 1 AppDelegate.application(_:didFinishLaunchingWithOptions:) (AppDelegate.swift:56)'
23
+ );
24
+ });
25
+
26
+ it('omits single-line iOS summaries from the rendered error message', () => {
27
+ const error = new NativeCrashError('/tmp/crash.harness.ts', {
28
+ phase: 'startup',
29
+ artifactType: 'ios-crash-report',
30
+ summary:
31
+ '2026-03-12 13:46:18.154 Df HarnessPlayground[18007:65e716] [com.apple.dt.xctest:Default] notify_get_state check indicated test daemon not ready.',
32
+ processName: 'HarnessPlayground',
33
+ pid: 18007,
34
+ signal: 'SIGABRT',
35
+ exceptionType: 'EXC_CRASH',
36
+ });
37
+
38
+ expect(error.message).not.toContain('notify_get_state check indicated test daemon not ready');
39
+ expect(error.message).toContain('Signal: SIGABRT');
40
+ expect(error.message).toContain('Exception: EXC_CRASH');
41
+ expect(error.message).toContain('Process: HarnessPlayground (pid 18007)');
42
+ });
43
+
44
+ it('does not duplicate stack frames when the summary already contains a crash block', () => {
45
+ const frame =
46
+ '03-13 07:59:44.943 20373 20373 E AndroidRuntime: \tat com.harnessplayground.MainActivity.onCreate(MainActivity.kt:38)';
47
+ const error = new NativeCrashError('/tmp/crash.harness.ts', {
48
+ phase: 'startup',
49
+ summary: [
50
+ '--------- beginning of crash',
51
+ '03-13 07:59:44.943 20373 20373 E AndroidRuntime: FATAL EXCEPTION: main',
52
+ frame,
53
+ ].join('\n'),
54
+ stackTrace: [frame],
55
+ });
56
+
57
+ expect(error.message.match(/MainActivity\.onCreate\(MainActivity\.kt:38\)/g)).toHaveLength(
58
+ 1
59
+ );
60
+ });
61
+
62
+ it('collapses the native crash stack header so jest does not reprint multiline messages', () => {
63
+ const error = new NativeCrashError('/tmp/crash.harness.ts', {
64
+ phase: 'startup',
65
+ summary: ['line one', 'line two'].join('\n'),
66
+ });
67
+
68
+ expect(error.stack).toBe('NativeCrashError: The native app crashed while preparing to run this test file.');
69
+ });
70
+ });
@@ -0,0 +1,129 @@
1
+ import { EventEmitter } from 'node:events';
2
+ import { describe, expect, it, vi } from 'vitest';
3
+ import { waitForAppReady } from '../harness.js';
4
+ import type { BridgeServer } from '@react-native-harness/bridge/server';
5
+ import type {
6
+ AppMonitor,
7
+ AppMonitorListener,
8
+ HarnessPlatformRunner,
9
+ } from '@react-native-harness/platforms';
10
+ import { createCrashSupervisor } from '../crash-supervisor.js';
11
+
12
+ const createBridgeServer = () => {
13
+ const emitter = new EventEmitter();
14
+
15
+ return {
16
+ serverBridge: {
17
+ on: emitter.on.bind(emitter),
18
+ once: emitter.once.bind(emitter),
19
+ off: emitter.off.bind(emitter),
20
+ } as unknown as BridgeServer,
21
+ emitReady: () => {
22
+ emitter.emit('ready');
23
+ },
24
+ };
25
+ };
26
+
27
+ const createAppMonitor = (): AppMonitor => {
28
+ const listeners = new Set<AppMonitorListener>();
29
+
30
+ return {
31
+ start: async () => undefined,
32
+ stop: async () => undefined,
33
+ dispose: async () => undefined,
34
+ addListener: (listener) => {
35
+ listeners.add(listener);
36
+ },
37
+ removeListener: (listener) => {
38
+ listeners.delete(listener);
39
+ },
40
+ };
41
+ };
42
+
43
+ const createPlatformRunner = (
44
+ restartApp: HarnessPlatformRunner['restartApp']
45
+ ): HarnessPlatformRunner => ({
46
+ startApp: async () => undefined,
47
+ restartApp,
48
+ stopApp: async () => undefined,
49
+ dispose: async () => undefined,
50
+ isAppRunning: async () => true,
51
+ createAppMonitor: createAppMonitor,
52
+ });
53
+
54
+ describe('waitForAppReady', () => {
55
+ it('passes launch options to the initial launch', async () => {
56
+ const { serverBridge, emitReady } = createBridgeServer();
57
+ const restartApp = vi.fn().mockResolvedValue(undefined);
58
+ const platformInstance = createPlatformRunner(restartApp);
59
+ const crashSupervisor = createCrashSupervisor({
60
+ appMonitor: createAppMonitor(),
61
+ platformRunner: platformInstance,
62
+ });
63
+
64
+ const promise = waitForAppReady({
65
+ serverBridge,
66
+ platformInstance,
67
+ bridgeTimeout: 5000,
68
+ testFilePath: '/tmp/test.harness.ts',
69
+ crashSupervisor,
70
+ appLaunchOptions: {
71
+ extras: {
72
+ mode: 'startup',
73
+ },
74
+ },
75
+ });
76
+
77
+ await Promise.resolve();
78
+ expect(restartApp).toHaveBeenCalledWith({
79
+ extras: {
80
+ mode: 'startup',
81
+ },
82
+ });
83
+
84
+ emitReady();
85
+ await promise;
86
+ await crashSupervisor.dispose();
87
+ });
88
+
89
+ it('does not retry launch when the app never becomes ready', async () => {
90
+ vi.useFakeTimers();
91
+
92
+ const { serverBridge } = createBridgeServer();
93
+ const restartApp = vi.fn().mockResolvedValue(undefined);
94
+ const platformInstance = createPlatformRunner(restartApp);
95
+ const crashSupervisor = createCrashSupervisor({
96
+ appMonitor: createAppMonitor(),
97
+ platformRunner: platformInstance,
98
+ });
99
+
100
+ const promise = waitForAppReady({
101
+ serverBridge,
102
+ platformInstance,
103
+ bridgeTimeout: 1000,
104
+ testFilePath: '/tmp/test.harness.ts',
105
+ crashSupervisor,
106
+ appLaunchOptions: {
107
+ extras: {
108
+ mode: 'startup',
109
+ },
110
+ },
111
+ });
112
+
113
+ await Promise.resolve();
114
+ await vi.advanceTimersByTimeAsync(1000);
115
+
116
+ await expect(promise).rejects.toMatchObject({
117
+ name: 'AbortError',
118
+ });
119
+ expect(restartApp).toHaveBeenCalledTimes(1);
120
+ expect(restartApp).toHaveBeenCalledWith({
121
+ extras: {
122
+ mode: 'startup',
123
+ },
124
+ });
125
+
126
+ await crashSupervisor.dispose();
127
+ vi.useRealTimers();
128
+ });
129
+ });