Skip to content

Commit ded8a0d

Browse files
schwarzkopfbcaspervonb
authored andcommitted
fix(node): misnamed assert exports (denoland/deno#7123)
1 parent 550a5fb commit ded8a0d

4 files changed

Lines changed: 147 additions & 4 deletions

File tree

node/assert.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@ import {
22
assertEquals,
33
assertNotEquals,
44
assertStrictEquals,
5+
assertNotStrictEquals,
56
assertMatch,
67
assertThrows,
78
} from "../testing/asserts.ts";
89

9-
export { assert, fail } from "../testing/asserts.ts";
10+
export {
11+
assert as default,
12+
assert as ok,
13+
assert,
14+
fail,
15+
} from "../testing/asserts.ts";
1016

11-
export const equal = assertEquals;
12-
export const notEqual = assertNotEquals;
17+
export const deepStrictEqual = assertEquals;
18+
export const notDeepStrictEqual = assertNotEquals;
1319
export const strictEqual = assertStrictEquals;
20+
export const notStrictEqual = assertNotStrictEquals;
1421
export const match = assertMatch;
1522
export const throws = assertThrows;

node/assert_test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import {
2+
assert as denoAssert,
3+
assertEquals,
4+
assertNotEquals,
5+
assertStrictEquals,
6+
assertNotStrictEquals,
7+
assertMatch,
8+
assertThrows,
9+
fail as denoFail,
10+
} from "../testing/asserts.ts";
11+
12+
import assert from "./assert.ts";
13+
14+
import {
15+
ok,
16+
assert as assert_,
17+
deepStrictEqual,
18+
notDeepStrictEqual,
19+
strictEqual,
20+
notStrictEqual,
21+
match,
22+
throws,
23+
fail,
24+
} from "./assert.ts";
25+
26+
Deno.test("API should be exposed", () => {
27+
assertStrictEquals(
28+
assert_,
29+
assert,
30+
"`assert()` should be the default export",
31+
);
32+
assertStrictEquals(assert_, denoAssert, "`assert()` should be exposed");
33+
assertStrictEquals(assert_, ok, "`assert()` should be an alias of `ok()`");
34+
assertStrictEquals(
35+
assertEquals,
36+
deepStrictEqual,
37+
"`assertEquals()` should be exposed as `deepStrictEqual()`",
38+
);
39+
assertStrictEquals(
40+
assertNotEquals,
41+
notDeepStrictEqual,
42+
"`assertNotEquals()` should be exposed as `notDeepStrictEqual()`",
43+
);
44+
assertStrictEquals(
45+
assertStrictEquals,
46+
strictEqual,
47+
"`assertStrictEquals()` should be exposed as `strictEqual()`",
48+
);
49+
assertStrictEquals(
50+
assertNotStrictEquals,
51+
notStrictEqual,
52+
"`assertNotStrictEquals()` should be exposed as `notStrictEqual()`",
53+
);
54+
assertStrictEquals(
55+
assertMatch,
56+
match,
57+
"`assertMatch()` should be exposed as `match()`",
58+
);
59+
assertStrictEquals(
60+
assertThrows,
61+
throws,
62+
"`assertThrows()` should be exposed as `throws()`",
63+
);
64+
assertStrictEquals(fail, denoFail, "`fail()` should be exposed");
65+
});

testing/asserts.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,20 @@ export function assertNotEquals(
242242
* assertStrictEquals(1, 2)
243243
* ```
244244
*/
245+
export function assertStrictEquals(
246+
actual: unknown,
247+
expected: unknown,
248+
msg?: string,
249+
): void;
245250
export function assertStrictEquals<T>(
246251
actual: T,
247252
expected: T,
248253
msg?: string,
254+
): void;
255+
export function assertStrictEquals(
256+
actual: unknown,
257+
expected: unknown,
258+
msg?: string,
249259
): void {
250260
if (actual === expected) {
251261
return;
@@ -285,6 +295,37 @@ export function assertStrictEquals<T>(
285295
throw new AssertionError(message);
286296
}
287297

298+
/**
299+
* Make an assertion that `actual` and `expected` are not strictly equal.
300+
* If the values are strictly equal then throw.
301+
* ```ts
302+
* assertNotStrictEquals(1, 1)
303+
* ```
304+
*/
305+
export function assertNotStrictEquals(
306+
actual: unknown,
307+
expected: unknown,
308+
msg?: string,
309+
): void;
310+
export function assertNotStrictEquals<T>(
311+
actual: T,
312+
expected: T,
313+
msg?: string,
314+
): void;
315+
export function assertNotStrictEquals(
316+
actual: unknown,
317+
expected: unknown,
318+
msg?: string,
319+
): void {
320+
if (actual !== expected) {
321+
return;
322+
}
323+
324+
throw new AssertionError(
325+
msg ?? `Expected "actual" to be strictly unequal to: ${_format(actual)}\n`,
326+
);
327+
}
328+
288329
/**
289330
* Make an assertion that actual contains expected. If not
290331
* then thrown.

testing/asserts_test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
assertMatch,
99
assertEquals,
1010
assertStrictEquals,
11+
assertNotStrictEquals,
1112
assertThrows,
1213
assertThrowsAsync,
1314
AssertionError,
@@ -463,13 +464,42 @@ Deno.test({
463464
});
464465

465466
Deno.test({
466-
name: "assert* functions with specified type paratemeter",
467+
name: "strictly unequal pass case",
468+
fn(): void {
469+
assertNotStrictEquals(true, false);
470+
assertNotStrictEquals(10, 11);
471+
assertNotStrictEquals("abc", "xyz");
472+
assertNotStrictEquals(1, "1");
473+
474+
const xs = [1, false, "foo"];
475+
const ys = [1, true, "bar"];
476+
assertNotStrictEquals(xs, ys);
477+
478+
const x = { a: 1 };
479+
const y = { a: 2 };
480+
assertNotStrictEquals(x, y);
481+
},
482+
});
483+
484+
Deno.test({
485+
name: "strictly unequal fail case",
486+
fn(): void {
487+
assertThrows(
488+
() => assertNotStrictEquals(1, 1),
489+
AssertionError,
490+
);
491+
},
492+
});
493+
494+
Deno.test({
495+
name: "assert* functions with specified type parameter",
467496
fn(): void {
468497
assertEquals<string>("hello", "hello");
469498
assertNotEquals<number>(1, 2);
470499
assertArrayContains<boolean>([true, false], [true]);
471500
const value = { x: 1 };
472501
assertStrictEquals<typeof value>(value, value);
502+
assertNotStrictEquals<object>(value, { x: 1 });
473503
},
474504
});
475505

0 commit comments

Comments
 (0)