@react-native-harness/jest 1.1.0-rc.1 → 1.1.0-rc.3
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/dist/__tests__/errors.test.js +6 -1
- package/dist/__tests__/harness-cache.test.d.ts +2 -0
- package/dist/__tests__/harness-cache.test.d.ts.map +1 -0
- package/dist/__tests__/harness-cache.test.js +51 -0
- package/dist/__tests__/harness.test.js +484 -47
- package/dist/__tests__/index.test.d.ts +2 -0
- package/dist/__tests__/index.test.d.ts.map +1 -0
- package/dist/__tests__/index.test.js +42 -0
- package/dist/__tests__/resource-lock.test.d.ts +2 -0
- package/dist/__tests__/resource-lock.test.d.ts.map +1 -0
- package/dist/__tests__/resource-lock.test.js +103 -0
- package/dist/action-hooks.d.ts +9 -0
- package/dist/action-hooks.d.ts.map +1 -0
- package/dist/action-hooks.js +36 -0
- package/dist/cli-args.d.ts +1 -0
- package/dist/cli-args.d.ts.map +1 -1
- package/dist/cli-args.js +5 -0
- package/dist/crash-supervisor.d.ts.map +1 -1
- package/dist/crash-supervisor.js +7 -6
- package/dist/errors.d.ts +5 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +10 -2
- package/dist/harness.d.ts +23 -1
- package/dist/harness.d.ts.map +1 -1
- package/dist/harness.js +574 -136
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +205 -58
- package/dist/logs.d.ts +4 -0
- package/dist/logs.d.ts.map +1 -1
- package/dist/logs.js +12 -0
- package/dist/resource-lock.d.ts +25 -0
- package/dist/resource-lock.d.ts.map +1 -0
- package/dist/resource-lock.js +278 -0
- package/dist/run.d.ts +7 -1
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +13 -8
- package/dist/setup.d.ts.map +1 -1
- package/dist/setup.js +29 -2
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/package.json +7 -6
- package/src/__tests__/errors.test.ts +18 -6
- package/src/__tests__/harness-cache.test.ts +74 -0
- package/src/__tests__/harness.test.ts +702 -49
- package/src/__tests__/index.test.ts +61 -0
- package/src/__tests__/resource-lock.test.ts +138 -0
- package/src/action-hooks.ts +66 -0
- package/src/cli-args.ts +6 -0
- package/src/crash-supervisor.ts +13 -6
- package/src/env.d.ts +1 -1
- package/src/errors.ts +15 -4
- package/src/harness.ts +743 -157
- package/src/index.ts +276 -64
- package/src/logs.ts +17 -3
- package/src/resource-lock.ts +450 -0
- package/src/run.ts +20 -10
- package/src/setup.ts +42 -1
- package/tsconfig.json +3 -3
- package/tsconfig.lib.json +6 -6
package/dist/harness.js
CHANGED
|
@@ -1,163 +1,601 @@
|
|
|
1
1
|
import { getBridgeServer, } from '@react-native-harness/bridge/server';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { HARNESS_BRIDGE_PATH, } from '@react-native-harness/bridge';
|
|
3
|
+
import { getMetroInstance, isMetroCacheReusable, waitForMetroBackedAppReady, } from '@react-native-harness/bundler-metro';
|
|
4
|
+
import { createHarnessPluginManager, } from '@react-native-harness/plugins';
|
|
5
|
+
import { logger, createCrashArtifactWriter, getTimeoutSignal, raceAbortSignals, } from '@react-native-harness/tools';
|
|
6
|
+
import { PlatformReadyTimeoutError } from './errors.js';
|
|
5
7
|
import { createCrashSupervisor, } from './crash-supervisor.js';
|
|
6
8
|
import { createClientLogListener } from './client-log-handler.js';
|
|
7
|
-
import
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { logMetroCacheReused, logRunnerStarting, logRunnerStillWaitingInQueue, logRunnerWaitingInQueue, } from './logs.js';
|
|
11
|
+
import { createResourceLockManager } from './resource-lock.js';
|
|
12
|
+
const harnessLogger = logger.child('runtime');
|
|
13
|
+
const resourceLockManager = createResourceLockManager();
|
|
14
|
+
export const maybeLogMetroCacheReuse = (config, platform, projectRoot) => {
|
|
15
|
+
if (config.unstable__enableMetroCache && isMetroCacheReusable(projectRoot)) {
|
|
16
|
+
logMetroCacheReused(platform);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
const createAbortError = () => new DOMException('The operation was aborted', 'AbortError');
|
|
20
|
+
const getDefaultResourceLockKey = (platform) => `${platform.platformId}:${platform.name}`;
|
|
21
|
+
const waitForAbort = (signal) => {
|
|
22
|
+
if (signal.aborted) {
|
|
23
|
+
return Promise.reject(signal.reason ?? createAbortError());
|
|
24
|
+
}
|
|
25
|
+
return new Promise((_, reject) => {
|
|
26
|
+
signal.addEventListener('abort', () => {
|
|
27
|
+
reject(signal.reason ?? createAbortError());
|
|
28
|
+
}, { once: true });
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
const withPlatformReadyTimeout = async (options) => {
|
|
32
|
+
const timeoutSignal = getTimeoutSignal(options.timeout);
|
|
33
|
+
const combinedSignal = raceAbortSignals([options.signal, timeoutSignal]);
|
|
34
|
+
try {
|
|
35
|
+
return await options.work(combinedSignal);
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
if (error instanceof DOMException &&
|
|
39
|
+
error.name === 'AbortError' &&
|
|
40
|
+
timeoutSignal.aborted &&
|
|
41
|
+
!options.signal.aborted) {
|
|
42
|
+
throw new PlatformReadyTimeoutError(options.timeout);
|
|
43
|
+
}
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
8
47
|
export const waitForAppReady = async (options) => {
|
|
9
|
-
const { serverBridge, platformInstance,
|
|
10
|
-
const signal =
|
|
11
|
-
|
|
12
|
-
|
|
48
|
+
const { metroInstance, serverBridge, platformInstance, platformId, bundleStartTimeout, readyTimeout, maxAppRestarts, testFilePath, crashSupervisor, appLaunchOptions, launchApp = () => platformInstance.restartApp(appLaunchOptions), } = options;
|
|
49
|
+
const signal = options.signal ?? new AbortController().signal;
|
|
50
|
+
const logWait = (message, ...args) => {
|
|
51
|
+
harnessLogger.debug(`waitForAppReady: ${message}`, ...args);
|
|
52
|
+
};
|
|
53
|
+
return await waitForMetroBackedAppReady({
|
|
54
|
+
metro: metroInstance,
|
|
55
|
+
platformId,
|
|
56
|
+
bundleStartTimeout,
|
|
57
|
+
readyTimeout,
|
|
58
|
+
maxAppRestarts,
|
|
59
|
+
signal,
|
|
60
|
+
startAttempt: async () => {
|
|
61
|
+
logWait('launching app for %s', testFilePath);
|
|
62
|
+
await launchApp();
|
|
63
|
+
logWait('launch request completed, waiting for bridge ready');
|
|
64
|
+
},
|
|
65
|
+
waitForReady: async (signal) => {
|
|
66
|
+
logWait('waiting for runtime ready');
|
|
67
|
+
return await Promise.race([
|
|
68
|
+
new Promise((resolve) => {
|
|
69
|
+
const onReady = () => {
|
|
70
|
+
cleanup();
|
|
71
|
+
crashSupervisor.markReady();
|
|
72
|
+
logWait('runtime ready received');
|
|
73
|
+
resolve();
|
|
74
|
+
};
|
|
75
|
+
const onAbort = () => {
|
|
76
|
+
cleanup();
|
|
77
|
+
};
|
|
78
|
+
const cleanup = () => {
|
|
79
|
+
serverBridge.off('ready', onReady);
|
|
80
|
+
signal.removeEventListener('abort', onAbort);
|
|
81
|
+
};
|
|
82
|
+
serverBridge.on('ready', onReady);
|
|
83
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
84
|
+
}),
|
|
85
|
+
waitForAbort(signal),
|
|
86
|
+
]);
|
|
87
|
+
},
|
|
88
|
+
waitForCrash: async (signal) => {
|
|
89
|
+
try {
|
|
90
|
+
logWait('waiting for crash or runtime ready');
|
|
91
|
+
return await Promise.race([
|
|
92
|
+
crashSupervisor.waitForCrash(testFilePath),
|
|
93
|
+
waitForAbort(signal),
|
|
94
|
+
]);
|
|
95
|
+
}
|
|
96
|
+
finally {
|
|
97
|
+
crashSupervisor.cancelCrashWaiters();
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
onAttemptStart: () => {
|
|
101
|
+
logWait('beginning launch attempt for %s', testFilePath);
|
|
13
102
|
crashSupervisor.beginLaunch(testFilePath);
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
crashSupervisor.markReady();
|
|
18
|
-
cleanup();
|
|
19
|
-
resolve();
|
|
20
|
-
};
|
|
21
|
-
const onAbort = () => {
|
|
22
|
-
cleanup();
|
|
23
|
-
reject(new DOMException('The operation was aborted', 'AbortError'));
|
|
24
|
-
};
|
|
25
|
-
const cleanup = () => {
|
|
26
|
-
serverBridge.off('ready', onReady);
|
|
27
|
-
signal.removeEventListener('abort', onAbort);
|
|
103
|
+
},
|
|
104
|
+
onAttemptReset: () => {
|
|
105
|
+
logWait('resetting launch attempt state');
|
|
28
106
|
crashSupervisor.cancelCrashWaiters();
|
|
29
|
-
}
|
|
30
|
-
signal.addEventListener('abort', onAbort);
|
|
31
|
-
serverBridge.once('ready', onReady);
|
|
32
|
-
void crashSupervisor.waitForCrash(testFilePath).catch((error) => {
|
|
33
|
-
cleanup();
|
|
34
|
-
reject(error);
|
|
35
|
-
});
|
|
36
|
-
void launchApp().catch((error) => {
|
|
37
|
-
cleanup();
|
|
38
|
-
reject(error);
|
|
39
|
-
});
|
|
107
|
+
},
|
|
40
108
|
});
|
|
41
109
|
};
|
|
42
110
|
const getHarnessInternal = async (config, platform, projectRoot, signal) => {
|
|
43
111
|
const context = {
|
|
44
112
|
platform,
|
|
45
113
|
};
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
platformRunner: platformInstance,
|
|
114
|
+
harnessLogger.debug('creating Harness internals for runner=%s platform=%s', platform.name, platform.platformId);
|
|
115
|
+
const resourceLockKey = await (platform.getResourceLockKey?.() ??
|
|
116
|
+
getDefaultResourceLockKey(platform));
|
|
117
|
+
let didWaitForResourceLock = false;
|
|
118
|
+
let lastStillWaitingLogAt = 0;
|
|
119
|
+
const resourceLease = await resourceLockManager.acquire(resourceLockKey, {
|
|
120
|
+
signal,
|
|
121
|
+
onWait: () => {
|
|
122
|
+
didWaitForResourceLock = true;
|
|
123
|
+
logRunnerWaitingInQueue(platform);
|
|
124
|
+
harnessLogger.debug('waiting in queue for runner=%s key=%s', platform.name, resourceLockKey);
|
|
125
|
+
},
|
|
126
|
+
onStillWaiting: (elapsedMs) => {
|
|
127
|
+
if (elapsedMs - lastStillWaitingLogAt < 5000) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
lastStillWaitingLogAt = elapsedMs;
|
|
131
|
+
logRunnerStillWaitingInQueue(platform);
|
|
132
|
+
harnessLogger.debug('still waiting in queue for runner=%s key=%s elapsedMs=%d', platform.name, resourceLockKey, elapsedMs);
|
|
133
|
+
},
|
|
67
134
|
});
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
metroInstance.events.addListener(clientLogListener);
|
|
71
|
-
}
|
|
72
|
-
const dispose = async () => {
|
|
73
|
-
if (config.forwardClientLogs) {
|
|
74
|
-
metroInstance.events.removeListener(clientLogListener);
|
|
75
|
-
}
|
|
76
|
-
serverBridge.off('ready', crashSupervisor.markReady);
|
|
77
|
-
await Promise.all([
|
|
78
|
-
crashSupervisor.dispose(),
|
|
79
|
-
serverBridge.dispose(),
|
|
80
|
-
platformInstance.dispose(),
|
|
81
|
-
metroInstance.dispose(),
|
|
82
|
-
]);
|
|
83
|
-
};
|
|
84
|
-
if (signal.aborted) {
|
|
85
|
-
await dispose();
|
|
86
|
-
throw new DOMException('The operation was aborted', 'AbortError');
|
|
135
|
+
if (didWaitForResourceLock) {
|
|
136
|
+
logRunnerStarting(platform);
|
|
87
137
|
}
|
|
138
|
+
harnessLogger.debug('resource lock acquired for runner=%s key=%s', platform.name, resourceLockKey);
|
|
88
139
|
try {
|
|
89
|
-
|
|
140
|
+
maybeLogMetroCacheReuse(config, platform, projectRoot);
|
|
141
|
+
const pluginAbortController = new AbortController();
|
|
142
|
+
const pluginManager = createHarnessPluginManager({
|
|
143
|
+
plugins: (config.plugins ?? []),
|
|
90
144
|
projectRoot,
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
minify: false,
|
|
95
|
-
signal,
|
|
145
|
+
config,
|
|
146
|
+
runner: platform,
|
|
147
|
+
abortSignal: pluginAbortController.signal,
|
|
96
148
|
});
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
149
|
+
let currentRun = null;
|
|
150
|
+
let activeTestFilePath;
|
|
151
|
+
const pendingHookPromises = new Set();
|
|
152
|
+
let pendingHookError;
|
|
153
|
+
const getCurrentRunId = () => currentRun?.runId;
|
|
154
|
+
const toRelativeTestFilePath = (testFilePath) => testFilePath == null
|
|
155
|
+
? undefined
|
|
156
|
+
: path.relative(projectRoot, testFilePath);
|
|
157
|
+
const setActiveTestFilePath = (testFilePath) => {
|
|
158
|
+
activeTestFilePath = toRelativeTestFilePath(testFilePath);
|
|
159
|
+
};
|
|
160
|
+
const flushPendingHooks = async () => {
|
|
161
|
+
if (pendingHookPromises.size > 0) {
|
|
162
|
+
await Promise.allSettled([...pendingHookPromises]);
|
|
163
|
+
}
|
|
164
|
+
if (pendingHookError !== undefined) {
|
|
165
|
+
const error = pendingHookError;
|
|
166
|
+
pendingHookError = undefined;
|
|
167
|
+
throw error;
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
const trackHook = (promise) => {
|
|
171
|
+
const trackedPromise = promise
|
|
172
|
+
.catch((error) => {
|
|
173
|
+
pendingHookError ??= error;
|
|
174
|
+
})
|
|
175
|
+
.finally(() => {
|
|
176
|
+
pendingHookPromises.delete(trackedPromise);
|
|
177
|
+
});
|
|
178
|
+
pendingHookPromises.add(trackedPromise);
|
|
179
|
+
};
|
|
180
|
+
const scheduleHook = (name, payload) => {
|
|
181
|
+
trackHook(pluginManager.callHook(name, payload));
|
|
182
|
+
};
|
|
183
|
+
const serverBridge = await getBridgeServer({
|
|
184
|
+
noServer: true,
|
|
185
|
+
timeout: config.bridgeTimeout,
|
|
186
|
+
context,
|
|
117
187
|
});
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
await
|
|
121
|
-
|
|
122
|
-
|
|
188
|
+
harnessLogger.debug('starting Metro, platform runner, and bridge initialization');
|
|
189
|
+
harnessLogger.debug('bridge server initialized on Metro websocket path %s', HARNESS_BRIDGE_PATH);
|
|
190
|
+
const [metroInstance, platformInstance] = await (async () => {
|
|
191
|
+
try {
|
|
192
|
+
return await Promise.all([
|
|
193
|
+
getMetroInstance({
|
|
194
|
+
projectRoot,
|
|
195
|
+
harnessConfig: config,
|
|
196
|
+
websocketEndpoints: {
|
|
197
|
+
[HARNESS_BRIDGE_PATH]: serverBridge.ws,
|
|
198
|
+
},
|
|
199
|
+
}, signal).then((instance) => {
|
|
200
|
+
harnessLogger.debug('Metro initialized');
|
|
201
|
+
return instance;
|
|
202
|
+
}),
|
|
203
|
+
withPlatformReadyTimeout({
|
|
204
|
+
timeout: config.platformReadyTimeout,
|
|
205
|
+
signal,
|
|
206
|
+
work: async () => {
|
|
207
|
+
return await import(platform.runner)
|
|
208
|
+
.then((module) => module.default(platform.config, config, {
|
|
209
|
+
signal,
|
|
210
|
+
}))
|
|
211
|
+
.then((instance) => {
|
|
212
|
+
harnessLogger.debug('platform runner initialized');
|
|
213
|
+
return instance;
|
|
214
|
+
});
|
|
215
|
+
},
|
|
216
|
+
}),
|
|
217
|
+
]);
|
|
218
|
+
}
|
|
219
|
+
catch (error) {
|
|
220
|
+
await Promise.allSettled([
|
|
221
|
+
resourceLease.release(),
|
|
222
|
+
serverBridge.dispose(),
|
|
223
|
+
]);
|
|
224
|
+
throw error;
|
|
225
|
+
}
|
|
226
|
+
})();
|
|
227
|
+
const crashArtifactWriter = createCrashArtifactWriter({
|
|
228
|
+
runnerName: platform.name,
|
|
229
|
+
platformId: platform.platformId,
|
|
230
|
+
});
|
|
231
|
+
const appMonitor = platformInstance.createAppMonitor({
|
|
232
|
+
crashArtifactWriter,
|
|
233
|
+
});
|
|
234
|
+
const appLaunchOptions = platform.config.appLaunchOptions;
|
|
235
|
+
const clientLogListener = createClientLogListener();
|
|
236
|
+
const bridgeEventListener = (event) => {
|
|
237
|
+
const runId = getCurrentRunId();
|
|
238
|
+
if (!runId) {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
switch (event.type) {
|
|
242
|
+
case 'collection-started':
|
|
243
|
+
scheduleHook('collection:started', {
|
|
244
|
+
runId,
|
|
245
|
+
file: event.file,
|
|
246
|
+
});
|
|
247
|
+
break;
|
|
248
|
+
case 'collection-finished':
|
|
249
|
+
scheduleHook('collection:finished', {
|
|
250
|
+
runId,
|
|
251
|
+
file: event.file,
|
|
252
|
+
duration: event.duration,
|
|
253
|
+
totalTests: event.totalTests,
|
|
254
|
+
});
|
|
255
|
+
break;
|
|
256
|
+
case 'suite-started':
|
|
257
|
+
scheduleHook('suite:started', {
|
|
258
|
+
runId,
|
|
259
|
+
file: event.file,
|
|
260
|
+
name: event.name,
|
|
261
|
+
});
|
|
262
|
+
break;
|
|
263
|
+
case 'suite-finished':
|
|
264
|
+
scheduleHook('suite:finished', {
|
|
265
|
+
runId,
|
|
266
|
+
file: event.file,
|
|
267
|
+
name: event.name,
|
|
268
|
+
duration: event.duration,
|
|
269
|
+
status: event.status,
|
|
270
|
+
error: event.error,
|
|
271
|
+
});
|
|
272
|
+
break;
|
|
273
|
+
case 'test-started':
|
|
274
|
+
scheduleHook('test:started', {
|
|
275
|
+
runId,
|
|
276
|
+
file: event.file,
|
|
277
|
+
suite: event.suite,
|
|
278
|
+
name: event.name,
|
|
279
|
+
});
|
|
280
|
+
break;
|
|
281
|
+
case 'test-finished':
|
|
282
|
+
scheduleHook('test:finished', {
|
|
283
|
+
runId,
|
|
284
|
+
file: event.file,
|
|
285
|
+
suite: event.suite,
|
|
286
|
+
name: event.name,
|
|
287
|
+
duration: event.duration,
|
|
288
|
+
status: event.status,
|
|
289
|
+
error: event.error,
|
|
290
|
+
});
|
|
291
|
+
break;
|
|
292
|
+
case 'module-bundling-started':
|
|
293
|
+
scheduleHook('metro:bundle-started', {
|
|
294
|
+
runId,
|
|
295
|
+
target: 'module',
|
|
296
|
+
file: event.file,
|
|
297
|
+
});
|
|
298
|
+
break;
|
|
299
|
+
case 'module-bundling-finished':
|
|
300
|
+
scheduleHook('metro:bundle-finished', {
|
|
301
|
+
runId,
|
|
302
|
+
target: 'module',
|
|
303
|
+
file: event.file,
|
|
304
|
+
duration: event.duration,
|
|
305
|
+
});
|
|
306
|
+
break;
|
|
307
|
+
case 'module-bundling-failed':
|
|
308
|
+
scheduleHook('metro:bundle-failed', {
|
|
309
|
+
runId,
|
|
310
|
+
target: 'module',
|
|
311
|
+
file: event.file,
|
|
312
|
+
duration: event.duration,
|
|
313
|
+
error: event.error,
|
|
314
|
+
});
|
|
315
|
+
break;
|
|
316
|
+
case 'setup-file-bundling-started':
|
|
317
|
+
scheduleHook('metro:bundle-started', {
|
|
318
|
+
runId,
|
|
319
|
+
target: 'setupFile',
|
|
320
|
+
file: event.file,
|
|
321
|
+
setupType: event.setupType,
|
|
322
|
+
});
|
|
323
|
+
break;
|
|
324
|
+
case 'setup-file-bundling-finished':
|
|
325
|
+
scheduleHook('metro:bundle-finished', {
|
|
326
|
+
runId,
|
|
327
|
+
target: 'setupFile',
|
|
328
|
+
file: event.file,
|
|
329
|
+
setupType: event.setupType,
|
|
330
|
+
duration: event.duration,
|
|
331
|
+
});
|
|
332
|
+
break;
|
|
333
|
+
case 'setup-file-bundling-failed':
|
|
334
|
+
scheduleHook('metro:bundle-failed', {
|
|
335
|
+
runId,
|
|
336
|
+
target: 'setupFile',
|
|
337
|
+
file: event.file,
|
|
338
|
+
setupType: event.setupType,
|
|
339
|
+
duration: event.duration,
|
|
340
|
+
error: event.error,
|
|
341
|
+
});
|
|
342
|
+
break;
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
const onMetroEvent = (event) => {
|
|
346
|
+
const runId = getCurrentRunId();
|
|
347
|
+
if (runId && event.type === 'client_log') {
|
|
348
|
+
scheduleHook('metro:client-log', {
|
|
349
|
+
runId,
|
|
350
|
+
level: event.level,
|
|
351
|
+
data: event.data,
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
const crashSupervisor = createCrashSupervisor({
|
|
356
|
+
appMonitor,
|
|
357
|
+
platformRunner: platformInstance,
|
|
358
|
+
});
|
|
359
|
+
const onReady = (device) => {
|
|
360
|
+
crashSupervisor.markReady();
|
|
361
|
+
const runId = getCurrentRunId();
|
|
362
|
+
if (!runId) {
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
scheduleHook('runtime:ready', {
|
|
366
|
+
runId,
|
|
367
|
+
device,
|
|
368
|
+
});
|
|
369
|
+
};
|
|
370
|
+
const onDisconnect = () => {
|
|
371
|
+
const runId = getCurrentRunId();
|
|
372
|
+
if (!runId) {
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
scheduleHook('runtime:disconnected', {
|
|
376
|
+
runId,
|
|
377
|
+
reason: 'bridge-disconnected',
|
|
378
|
+
});
|
|
379
|
+
};
|
|
380
|
+
const onAppMonitorEvent = (event) => {
|
|
381
|
+
const runId = getCurrentRunId();
|
|
382
|
+
if (!runId) {
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
if (event.type === 'app_started') {
|
|
386
|
+
scheduleHook('app:started', {
|
|
387
|
+
runId,
|
|
388
|
+
testFile: activeTestFilePath,
|
|
389
|
+
pid: event.pid,
|
|
390
|
+
source: event.source,
|
|
391
|
+
line: event.line,
|
|
392
|
+
});
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
if (event.type === 'app_exited') {
|
|
396
|
+
scheduleHook('app:exited', {
|
|
397
|
+
runId,
|
|
398
|
+
testFile: activeTestFilePath,
|
|
399
|
+
pid: event.pid,
|
|
400
|
+
source: event.source,
|
|
401
|
+
line: event.line,
|
|
402
|
+
isConfirmed: event.isConfirmed,
|
|
403
|
+
crashDetails: event.crashDetails,
|
|
404
|
+
});
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
if (event.type === 'possible_crash') {
|
|
408
|
+
scheduleHook('app:possible-crash', {
|
|
409
|
+
runId,
|
|
410
|
+
testFile: activeTestFilePath,
|
|
411
|
+
pid: event.pid,
|
|
412
|
+
source: event.source,
|
|
413
|
+
line: event.line,
|
|
414
|
+
isConfirmed: event.isConfirmed,
|
|
415
|
+
crashDetails: event.crashDetails,
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
serverBridge.on('ready', onReady);
|
|
420
|
+
serverBridge.on('disconnect', onDisconnect);
|
|
421
|
+
serverBridge.on('event', bridgeEventListener);
|
|
422
|
+
metroInstance.events.addListener(onMetroEvent);
|
|
423
|
+
appMonitor.addListener(onAppMonitorEvent);
|
|
424
|
+
harnessLogger.debug('registered runtime, bridge, and Metro listeners');
|
|
425
|
+
if (config.forwardClientLogs) {
|
|
426
|
+
metroInstance.events.addListener(clientLogListener);
|
|
427
|
+
harnessLogger.debug('client log forwarding enabled');
|
|
428
|
+
}
|
|
429
|
+
const dispose = async (reason = 'normal') => {
|
|
430
|
+
harnessLogger.debug('disposing Harness (reason=%s)', reason);
|
|
431
|
+
let hookError;
|
|
432
|
+
try {
|
|
433
|
+
await flushPendingHooks();
|
|
434
|
+
await pluginManager.callHook('harness:after-run', {
|
|
435
|
+
runId: currentRun?.runId,
|
|
436
|
+
reason,
|
|
437
|
+
summary: currentRun?.summary,
|
|
438
|
+
status: currentRun?.status,
|
|
439
|
+
error: currentRun?.error,
|
|
440
|
+
});
|
|
441
|
+
await flushPendingHooks();
|
|
442
|
+
await pluginManager.callHook('harness:before-dispose', {
|
|
443
|
+
runId: currentRun?.runId,
|
|
444
|
+
reason,
|
|
445
|
+
summary: currentRun?.summary,
|
|
446
|
+
status: currentRun?.status,
|
|
447
|
+
error: currentRun?.error,
|
|
448
|
+
});
|
|
449
|
+
await flushPendingHooks();
|
|
450
|
+
}
|
|
451
|
+
catch (error) {
|
|
452
|
+
hookError = error;
|
|
453
|
+
}
|
|
454
|
+
if (config.forwardClientLogs) {
|
|
455
|
+
metroInstance.events.removeListener(clientLogListener);
|
|
456
|
+
}
|
|
457
|
+
metroInstance.events.removeListener(onMetroEvent);
|
|
458
|
+
appMonitor.removeListener(onAppMonitorEvent);
|
|
459
|
+
serverBridge.off('ready', onReady);
|
|
460
|
+
serverBridge.off('disconnect', onDisconnect);
|
|
461
|
+
serverBridge.off('event', bridgeEventListener);
|
|
462
|
+
let cleanupError;
|
|
463
|
+
try {
|
|
464
|
+
await Promise.all([
|
|
465
|
+
crashSupervisor.dispose(),
|
|
466
|
+
serverBridge.dispose(),
|
|
467
|
+
platformInstance.dispose(),
|
|
468
|
+
metroInstance.dispose(),
|
|
469
|
+
]);
|
|
470
|
+
}
|
|
471
|
+
catch (error) {
|
|
472
|
+
cleanupError = error;
|
|
473
|
+
}
|
|
474
|
+
finally {
|
|
475
|
+
await resourceLease.release();
|
|
476
|
+
pluginAbortController.abort();
|
|
477
|
+
}
|
|
478
|
+
harnessLogger.debug('Harness resources disposed');
|
|
479
|
+
if (hookError) {
|
|
480
|
+
throw hookError;
|
|
481
|
+
}
|
|
482
|
+
if (cleanupError) {
|
|
483
|
+
throw cleanupError;
|
|
484
|
+
}
|
|
485
|
+
};
|
|
486
|
+
if (signal.aborted) {
|
|
487
|
+
await dispose('abort');
|
|
488
|
+
throw new DOMException('The operation was aborted', 'AbortError');
|
|
123
489
|
}
|
|
124
|
-
|
|
125
|
-
await
|
|
490
|
+
try {
|
|
491
|
+
await pluginManager.callHook('harness:before-creation', {
|
|
492
|
+
appLaunchOptions,
|
|
493
|
+
});
|
|
494
|
+
await flushPendingHooks();
|
|
495
|
+
await appMonitor.start();
|
|
496
|
+
harnessLogger.debug('app monitor started');
|
|
497
|
+
await pluginManager.callHook('harness:before-run', {
|
|
498
|
+
appLaunchOptions,
|
|
499
|
+
});
|
|
500
|
+
await flushPendingHooks();
|
|
126
501
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
502
|
+
catch (error) {
|
|
503
|
+
const runState = currentRun;
|
|
504
|
+
if (runState) {
|
|
505
|
+
runState.error = error;
|
|
506
|
+
currentRun = runState;
|
|
507
|
+
}
|
|
508
|
+
await dispose(error instanceof DOMException && error.name === 'AbortError'
|
|
509
|
+
? 'abort'
|
|
510
|
+
: 'error');
|
|
511
|
+
throw error;
|
|
131
512
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
if (
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
513
|
+
const ensureAppReady = async (testFilePath) => {
|
|
514
|
+
await flushPendingHooks();
|
|
515
|
+
setActiveTestFilePath(testFilePath);
|
|
516
|
+
crashSupervisor.setActiveTestFile(testFilePath);
|
|
517
|
+
harnessLogger.debug('ensuring app is ready for %s', testFilePath);
|
|
518
|
+
if (crashSupervisor.isReady() &&
|
|
519
|
+
(await platformInstance.isAppRunning())) {
|
|
520
|
+
harnessLogger.debug('reusing existing ready app for %s', testFilePath);
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
crashSupervisor.reset();
|
|
524
|
+
harnessLogger.debug('app not ready, waiting for launch and runtime readiness');
|
|
525
|
+
await waitForAppReady({
|
|
526
|
+
metroInstance,
|
|
527
|
+
serverBridge,
|
|
528
|
+
platformInstance: platformInstance,
|
|
529
|
+
platformId: platform.platformId,
|
|
530
|
+
bundleStartTimeout: config.bundleStartTimeout ?? 60000,
|
|
531
|
+
readyTimeout: config.bridgeTimeout,
|
|
532
|
+
maxAppRestarts: config.maxAppRestarts ?? 2,
|
|
533
|
+
testFilePath,
|
|
534
|
+
crashSupervisor,
|
|
535
|
+
appLaunchOptions,
|
|
143
536
|
});
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
537
|
+
await flushPendingHooks();
|
|
538
|
+
harnessLogger.debug('app is ready for %s', testFilePath);
|
|
539
|
+
};
|
|
540
|
+
const restart = async (testFilePath) => {
|
|
541
|
+
await flushPendingHooks();
|
|
542
|
+
await crashSupervisor.stop();
|
|
543
|
+
setActiveTestFilePath(testFilePath);
|
|
544
|
+
harnessLogger.debug('restarting app (testFile=%s mode=%s)', testFilePath ?? 'n/a', testFilePath ? 'stop-and-ensure-ready' : 'direct-restart');
|
|
545
|
+
if (testFilePath) {
|
|
546
|
+
harnessLogger.debug('stopping app before restart');
|
|
547
|
+
await platformInstance.stopApp();
|
|
548
|
+
}
|
|
549
|
+
else {
|
|
550
|
+
harnessLogger.debug('requesting direct app restart');
|
|
551
|
+
await platformInstance.restartApp(appLaunchOptions);
|
|
552
|
+
}
|
|
553
|
+
crashSupervisor.reset();
|
|
554
|
+
await crashSupervisor.start();
|
|
555
|
+
if (testFilePath) {
|
|
556
|
+
await ensureAppReady(testFilePath);
|
|
557
|
+
}
|
|
558
|
+
await flushPendingHooks();
|
|
559
|
+
harnessLogger.debug('restart completed');
|
|
560
|
+
};
|
|
561
|
+
return {
|
|
562
|
+
context,
|
|
563
|
+
runTests: async (path, options) => {
|
|
564
|
+
await flushPendingHooks();
|
|
565
|
+
activeTestFilePath = path;
|
|
566
|
+
const client = serverBridge.rpc.clients.at(-1);
|
|
567
|
+
if (!client) {
|
|
568
|
+
throw new Error('No client found');
|
|
569
|
+
}
|
|
570
|
+
harnessLogger.debug('running test file on client: %s', path);
|
|
571
|
+
const result = await client.runTests(path, {
|
|
572
|
+
...options,
|
|
573
|
+
runner: platform.runner,
|
|
574
|
+
});
|
|
575
|
+
await flushPendingHooks();
|
|
576
|
+
return result;
|
|
577
|
+
},
|
|
578
|
+
ensureAppReady,
|
|
579
|
+
restart,
|
|
580
|
+
dispose: () => dispose('normal'),
|
|
581
|
+
crashSupervisor,
|
|
582
|
+
callHook: async (name, payload) => {
|
|
583
|
+
await flushPendingHooks();
|
|
584
|
+
await pluginManager.callHook(name, payload);
|
|
585
|
+
await flushPendingHooks();
|
|
586
|
+
},
|
|
587
|
+
setRunState: (runState) => {
|
|
588
|
+
currentRun = runState;
|
|
589
|
+
},
|
|
590
|
+
getRunState: () => currentRun,
|
|
591
|
+
};
|
|
156
592
|
}
|
|
157
593
|
catch (error) {
|
|
158
|
-
|
|
159
|
-
throw new InitializationTimeoutError();
|
|
160
|
-
}
|
|
594
|
+
await resourceLease.release();
|
|
161
595
|
throw error;
|
|
162
596
|
}
|
|
163
597
|
};
|
|
598
|
+
export const getHarness = async (config, platform, projectRoot) => {
|
|
599
|
+
harnessLogger.debug('creating Harness with platform ready timeout %dms', config.platformReadyTimeout);
|
|
600
|
+
return await getHarnessInternal(config, platform, projectRoot, new AbortController().signal);
|
|
601
|
+
};
|