Skip to content

Commit 244dca8

Browse files
committed
refactor: wip
1 parent 46f69d5 commit 244dca8

28 files changed

Lines changed: 189 additions & 213 deletions

packages/utils/mocks/multiprocess-profiling/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type { TraceEvent } from '../../src/lib/profiler/trace-file.type.js';
77
import { traceEventWalFormat } from '../../src/lib/profiler/wal-json-trace.js';
88
import {
99
asOptions,
10-
markerPayload,
1110
trackEntryPayload,
1211
} from '../../src/lib/user-timing-extensibility-api-utils.js';
1312
import type {

packages/utils/mocks/omit-trace-json.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,22 @@ const uniq = <T>(v: (T | undefined)[]) => [
8888
];
8989
const ctx = (e: TraceEvent[], base = BASE_TS) => ({
9090
pid: new Map(
91-
uniq(e.map(x => x.pid))
91+
[...uniq(e.map(x => x.pid))]
9292
.sort()
93-
.map((v, i) => [v, 10001 + i]),
93+
.map((v, i) => [v, 10_001 + i]),
9494
),
9595
tid: new Map(
96-
uniq(e.map(x => x.tid))
96+
[...uniq(e.map(x => x.tid))]
9797
.sort()
9898
.map((v, i) => [v, i + 1]),
9999
),
100100
ts: new Map(
101-
uniq(e.map(x => x.ts))
101+
[...uniq(e.map(x => x.ts))]
102102
.sort()
103103
.map((v, i) => [v, base + i * 100]),
104104
),
105105
id: new Map(
106-
uniq(e.map(x => x.id2?.local))
106+
[...uniq(e.map(x => x.id2?.local))]
107107
.sort()
108108
.map((v, i) => [v, `0x${(i + 1).toString(16)}`]),
109109
),
@@ -165,7 +165,7 @@ export const normalizeTraceEvents = (
165165
events: TraceEvent[],
166166
{ baseTimestampUs = BASE_TS } = {},
167167
) => {
168-
if (!events.length) return [];
168+
if (events.length === 0) return [];
169169
const decoded = events.map(decodeEvent);
170170
const c = ctx(decoded, baseTimestampUs);
171171
return decoded.map(e => normalizeEvent(e, c));

packages/utils/src/lib/execute-process.int.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ process:complete
129129
throwError: true,
130130
}),
131131
),
132-
).rejects.toThrow('Process failed with exit code 1');
132+
).rejects.toThrowError('Process failed with exit code 1');
133133
expect(logger.debug).toHaveBeenCalledWith(
134134
expect.stringMatching(/process:start.*Error: dummy-error/s),
135135
{ force: true },

packages/utils/src/lib/exit-process.int.test.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('subscribeProcessExit', () => {
2525
});
2626

2727
it('should install event listeners for all expected events', () => {
28-
expect(() => subscribeProcessExit({ onError, onExit })).not.toThrow();
28+
expect(() => subscribeProcessExit({ onError, onExit })).not.toThrowError();
2929

3030
expect(processOnSpy).toHaveBeenCalledWith(
3131
'uncaughtException',
@@ -42,86 +42,79 @@ describe('subscribeProcessExit', () => {
4242
});
4343

4444
it('should call onError with error and kind for uncaughtException', () => {
45-
expect(() => subscribeProcessExit({ onError })).not.toThrow();
45+
expect(() => subscribeProcessExit({ onError })).not.toThrowError();
4646

4747
const testError = new Error('Test uncaught exception');
4848

4949
(process as any).emit('uncaughtException', testError);
5050

51-
expect(onError).toHaveBeenCalledWith(testError, 'uncaughtException');
52-
expect(onError).toHaveBeenCalledOnce();
51+
expect(onError).toHaveBeenCalledExactlyOnceWith(testError, 'uncaughtException');
5352
expect(onExit).not.toHaveBeenCalled();
5453
});
5554

5655
it('should call onError with reason and kind for unhandledRejection', () => {
57-
expect(() => subscribeProcessExit({ onError })).not.toThrow();
56+
expect(() => subscribeProcessExit({ onError })).not.toThrowError();
5857

5958
const testReason = 'Test unhandled rejection';
6059

6160
(process as any).emit('unhandledRejection', testReason);
6261

63-
expect(onError).toHaveBeenCalledWith(testReason, 'unhandledRejection');
64-
expect(onError).toHaveBeenCalledOnce();
62+
expect(onError).toHaveBeenCalledExactlyOnceWith(testReason, 'unhandledRejection');
6563
expect(onExit).not.toHaveBeenCalled();
6664
});
6765

6866
it('should call onExit and exit with code 0 for SIGINT', () => {
69-
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
67+
expect(() => subscribeProcessExit({ onExit })).not.toThrowError();
7068

7169
(process as any).emit('SIGINT');
7270

73-
expect(onExit).toHaveBeenCalledOnce();
74-
expect(onExit).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGINT, {
71+
expect(onExit).toHaveBeenCalledExactlyOnceWith(SIGNAL_EXIT_CODES().SIGINT, {
7572
kind: 'signal',
7673
signal: 'SIGINT',
7774
});
7875
expect(onError).not.toHaveBeenCalled();
7976
});
8077

8178
it('should call onExit and exit with code 0 for SIGTERM', () => {
82-
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
79+
expect(() => subscribeProcessExit({ onExit })).not.toThrowError();
8380

8481
(process as any).emit('SIGTERM');
8582

86-
expect(onExit).toHaveBeenCalledOnce();
87-
expect(onExit).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGTERM, {
83+
expect(onExit).toHaveBeenCalledExactlyOnceWith(SIGNAL_EXIT_CODES().SIGTERM, {
8884
kind: 'signal',
8985
signal: 'SIGTERM',
9086
});
9187
expect(onError).not.toHaveBeenCalled();
9288
});
9389

9490
it('should call onExit and exit with code 0 for SIGQUIT', () => {
95-
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
91+
expect(() => subscribeProcessExit({ onExit })).not.toThrowError();
9692

9793
(process as any).emit('SIGQUIT');
9894

99-
expect(onExit).toHaveBeenCalledOnce();
100-
expect(onExit).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGQUIT, {
95+
expect(onExit).toHaveBeenCalledExactlyOnceWith(SIGNAL_EXIT_CODES().SIGQUIT, {
10196
kind: 'signal',
10297
signal: 'SIGQUIT',
10398
});
10499
expect(onError).not.toHaveBeenCalled();
105100
});
106101

107102
it('should call onExit for successful process termination with exit code 0', () => {
108-
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
103+
expect(() => subscribeProcessExit({ onExit })).not.toThrowError();
109104

110105
(process as any).emit('exit', 0);
111106

112-
expect(onExit).toHaveBeenCalledOnce();
113-
expect(onExit).toHaveBeenCalledWith(0, { kind: 'exit' });
107+
expect(onExit).toHaveBeenCalledExactlyOnceWith(0, { kind: 'exit' });
114108
expect(onError).not.toHaveBeenCalled();
115109
expect(processExitSpy).not.toHaveBeenCalled();
116110
});
117111

118112
it('should call onExit for failed process termination with exit code 1', () => {
119-
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
113+
expect(() => subscribeProcessExit({ onExit })).not.toThrowError();
120114

121115
(process as any).emit('exit', 1);
122116

123-
expect(onExit).toHaveBeenCalledOnce();
124-
expect(onExit).toHaveBeenCalledWith(1, { kind: 'exit' });
117+
expect(onExit).toHaveBeenCalledExactlyOnceWith(1, { kind: 'exit' });
125118
expect(onError).not.toHaveBeenCalled();
126119
expect(processExitSpy).not.toHaveBeenCalled();
127120
});

packages/utils/src/lib/exit-process.unit.test.ts

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('subscribeProcessExit', () => {
2626
});
2727

2828
it('should install event listeners for all expected events', () => {
29-
expect(() => subscribeProcessExit({ onError, onExit })).not.toThrow();
29+
expect(() => subscribeProcessExit({ onError, onExit })).not.toThrowError();
3030

3131
expect(processOnSpy).toHaveBeenCalledWith(
3232
'uncaughtException',
@@ -43,38 +43,35 @@ describe('subscribeProcessExit', () => {
4343
});
4444

4545
it('should call onError with error and kind for uncaughtException', () => {
46-
expect(() => subscribeProcessExit({ onError })).not.toThrow();
46+
expect(() => subscribeProcessExit({ onError })).not.toThrowError();
4747

4848
const testError = new Error('Test uncaught exception');
4949

5050
(process as any).emit('uncaughtException', testError);
5151

52-
expect(onError).toHaveBeenCalledWith(testError, 'uncaughtException');
53-
expect(onError).toHaveBeenCalledOnce();
52+
expect(onError).toHaveBeenCalledExactlyOnceWith(testError, 'uncaughtException');
5453
expect(onExit).not.toHaveBeenCalled();
5554
});
5655

5756
it('should call onError with reason and kind for unhandledRejection', () => {
58-
expect(() => subscribeProcessExit({ onError })).not.toThrow();
57+
expect(() => subscribeProcessExit({ onError })).not.toThrowError();
5958

6059
const testReason = 'Test unhandled rejection';
6160

6261
(process as any).emit('unhandledRejection', testReason);
6362

64-
expect(onError).toHaveBeenCalledWith(testReason, 'unhandledRejection');
65-
expect(onError).toHaveBeenCalledOnce();
63+
expect(onError).toHaveBeenCalledExactlyOnceWith(testReason, 'unhandledRejection');
6664
expect(onExit).not.toHaveBeenCalled();
6765
});
6866

6967
it('should call onExit with correct code and reason for SIGINT', () => {
7068
expect(() =>
7169
subscribeProcessExit({ onExit, exitOnSignal: true }),
72-
).not.toThrow();
70+
).not.toThrowError();
7371

7472
(process as any).emit('SIGINT');
7573

76-
expect(onExit).toHaveBeenCalledOnce();
77-
expect(onExit).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGINT, {
74+
expect(onExit).toHaveBeenCalledExactlyOnceWith(SIGNAL_EXIT_CODES().SIGINT, {
7875
kind: 'signal',
7976
signal: 'SIGINT',
8077
});
@@ -85,12 +82,11 @@ describe('subscribeProcessExit', () => {
8582
it('should call onExit with correct code and reason for SIGTERM', () => {
8683
expect(() =>
8784
subscribeProcessExit({ onExit, exitOnSignal: true }),
88-
).not.toThrow();
85+
).not.toThrowError();
8986

9087
(process as any).emit('SIGTERM');
9188

92-
expect(onExit).toHaveBeenCalledOnce();
93-
expect(onExit).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGTERM, {
89+
expect(onExit).toHaveBeenCalledExactlyOnceWith(SIGNAL_EXIT_CODES().SIGTERM, {
9490
kind: 'signal',
9591
signal: 'SIGTERM',
9692
});
@@ -101,12 +97,11 @@ describe('subscribeProcessExit', () => {
10197
it('should call onExit with correct code and reason for SIGQUIT', () => {
10298
expect(() =>
10399
subscribeProcessExit({ onExit, exitOnSignal: true }),
104-
).not.toThrow();
100+
).not.toThrowError();
105101

106102
(process as any).emit('SIGQUIT');
107103

108-
expect(onExit).toHaveBeenCalledOnce();
109-
expect(onExit).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGQUIT, {
104+
expect(onExit).toHaveBeenCalledExactlyOnceWith(SIGNAL_EXIT_CODES().SIGQUIT, {
110105
kind: 'signal',
111106
signal: 'SIGQUIT',
112107
});
@@ -117,12 +112,11 @@ describe('subscribeProcessExit', () => {
117112
it('should not exit process when exitOnSignal is false', () => {
118113
expect(() =>
119114
subscribeProcessExit({ onExit, exitOnSignal: false }),
120-
).not.toThrow();
115+
).not.toThrowError();
121116

122117
(process as any).emit('SIGINT');
123118

124-
expect(onExit).toHaveBeenCalledOnce();
125-
expect(onExit).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGINT, {
119+
expect(onExit).toHaveBeenCalledExactlyOnceWith(SIGNAL_EXIT_CODES().SIGINT, {
126120
kind: 'signal',
127121
signal: 'SIGINT',
128122
});
@@ -131,12 +125,11 @@ describe('subscribeProcessExit', () => {
131125
});
132126

133127
it('should not exit process when exitOnSignal is not set', () => {
134-
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
128+
expect(() => subscribeProcessExit({ onExit })).not.toThrowError();
135129

136130
(process as any).emit('SIGTERM');
137131

138-
expect(onExit).toHaveBeenCalledOnce();
139-
expect(onExit).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGTERM, {
132+
expect(onExit).toHaveBeenCalledExactlyOnceWith(SIGNAL_EXIT_CODES().SIGTERM, {
140133
kind: 'signal',
141134
signal: 'SIGTERM',
142135
});
@@ -145,33 +138,30 @@ describe('subscribeProcessExit', () => {
145138
});
146139

147140
it('should call onExit with exit code and reason for normal exit', () => {
148-
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
141+
expect(() => subscribeProcessExit({ onExit })).not.toThrowError();
149142

150143
const exitCode = 42;
151144
(process as any).emit('exit', exitCode);
152145

153-
expect(onExit).toHaveBeenCalledOnce();
154-
expect(onExit).toHaveBeenCalledWith(exitCode, { kind: 'exit' });
146+
expect(onExit).toHaveBeenCalledExactlyOnceWith(exitCode, { kind: 'exit' });
155147
expect(onError).not.toHaveBeenCalled();
156148
expect(processExitSpy).not.toHaveBeenCalled();
157149
});
158150

159151
it('should call onExit with fatal reason when exitOnFatal is true', () => {
160152
expect(() =>
161153
subscribeProcessExit({ onError, onExit, exitOnFatal: true }),
162-
).not.toThrow();
154+
).not.toThrowError();
163155

164156
const testError = new Error('Test uncaught exception');
165157

166158
(process as any).emit('uncaughtException', testError);
167159

168-
expect(onError).toHaveBeenCalledWith(testError, 'uncaughtException');
169-
expect(onError).toHaveBeenCalledOnce();
170-
expect(onExit).toHaveBeenCalledWith(1, {
160+
expect(onError).toHaveBeenCalledExactlyOnceWith(testError, 'uncaughtException');
161+
expect(onExit).toHaveBeenCalledExactlyOnceWith(1, {
171162
kind: 'fatal',
172163
fatal: 'uncaughtException',
173164
});
174-
expect(onExit).toHaveBeenCalledOnce();
175165
});
176166

177167
it('should use custom fatalExitCode when exitOnFatal is true', () => {
@@ -182,37 +172,33 @@ describe('subscribeProcessExit', () => {
182172
exitOnFatal: true,
183173
fatalExitCode: 42,
184174
}),
185-
).not.toThrow();
175+
).not.toThrowError();
186176

187177
const testError = new Error('Test uncaught exception');
188178

189179
(process as any).emit('uncaughtException', testError);
190180

191-
expect(onError).toHaveBeenCalledWith(testError, 'uncaughtException');
192-
expect(onError).toHaveBeenCalledOnce();
193-
expect(onExit).toHaveBeenCalledWith(42, {
181+
expect(onError).toHaveBeenCalledExactlyOnceWith(testError, 'uncaughtException');
182+
expect(onExit).toHaveBeenCalledExactlyOnceWith(42, {
194183
kind: 'fatal',
195184
fatal: 'uncaughtException',
196185
});
197-
expect(onExit).toHaveBeenCalledOnce();
198186
});
199187

200188
it('should call onExit with fatal reason for unhandledRejection when exitOnFatal is true', () => {
201189
expect(() =>
202190
subscribeProcessExit({ onError, onExit, exitOnFatal: true }),
203-
).not.toThrow();
191+
).not.toThrowError();
204192

205193
const testReason = 'Test unhandled rejection';
206194

207195
(process as any).emit('unhandledRejection', testReason);
208196

209-
expect(onError).toHaveBeenCalledWith(testReason, 'unhandledRejection');
210-
expect(onError).toHaveBeenCalledOnce();
211-
expect(onExit).toHaveBeenCalledWith(1, {
197+
expect(onError).toHaveBeenCalledExactlyOnceWith(testReason, 'unhandledRejection');
198+
expect(onExit).toHaveBeenCalledExactlyOnceWith(1, {
212199
kind: 'fatal',
213200
fatal: 'unhandledRejection',
214201
});
215-
expect(onExit).toHaveBeenCalledOnce();
216202
});
217203

218204
it('should have correct SIGINT exit code on Windows', () => {
@@ -244,11 +230,10 @@ describe('subscribeProcessExit', () => {
244230
it('should call onExit only once even when close is called multiple times', () => {
245231
expect(() =>
246232
subscribeProcessExit({ onExit, exitOnSignal: true }),
247-
).not.toThrow();
233+
).not.toThrowError();
248234

249235
(process as any).emit('SIGINT');
250-
expect(onExit).toHaveBeenCalledOnce();
251-
expect(onExit).toHaveBeenCalledWith(SIGNAL_EXIT_CODES().SIGINT, {
236+
expect(onExit).toHaveBeenCalledExactlyOnceWith(SIGNAL_EXIT_CODES().SIGINT, {
252237
kind: 'signal',
253238
signal: 'SIGINT',
254239
});

packages/utils/src/lib/file-system.int.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,19 @@ describe('importModule', () => {
4747
it('should throw if the file does not exist', async () => {
4848
await expect(
4949
importModule({ filepath: 'path/to/non-existent-export.mjs' }),
50-
).rejects.toThrow("File 'path/to/non-existent-export.mjs' does not exist");
50+
).rejects.toThrowError("File 'path/to/non-existent-export.mjs' does not exist");
5151
});
5252

5353
it('should throw if path is a directory', async () => {
54-
await expect(importModule({ filepath: mockDir })).rejects.toThrow(
54+
await expect(importModule({ filepath: mockDir })).rejects.toThrowError(
5555
`Expected '${mockDir}' to be a file`,
5656
);
5757
});
5858

5959
it('should throw if file is not valid JS', async () => {
6060
await expect(
6161
importModule({ filepath: path.join(mockDir, 'invalid-js-file.json') }),
62-
).rejects.toThrow(
62+
).rejects.toThrowError(
6363
`${path.join(mockDir, 'invalid-js-file.json')} is not a valid JS file`,
6464
);
6565
});

0 commit comments

Comments
 (0)