1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
import { AnyFunction } from "bun";
import { gcTick, hideFromStackTrace } from "harness";
import assertNode from "node:assert";
type DoneCb = (err?: Error) => any;
function noop() {}
export function createTest(path: string) {
const { expect, test, it, describe, beforeAll, afterAll, beforeEach, afterEach } = Bun.jest(path);
hideFromStackTrace(expect);
// Assert
const strictEqual = (...args: Parameters<typeof assertNode.strictEqual>) => {
assertNode.strictEqual(...args);
expect(true).toBe(true);
};
const notStrictEqual = (...args: Parameters<typeof assertNode.notStrictEqual>) => {
assertNode.notStrictEqual(...args);
expect(true).toBe(true);
};
const deepStrictEqual = (...args: Parameters<typeof assertNode.deepStrictEqual>) => {
assertNode.deepStrictEqual(...args);
expect(true).toBe(true);
};
const throws = (...args: Parameters<typeof assertNode.throws>) => {
assertNode.throws(...args);
expect(true).toBe(true);
};
const ok = (...args: Parameters<typeof assertNode.ok>) => {
assertNode.ok(...args);
expect(true).toBe(true);
};
const ifError = (...args: Parameters<typeof assertNode.ifError>) => {
assertNode.ifError(...args);
expect(true).toBe(true);
};
const match = (...args: Parameters<typeof assertNode.match>) => {
assertNode.match(...args);
expect(true).toBe(true);
};
interface NodeAssert {
(args: any): void;
strictEqual: typeof strictEqual;
deepStrictEqual: typeof deepStrictEqual;
notStrictEqual: typeof notStrictEqual;
throws: typeof throws;
ok: typeof ok;
ifError: typeof ifError;
match: typeof match;
}
const assert = function (...args: any[]) {
// @ts-ignore
assertNode(...args);
} as NodeAssert;
hideFromStackTrace(strictEqual);
hideFromStackTrace(notStrictEqual);
hideFromStackTrace(deepStrictEqual);
hideFromStackTrace(throws);
hideFromStackTrace(ok);
hideFromStackTrace(ifError);
hideFromStackTrace(match);
hideFromStackTrace(assert);
Object.assign(assert, {
strictEqual,
deepStrictEqual,
notStrictEqual,
throws,
ok,
ifError,
match,
});
// End assert
const createCallCheckCtx = (done: DoneCb) => {
var timers: Timer[] = [];
const createDone = createDoneDotAll(done, undefined, timers);
// const mustCallChecks = [];
// failed.forEach(function (context) {
// console.log(
// "Mismatched %s function calls. Expected %s, actual %d.",
// context.name,
// context.messageSegment,
// context.actual
// );
// console.log(context.stack.split("\n").slice(2).join("\n"));
// });
// TODO: Implement this to be exact only
function mustCall(fn?: (...args: any[]) => any, exact?: number) {
return mustCallAtLeast(fn!, exact!);
}
function closeTimers() {
timers.forEach(t => clearTimeout(t));
}
function mustNotCall(reason: string = "function should not have been called", optionalCb?: (err?: any) => void) {
const localDone = createDone();
timers.push(setTimeout(() => localDone(), 200));
return () => {
closeTimers();
if (optionalCb) optionalCb.apply(undefined, reason ? [reason] : []);
done(new Error(reason));
};
}
function mustSucceed(fn: () => any, exact?: number) {
return mustCall(function (err, ...args) {
ifError(err);
// @ts-ignore
if (typeof fn === "function") return fn(...(args as []));
}, exact);
}
function mustCallAtLeast(fn: AnyFunction, minimum: number) {
return _mustCallInner(fn, minimum, "minimum");
}
function _mustCallInner(fn: AnyFunction, criteria = 1, field: string) {
// @ts-ignore
if (process._exiting) throw new Error("Cannot use common.mustCall*() in process exit handler");
if (typeof fn === "number") {
criteria = fn;
fn = noop;
} else if (fn === undefined) {
fn = noop;
}
if (typeof criteria !== "number") throw new TypeError(`Invalid ${field} value: ${criteria}`);
let actual = 0;
let expected = criteria;
// mustCallChecks.push(context);
const done = createDone();
const _return = (...args: any[]) => {
try {
// @ts-ignore
const result = fn(...args);
actual++;
if (actual >= expected) {
closeTimers();
done();
}
return result;
} catch (err) {
if (err instanceof Error) done(err);
else if (err?.toString) done(new Error(err?.toString()));
else {
console.error("Unknown error", err);
done(new Error("Unknown error"));
}
closeTimers();
}
};
// Function instances have own properties that may be relevant.
// Let's replicate those properties to the returned function.
// Refs: https://tc39.es/ecma262/#sec-function-instances
Object.defineProperties(_return, {
name: {
value: fn.name,
writable: false,
enumerable: false,
configurable: true,
},
length: {
value: fn.length,
writable: false,
enumerable: false,
configurable: true,
},
});
return _return;
}
return {
mustSucceed,
mustCall,
mustCallAtLeast,
mustNotCall,
closeTimers,
};
};
function createDoneDotAll(done: DoneCb, globalTimeout?: number, timers: Timer[] = []) {
let toComplete = 0;
let completed = 0;
const globalTimer = globalTimeout
? (timers.push(
setTimeout(() => {
console.log("Global Timeout");
done(new Error("Timed out!"));
}, globalTimeout),
),
timers[timers.length - 1])
: undefined;
function createDoneCb(timeout?: number) {
toComplete += 1;
const timer =
timeout !== undefined
? (timers.push(
setTimeout(() => {
console.log("Timeout");
done(new Error("Timed out!"));
}, timeout),
),
timers[timers.length - 1])
: timeout;
return (result?: Error) => {
if (timer) clearTimeout(timer);
if (globalTimer) clearTimeout(globalTimer);
if (result instanceof Error) {
done(result);
return;
}
completed += 1;
if (completed === toComplete) {
done();
}
};
}
return createDoneCb;
}
return {
expect,
test,
it,
describe,
beforeAll,
afterAll,
beforeEach,
afterEach,
createDoneDotAll,
strictEqual,
notStrictEqual,
deepStrictEqual,
throws,
ok,
ifError,
createCallCheckCtx,
match,
assert,
};
}
declare namespace Bun {
function jest(path: string): typeof import("bun:test");
}
|