@react-native-harness/jest 1.0.0-canary.1766225407244 → 1.0.0
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__/client-log-handler.test.d.ts +2 -0
- package/dist/__tests__/client-log-handler.test.d.ts.map +1 -0
- package/dist/__tests__/client-log-handler.test.js +262 -0
- package/dist/cli-args.test.d.ts +2 -0
- package/dist/cli-args.test.d.ts.map +1 -0
- package/dist/cli-args.test.js +1 -0
- package/dist/client-log-handler.d.ts +23 -0
- package/dist/client-log-handler.d.ts.map +1 -0
- package/dist/client-log-handler.js +84 -0
- package/dist/crash-monitor.d.ts +16 -0
- package/dist/crash-monitor.d.ts.map +1 -0
- package/dist/crash-monitor.js +75 -0
- package/dist/errors.d.ts +8 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +17 -0
- package/dist/event-mapper.d.ts +16 -0
- package/dist/event-mapper.d.ts.map +1 -0
- package/dist/event-mapper.js +120 -0
- package/dist/global-setup.d.ts +3 -0
- package/dist/global-setup.d.ts.map +1 -0
- package/dist/global-setup.js +60 -0
- package/dist/global-teardown.d.ts +3 -0
- package/dist/global-teardown.d.ts.map +1 -0
- package/dist/global-teardown.js +11 -0
- package/dist/harness-reporter.d.ts +8 -0
- package/dist/harness-reporter.d.ts.map +1 -0
- package/dist/harness-reporter.js +12 -0
- package/dist/harness.d.ts +17 -3
- package/dist/harness.d.ts.map +1 -1
- package/dist/harness.js +114 -19
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +39 -8
- package/dist/jest-options.d.ts +2 -0
- package/dist/jest-options.d.ts.map +1 -0
- package/dist/jest-options.js +1 -0
- package/dist/logs.d.ts +1 -0
- package/dist/logs.d.ts.map +1 -1
- package/dist/logs.js +3 -0
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +1 -0
- package/dist/setup.d.ts.map +1 -1
- package/dist/setup.js +6 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/dist/types.d.ts +17 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/eslint.config.mjs +5 -2
- package/package.json +6 -7
- package/src/__tests__/client-log-handler.test.ts +328 -0
- package/src/client-log-handler.ts +101 -0
- package/src/crash-monitor.ts +109 -0
- package/src/errors.ts +20 -0
- package/src/harness.ts +173 -29
- package/src/index.ts +46 -10
- package/src/logs.ts +6 -0
- package/src/run.ts +1 -0
- package/src/setup.ts +8 -0
- package/tsconfig.json +3 -3
- package/vite.config.ts +18 -0
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { TestContext, TestResult } from '@jest/test-result';
|
|
2
|
+
import type { Config, TestRunnerOptions } from 'jest-runner';
|
|
3
|
+
import type { CustomEnvironment } from '@react-native-harness/cli';
|
|
4
|
+
export type RunTestOptions<ExtraOptions extends Record<string, unknown> = Record<string, unknown>> = {
|
|
5
|
+
config: Config.ProjectConfig;
|
|
6
|
+
extraOptions: ExtraOptions;
|
|
7
|
+
globalConfig: Config.GlobalConfig;
|
|
8
|
+
rawModuleMap: ReturnType<TestContext['moduleMap']['getRawModuleMap']> | null;
|
|
9
|
+
options: TestRunnerOptions;
|
|
10
|
+
testPath: string;
|
|
11
|
+
};
|
|
12
|
+
export type RunTest<ExtraOptions extends Record<string, unknown> = Record<string, unknown>> = (options: RunTestOptions<ExtraOptions>, environment: CustomEnvironment) => TestResult | Promise<TestResult>;
|
|
13
|
+
export interface TestDetail {
|
|
14
|
+
title: string;
|
|
15
|
+
path: string;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACjE,OAAO,KAAK,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAC7D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAEnE,MAAM,MAAM,cAAc,CACxB,YAAY,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IACpE;IACF,MAAM,EAAE,MAAM,CAAC,aAAa,CAAC;IAC7B,YAAY,EAAE,YAAY,CAAC;IAC3B,YAAY,EAAE,MAAM,CAAC,YAAY,CAAC;IAClC,YAAY,EAAE,UAAU,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC,iBAAiB,CAAC,CAAC,GAAG,IAAI,CAAC;IAC7E,OAAO,EAAE,iBAAiB,CAAC;IAC3B,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,OAAO,CACjB,YAAY,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IACpE,CACF,OAAO,EAAE,cAAc,CAAC,YAAY,CAAC,EACrC,WAAW,EAAE,iBAAiB,KAC3B,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;AAEtC,MAAM,WAAW,UAAU;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;CACd"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/eslint.config.mjs
CHANGED
|
@@ -8,9 +8,12 @@ export default [
|
|
|
8
8
|
'@nx/dependency-checks': [
|
|
9
9
|
'error',
|
|
10
10
|
{
|
|
11
|
-
ignoredFiles: [
|
|
11
|
+
ignoredFiles: [
|
|
12
|
+
'{projectRoot}/eslint.config.{js,cjs,mjs,ts,cts,mts}',
|
|
13
|
+
'{projectRoot}/vite.config.{js,cjs,mjs,ts,cts,mts}',
|
|
14
|
+
],
|
|
12
15
|
// jest-runner: we only ingest types
|
|
13
|
-
ignoredDependencies: ['@react-native-harness/cli', 'jest-runner'],
|
|
16
|
+
ignoredDependencies: ['@react-native-harness/cli', 'jest-runner', 'vitest'],
|
|
14
17
|
},
|
|
15
18
|
],
|
|
16
19
|
},
|
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.0.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -38,14 +38,13 @@
|
|
|
38
38
|
"jest-message-util": "^30.2.0",
|
|
39
39
|
"jest-util": "^30.2.0",
|
|
40
40
|
"p-limit": "^7.1.1",
|
|
41
|
-
"p-retry": "^7.1.0",
|
|
42
41
|
"tslib": "^2.3.0",
|
|
43
42
|
"yargs": "^17.7.2",
|
|
44
|
-
"@react-native-harness/
|
|
45
|
-
"@react-native-harness/
|
|
46
|
-
"@react-native-harness/
|
|
47
|
-
"@react-native-harness/
|
|
48
|
-
"@react-native-harness/
|
|
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"
|
|
49
48
|
},
|
|
50
49
|
"devDependencies": {
|
|
51
50
|
"@types/yargs": "^17.0.32",
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
formatClientLogMessage,
|
|
4
|
+
formatClientLogLine,
|
|
5
|
+
handleClientLogEvent,
|
|
6
|
+
createClientLogListener,
|
|
7
|
+
type ClientLogEvent,
|
|
8
|
+
} from '../client-log-handler.js';
|
|
9
|
+
|
|
10
|
+
// Mock the log function
|
|
11
|
+
vi.mock('../logs.js', () => ({
|
|
12
|
+
log: vi.fn(),
|
|
13
|
+
}));
|
|
14
|
+
|
|
15
|
+
import { log } from '../logs.js';
|
|
16
|
+
|
|
17
|
+
describe('client-log-handler', () => {
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
vi.clearAllMocks();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('formatClientLogMessage', () => {
|
|
23
|
+
it('should format a single string', () => {
|
|
24
|
+
const result = formatClientLogMessage(['Hello, world!']);
|
|
25
|
+
expect(result).toBe('Hello, world!');
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('should join multiple strings with spaces', () => {
|
|
29
|
+
const result = formatClientLogMessage(['Hello', 'world', '!']);
|
|
30
|
+
expect(result).toBe('Hello world !');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('should format objects', () => {
|
|
34
|
+
const result = formatClientLogMessage([{ key: 'value' }]);
|
|
35
|
+
// util.format uses inspect-style output for objects
|
|
36
|
+
expect(result).toContain('key');
|
|
37
|
+
expect(result).toContain('value');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('should format arrays', () => {
|
|
41
|
+
const result = formatClientLogMessage([[1, 2, 3]]);
|
|
42
|
+
expect(result).toContain('1');
|
|
43
|
+
expect(result).toContain('2');
|
|
44
|
+
expect(result).toContain('3');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('should handle mixed types', () => {
|
|
48
|
+
const result = formatClientLogMessage([
|
|
49
|
+
'Message:',
|
|
50
|
+
{ count: 42 },
|
|
51
|
+
'items',
|
|
52
|
+
]);
|
|
53
|
+
expect(result).toContain('Message:');
|
|
54
|
+
expect(result).toContain('count');
|
|
55
|
+
expect(result).toContain('42');
|
|
56
|
+
expect(result).toContain('items');
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('should format numbers', () => {
|
|
60
|
+
const result = formatClientLogMessage([123, 456]);
|
|
61
|
+
expect(result).toBe('123 456');
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('should format booleans', () => {
|
|
65
|
+
const result = formatClientLogMessage([true, false]);
|
|
66
|
+
expect(result).toBe('true false');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('should format null and undefined', () => {
|
|
70
|
+
const result = formatClientLogMessage([null, undefined]);
|
|
71
|
+
expect(result).toBe('null undefined');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('should handle empty array', () => {
|
|
75
|
+
const result = formatClientLogMessage([]);
|
|
76
|
+
expect(result).toBe('');
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe('printf-style format specifiers', () => {
|
|
80
|
+
it('should handle %s string substitution', () => {
|
|
81
|
+
const result = formatClientLogMessage(['%s world', 'hello']);
|
|
82
|
+
expect(result).toBe('hello world');
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('should handle %d integer substitution', () => {
|
|
86
|
+
const result = formatClientLogMessage(['Count: %d', 42]);
|
|
87
|
+
expect(result).toBe('Count: 42');
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('should handle %i integer substitution', () => {
|
|
91
|
+
const result = formatClientLogMessage(['Value: %i', 123]);
|
|
92
|
+
expect(result).toBe('Value: 123');
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('should handle %f float substitution', () => {
|
|
96
|
+
const result = formatClientLogMessage(['Pi: %f', 3.14159]);
|
|
97
|
+
expect(result).toContain('3.14159');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should handle multiple substitutions', () => {
|
|
101
|
+
const result = formatClientLogMessage(['Hello %s, you have %d messages', 'Alice', 5]);
|
|
102
|
+
expect(result).toBe('Hello Alice, you have 5 messages');
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('should handle %j JSON substitution', () => {
|
|
106
|
+
const result = formatClientLogMessage(['Data: %j', { key: 'value' }]);
|
|
107
|
+
expect(result).toBe('Data: {"key":"value"}');
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('should handle %o object substitution', () => {
|
|
111
|
+
const result = formatClientLogMessage(['Object: %o', { a: 1 }]);
|
|
112
|
+
// %o produces inspect-style output, just check it contains the key
|
|
113
|
+
expect(result).toContain('a');
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('should handle %% as literal percent when substituting', () => {
|
|
117
|
+
// %% is only converted to % when there are substitutions
|
|
118
|
+
const result = formatClientLogMessage(['%s is 100%% complete', 'Task']);
|
|
119
|
+
expect(result).toBe('Task is 100% complete');
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('should append extra arguments after substitution', () => {
|
|
123
|
+
const result = formatClientLogMessage(['Hello %s', 'world', 'extra', 'args']);
|
|
124
|
+
expect(result).toBe('Hello world extra args');
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe('formatClientLogLine', () => {
|
|
130
|
+
it('should format log level event', () => {
|
|
131
|
+
const event: ClientLogEvent = {
|
|
132
|
+
type: 'client_log',
|
|
133
|
+
level: 'log',
|
|
134
|
+
data: ['Test message'],
|
|
135
|
+
};
|
|
136
|
+
const result = formatClientLogLine(event);
|
|
137
|
+
// The result will contain ANSI codes for styling, so we check it contains the message
|
|
138
|
+
expect(result).toContain('Test message');
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('should format error level event', () => {
|
|
142
|
+
const event: ClientLogEvent = {
|
|
143
|
+
type: 'client_log',
|
|
144
|
+
level: 'error',
|
|
145
|
+
data: ['Error occurred'],
|
|
146
|
+
};
|
|
147
|
+
const result = formatClientLogLine(event);
|
|
148
|
+
expect(result).toContain('Error occurred');
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('should format warn level event', () => {
|
|
152
|
+
const event: ClientLogEvent = {
|
|
153
|
+
type: 'client_log',
|
|
154
|
+
level: 'warn',
|
|
155
|
+
data: ['Warning message'],
|
|
156
|
+
};
|
|
157
|
+
const result = formatClientLogLine(event);
|
|
158
|
+
expect(result).toContain('Warning message');
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('should format info level event', () => {
|
|
162
|
+
const event: ClientLogEvent = {
|
|
163
|
+
type: 'client_log',
|
|
164
|
+
level: 'info',
|
|
165
|
+
data: ['Info message'],
|
|
166
|
+
};
|
|
167
|
+
const result = formatClientLogLine(event);
|
|
168
|
+
expect(result).toContain('Info message');
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('should format debug level event', () => {
|
|
172
|
+
const event: ClientLogEvent = {
|
|
173
|
+
type: 'client_log',
|
|
174
|
+
level: 'debug',
|
|
175
|
+
data: ['Debug message'],
|
|
176
|
+
};
|
|
177
|
+
const result = formatClientLogLine(event);
|
|
178
|
+
expect(result).toContain('Debug message');
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('should format trace level event', () => {
|
|
182
|
+
const event: ClientLogEvent = {
|
|
183
|
+
type: 'client_log',
|
|
184
|
+
level: 'trace',
|
|
185
|
+
data: ['Trace message'],
|
|
186
|
+
};
|
|
187
|
+
const result = formatClientLogLine(event);
|
|
188
|
+
expect(result).toContain('Trace message');
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('should handle multiple data items', () => {
|
|
192
|
+
const event: ClientLogEvent = {
|
|
193
|
+
type: 'client_log',
|
|
194
|
+
level: 'log',
|
|
195
|
+
data: ['User:', { id: 1, name: 'Test' }],
|
|
196
|
+
};
|
|
197
|
+
const result = formatClientLogLine(event);
|
|
198
|
+
expect(result).toContain('User:');
|
|
199
|
+
expect(result).toContain('id');
|
|
200
|
+
expect(result).toContain('Test');
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
describe('handleClientLogEvent', () => {
|
|
205
|
+
it('should handle client_log events and call log', () => {
|
|
206
|
+
const event: ClientLogEvent = {
|
|
207
|
+
type: 'client_log',
|
|
208
|
+
level: 'log',
|
|
209
|
+
data: ['Test message'],
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const result = handleClientLogEvent(event);
|
|
213
|
+
|
|
214
|
+
expect(result).toBe(true);
|
|
215
|
+
expect(log).toHaveBeenCalledTimes(1);
|
|
216
|
+
expect(log).toHaveBeenCalledWith(expect.stringContaining('Test message'));
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('should return false for non-client_log events', () => {
|
|
220
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
221
|
+
const event = { type: 'bundle_build_started' } as any;
|
|
222
|
+
|
|
223
|
+
const result = handleClientLogEvent(event);
|
|
224
|
+
|
|
225
|
+
expect(result).toBe(false);
|
|
226
|
+
expect(log).not.toHaveBeenCalled();
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('should handle error level logs', () => {
|
|
230
|
+
const event: ClientLogEvent = {
|
|
231
|
+
type: 'client_log',
|
|
232
|
+
level: 'error',
|
|
233
|
+
data: ['Something went wrong'],
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
handleClientLogEvent(event);
|
|
237
|
+
|
|
238
|
+
expect(log).toHaveBeenCalledWith(
|
|
239
|
+
expect.stringContaining('Something went wrong')
|
|
240
|
+
);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it('should handle warn level logs', () => {
|
|
244
|
+
const event: ClientLogEvent = {
|
|
245
|
+
type: 'client_log',
|
|
246
|
+
level: 'warn',
|
|
247
|
+
data: ['Deprecation warning'],
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
handleClientLogEvent(event);
|
|
251
|
+
|
|
252
|
+
expect(log).toHaveBeenCalledWith(
|
|
253
|
+
expect.stringContaining('Deprecation warning')
|
|
254
|
+
);
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
describe('createClientLogListener', () => {
|
|
259
|
+
it('should create a listener function', () => {
|
|
260
|
+
const listener = createClientLogListener();
|
|
261
|
+
expect(typeof listener).toBe('function');
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('should handle client_log events when called', () => {
|
|
265
|
+
const listener = createClientLogListener();
|
|
266
|
+
const event: ClientLogEvent = {
|
|
267
|
+
type: 'client_log',
|
|
268
|
+
level: 'info',
|
|
269
|
+
data: ['Listener test'],
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
listener(event);
|
|
273
|
+
|
|
274
|
+
expect(log).toHaveBeenCalledWith(expect.stringContaining('Listener test'));
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it('should ignore non-client_log events', () => {
|
|
278
|
+
const listener = createClientLogListener();
|
|
279
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
280
|
+
const event = { type: 'initialize_done' } as any;
|
|
281
|
+
|
|
282
|
+
listener(event);
|
|
283
|
+
|
|
284
|
+
expect(log).not.toHaveBeenCalled();
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
describe('group events', () => {
|
|
289
|
+
it('should handle group events without logging', () => {
|
|
290
|
+
const event: ClientLogEvent = {
|
|
291
|
+
type: 'client_log',
|
|
292
|
+
level: 'group',
|
|
293
|
+
data: ['Group label'],
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
const result = handleClientLogEvent(event);
|
|
297
|
+
|
|
298
|
+
expect(result).toBe(true);
|
|
299
|
+
expect(log).not.toHaveBeenCalled();
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it('should handle groupCollapsed events without logging', () => {
|
|
303
|
+
const event: ClientLogEvent = {
|
|
304
|
+
type: 'client_log',
|
|
305
|
+
level: 'groupCollapsed',
|
|
306
|
+
data: ['Collapsed'],
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
const result = handleClientLogEvent(event);
|
|
310
|
+
|
|
311
|
+
expect(result).toBe(true);
|
|
312
|
+
expect(log).not.toHaveBeenCalled();
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it('should handle groupEnd events without logging', () => {
|
|
316
|
+
const event: ClientLogEvent = {
|
|
317
|
+
type: 'client_log',
|
|
318
|
+
level: 'groupEnd',
|
|
319
|
+
data: [],
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
const result = handleClientLogEvent(event);
|
|
323
|
+
|
|
324
|
+
expect(result).toBe(true);
|
|
325
|
+
expect(log).not.toHaveBeenCalled();
|
|
326
|
+
});
|
|
327
|
+
});
|
|
328
|
+
});
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { ReportableEvent } from '@react-native-harness/bundler-metro';
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
import util from 'node:util';
|
|
4
|
+
import { log } from './logs.js';
|
|
5
|
+
|
|
6
|
+
export type ClientLogEvent = Extract<ReportableEvent, { type: 'client_log' }>;
|
|
7
|
+
|
|
8
|
+
type LogLevel = ClientLogEvent['level'];
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Gets the display level for a log level.
|
|
12
|
+
* Note: Metro treats 'trace' as 'log' because Hermes doesn't include stack traces.
|
|
13
|
+
*/
|
|
14
|
+
const getDisplayLevel = (level: LogLevel): string => {
|
|
15
|
+
// Metro converts trace to log for display (Hermes doesn't provide stack traces)
|
|
16
|
+
if (level === 'trace') {
|
|
17
|
+
return 'LOG';
|
|
18
|
+
}
|
|
19
|
+
return level.toUpperCase();
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Creates a styled tag for a log level with colored box appearance.
|
|
24
|
+
*/
|
|
25
|
+
const createLevelTag = (level: LogLevel): string => {
|
|
26
|
+
const displayLevel = getDisplayLevel(level);
|
|
27
|
+
const label = ` ${displayLevel} `;
|
|
28
|
+
|
|
29
|
+
if (!chalk.supportsColor) {
|
|
30
|
+
return `[${displayLevel}]`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
switch (level) {
|
|
34
|
+
case 'error':
|
|
35
|
+
return chalk.reset.inverse.bold.red(label);
|
|
36
|
+
case 'warn':
|
|
37
|
+
return chalk.reset.inverse.bold.yellow(label);
|
|
38
|
+
case 'info':
|
|
39
|
+
return chalk.reset.inverse.bold.cyan(label);
|
|
40
|
+
case 'debug':
|
|
41
|
+
return chalk.reset.inverse.bold.blue(label);
|
|
42
|
+
case 'trace':
|
|
43
|
+
// Trace displays as LOG but with a distinct color
|
|
44
|
+
return chalk.reset.inverse.bold.magenta(label);
|
|
45
|
+
case 'log':
|
|
46
|
+
default:
|
|
47
|
+
return chalk.reset.inverse.bold.white(label);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Formats a client log event data array into a string message.
|
|
53
|
+
* Uses util.format for printf-style format specifier support (%s, %d, %j, etc.)
|
|
54
|
+
*/
|
|
55
|
+
export const formatClientLogMessage = (data: unknown[]): string => {
|
|
56
|
+
if (data.length === 0) {
|
|
57
|
+
return '';
|
|
58
|
+
}
|
|
59
|
+
return util.format(...data);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Formats a client log event into a log line with styled level prefix.
|
|
64
|
+
*/
|
|
65
|
+
export const formatClientLogLine = (event: ClientLogEvent): string => {
|
|
66
|
+
const tag = createLevelTag(event.level);
|
|
67
|
+
const message = formatClientLogMessage(event.data);
|
|
68
|
+
return `${tag} ${message}`;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Handles a client_log event by formatting and logging it.
|
|
73
|
+
* Returns true if the event was handled, false otherwise.
|
|
74
|
+
*/
|
|
75
|
+
export const handleClientLogEvent = (event: ReportableEvent): boolean => {
|
|
76
|
+
if (event.type !== 'client_log') {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Skip group events - they don't produce output
|
|
81
|
+
if (
|
|
82
|
+
event.level === 'group' ||
|
|
83
|
+
event.level === 'groupCollapsed' ||
|
|
84
|
+
event.level === 'groupEnd'
|
|
85
|
+
) {
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const logLine = formatClientLogLine(event);
|
|
90
|
+
log(logLine);
|
|
91
|
+
return true;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Creates a client log event listener that can be added to the Metro reporter.
|
|
96
|
+
*/
|
|
97
|
+
export const createClientLogListener = (): ((event: ReportableEvent) => void) => {
|
|
98
|
+
return (event: ReportableEvent) => {
|
|
99
|
+
handleClientLogEvent(event);
|
|
100
|
+
};
|
|
101
|
+
};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { HarnessPlatformRunner } from '@react-native-harness/platforms';
|
|
2
|
+
import { BridgeServer } from '@react-native-harness/bridge/server';
|
|
3
|
+
import { NativeCrashError } from './errors.js';
|
|
4
|
+
import { logger } from '@react-native-harness/tools';
|
|
5
|
+
|
|
6
|
+
export type CrashMonitor = {
|
|
7
|
+
startMonitoring(testFilePath: string): Promise<never>;
|
|
8
|
+
stopMonitoring(): void;
|
|
9
|
+
markIntentionalRestart(): void;
|
|
10
|
+
clearIntentionalRestart(): void;
|
|
11
|
+
dispose(): void;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type CrashMonitorOptions = {
|
|
15
|
+
interval: number;
|
|
16
|
+
platformRunner: HarnessPlatformRunner;
|
|
17
|
+
bridgeServer: BridgeServer;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const createCrashMonitor = ({
|
|
21
|
+
interval,
|
|
22
|
+
platformRunner,
|
|
23
|
+
bridgeServer,
|
|
24
|
+
}: CrashMonitorOptions): CrashMonitor => {
|
|
25
|
+
let pollingInterval: NodeJS.Timeout | null = null;
|
|
26
|
+
let isIntentionalRestart = false;
|
|
27
|
+
let currentTestFilePath: string | null = null;
|
|
28
|
+
let rejectFn: ((error: NativeCrashError) => void) | null = null;
|
|
29
|
+
|
|
30
|
+
const handleDisconnect = () => {
|
|
31
|
+
// Verify if it's actually a crash by checking if app is still running
|
|
32
|
+
if (!isIntentionalRestart && currentTestFilePath) {
|
|
33
|
+
// Capture the value to avoid it being null when setTimeout callback runs
|
|
34
|
+
const testFilePath = currentTestFilePath;
|
|
35
|
+
logger.debug('Bridge disconnected, checking if app crashed');
|
|
36
|
+
// Use a slight delay to allow the OS to clean up the process
|
|
37
|
+
setTimeout(async () => {
|
|
38
|
+
const isRunning = await platformRunner.isAppRunning();
|
|
39
|
+
if (!isRunning && !isIntentionalRestart && rejectFn) {
|
|
40
|
+
logger.debug(`Native crash detected during: ${testFilePath}`);
|
|
41
|
+
rejectFn(new NativeCrashError(testFilePath));
|
|
42
|
+
}
|
|
43
|
+
}, 100);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const startMonitoring = (testFilePath: string): Promise<never> => {
|
|
48
|
+
currentTestFilePath = testFilePath;
|
|
49
|
+
|
|
50
|
+
return new Promise<never>((_, reject) => {
|
|
51
|
+
rejectFn = reject;
|
|
52
|
+
|
|
53
|
+
// Listen for bridge disconnect as early indicator
|
|
54
|
+
bridgeServer.on('disconnect', handleDisconnect);
|
|
55
|
+
|
|
56
|
+
// Poll for app running status
|
|
57
|
+
pollingInterval = setInterval(async () => {
|
|
58
|
+
// Skip check during intentional restarts
|
|
59
|
+
if (isIntentionalRestart) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
const isRunning = await platformRunner.isAppRunning();
|
|
65
|
+
|
|
66
|
+
if (!isRunning && currentTestFilePath) {
|
|
67
|
+
logger.debug(
|
|
68
|
+
`Native crash detected during: ${currentTestFilePath}`
|
|
69
|
+
);
|
|
70
|
+
stopMonitoring();
|
|
71
|
+
reject(new NativeCrashError(currentTestFilePath));
|
|
72
|
+
}
|
|
73
|
+
} catch (error) {
|
|
74
|
+
logger.error('Error checking app status:', error);
|
|
75
|
+
}
|
|
76
|
+
}, interval);
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const stopMonitoring = () => {
|
|
81
|
+
if (pollingInterval) {
|
|
82
|
+
clearInterval(pollingInterval);
|
|
83
|
+
pollingInterval = null;
|
|
84
|
+
}
|
|
85
|
+
bridgeServer.off('disconnect', handleDisconnect);
|
|
86
|
+
currentTestFilePath = null;
|
|
87
|
+
rejectFn = null;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const markIntentionalRestart = () => {
|
|
91
|
+
isIntentionalRestart = true;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const clearIntentionalRestart = () => {
|
|
95
|
+
isIntentionalRestart = false;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const dispose = () => {
|
|
99
|
+
stopMonitoring();
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
startMonitoring,
|
|
104
|
+
stopMonitoring,
|
|
105
|
+
markIntentionalRestart,
|
|
106
|
+
clearIntentionalRestart,
|
|
107
|
+
dispose,
|
|
108
|
+
};
|
|
109
|
+
};
|
package/src/errors.ts
CHANGED
|
@@ -20,3 +20,23 @@ export class InitializationTimeoutError extends HarnessError {
|
|
|
20
20
|
this.name = 'InitializationTimeoutError';
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
|
+
|
|
24
|
+
export class MaxAppRestartsError extends HarnessError {
|
|
25
|
+
constructor(attempts: number) {
|
|
26
|
+
super(
|
|
27
|
+
`App failed to start after ${attempts} attempts. ` +
|
|
28
|
+
`No bundling activity detected within timeout period.`
|
|
29
|
+
);
|
|
30
|
+
this.name = 'MaxAppRestartsError';
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export class NativeCrashError extends HarnessError {
|
|
35
|
+
constructor(
|
|
36
|
+
public readonly testFilePath: string,
|
|
37
|
+
public readonly lastKnownTest?: string
|
|
38
|
+
) {
|
|
39
|
+
super('The native app crashed during test execution.');
|
|
40
|
+
this.name = 'NativeCrashError';
|
|
41
|
+
}
|
|
42
|
+
}
|