Skip to content

Commit a0f383f

Browse files
kt3kbartlomieju
authored andcommitted
fix(ext/node): add assert property to test context object (#28904)
1 parent 48de9ed commit a0f383f

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

ext/node/polyfills/testing.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,44 @@ import { primordials } from "ext:core/mod.js";
44
const {
55
PromisePrototypeThen,
66
ArrayPrototypePush,
7+
ArrayPrototypeForEach,
78
SafePromiseAll,
89
SafePromisePrototypeFinally,
910
} = primordials;
1011
import { notImplemented, warnNotImplemented } from "ext:deno_node/_utils.ts";
12+
import assert from "node:assert";
13+
14+
const methodsToCopy = [
15+
"deepEqual",
16+
"deepStrictEqual",
17+
"doesNotMatch",
18+
"doesNotReject",
19+
"doesNotThrow",
20+
"equal",
21+
"fail",
22+
"ifError",
23+
"match",
24+
"notDeepEqual",
25+
"notDeepStrictEqual",
26+
"notEqual",
27+
"notStrictEqual",
28+
"partialDeepStrictEqual",
29+
"rejects",
30+
"strictEqual",
31+
"throws",
32+
];
33+
34+
/** `assert` object available via t.assert */
35+
let assertObject = undefined;
36+
function getAssertObject() {
37+
if (assertObject === undefined) {
38+
assertObject = { __proto__: null };
39+
ArrayPrototypeForEach(methodsToCopy, (method) => {
40+
assertObject[method] = assert[method];
41+
});
42+
}
43+
return assertObject;
44+
}
1145

1246
export function run() {
1347
notImplemented("test.run");
@@ -22,6 +56,10 @@ class NodeTestContext {
2256
this.#denoContext = t;
2357
}
2458

59+
get assert() {
60+
return getAssertObject();
61+
}
62+
2563
get signal() {
2664
notImplemented("test.TestContext.signal");
2765
return null;

tests/specs/node/node_test_module/test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,29 @@ suite("suite", () => {
130130
});
131131
});
132132

133+
test("assertions available via text context", async (t) => {
134+
assert.strictEqual(t.assert.deepEqual, assert.deepEqual);
135+
assert.strictEqual(t.assert.deepStrictEqual, assert.deepStrictEqual);
136+
assert.strictEqual(t.assert.doesNotMatch, assert.doesNotMatch);
137+
assert.strictEqual(t.assert.doesNotReject, assert.doesNotReject);
138+
assert.strictEqual(t.assert.doesNotThrow, assert.doesNotThrow);
139+
assert.strictEqual(t.assert.equal, assert.equal);
140+
assert.strictEqual(t.assert.fail, assert.fail);
141+
assert.strictEqual(t.assert.ifError, assert.ifError);
142+
assert.strictEqual(t.assert.match, assert.match);
143+
assert.strictEqual(t.assert.notDeepEqual, assert.notDeepEqual);
144+
assert.strictEqual(t.assert.notDeepStrictEqual, assert.notDeepStrictEqual);
145+
assert.strictEqual(t.assert.notEqual, assert.notEqual);
146+
assert.strictEqual(t.assert.notStrictEqual, assert.notStrictEqual);
147+
assert.strictEqual(
148+
t.assert.partialDeepStrictEqual,
149+
assert.partialDeepStrictEqual,
150+
);
151+
assert.strictEqual(t.assert.rejects, assert.rejects);
152+
assert.strictEqual(t.assert.strictEqual, assert.strictEqual);
153+
assert.strictEqual(t.assert.throws, assert.throws);
154+
});
155+
133156
test("unhandled rejection - passes but warns", () => {
134157
Promise.reject(new Error("rejected from unhandled rejection fail"));
135158
});

tests/specs/node/node_test_module/test.out

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[WILDCARD]
2-
running 66 tests from ./test.js
2+
running 67 tests from ./test.js
33
sync pass todo ...
44
------- output -------
55
Warning: Not implemented: test.TestContext.todo
@@ -92,6 +92,7 @@ suite ...
9292
nested test 2 ... ok ([WILDLINE])
9393
sub suite 2 ... FAILED (due to 1 failed step) ([WILDLINE])
9494
suite ... FAILED (due to 3 failed steps) ([WILDLINE])
95+
assertions available via text context ... ok ([WILDLINE])
9596
unhandled rejection - passes but warns ...
9697
Uncaught error from ./test.js FAILED
9798
unhandled rejection - passes but warns ... cancelled ([WILDCARD])
@@ -227,6 +228,6 @@ suite ... sub suite 1 ... nested test 2 => ./test.js:[WILDLINE]
227228
suite ... sub suite 2 ... nested test 1 => ./test.js:[WILDLINE]
228229
./test.js (uncaught error)
229230

230-
FAILED | 11 passed (21 steps) | 52 failed (5 steps) | 4 ignored [WILDCARD]
231+
FAILED | 12 passed (21 steps) | 52 failed (5 steps) | 4 ignored [WILDCARD]
231232

232233
error: Test failed

0 commit comments

Comments
 (0)