Skip to content

Commit b649680

Browse files
mbhrznriuioiua
andauthored
test(assert): improve test coverage (denoland#4679)
* test(assert): improve test coverage * chore: format * chore: lint * nits --------- Co-authored-by: Asher Gomez <[email protected]>
1 parent 5e99c21 commit b649680

25 files changed

+394
-174
lines changed

assert/assert_almost_equals_test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,11 @@ Deno.test("assertAlmostEquals() throws when special numbers do not match", () =>
5151
'Expected actual: "Infinity" to be close to "NaN"',
5252
);
5353
});
54+
55+
Deno.test("assertAlmostEquals() throws with custom message", () => {
56+
assertThrows(
57+
() => assertAlmostEquals(-Infinity, +Infinity, 1e-17, "CUSTOM MESSAGE"),
58+
AssertionError,
59+
`Expected actual: "-Infinity" to be close to "Infinity": delta "Infinity" is greater than "1e-17": CUSTOM MESSAGE`,
60+
);
61+
});

assert/assert_array_includes_test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@ missing: [
5656
);
5757
});
5858

59+
Deno.test("assertArrayIncludes() throws with custom message", () => {
60+
assertThrows(
61+
() => assertArrayIncludes(["a"], ["b"], "CUSTOM MESSAGE"),
62+
AssertionError,
63+
`
64+
Expected actual: "[
65+
"a",
66+
]" to include: "[
67+
"b",
68+
]": CUSTOM MESSAGE
69+
missing: [
70+
"b",
71+
]
72+
`.trim(),
73+
);
74+
});
75+
5976
// https://github.com/denoland/deno_std/issues/3372
6077
Deno.test("assertArrayIncludes() type-checks failing cases", () => {
6178
// @ts-expect-error 2nd arg - 'string' is not assignable to 'ArrayLikeArg<string>'.

assert/assert_equals_test.ts

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
22
import { assertEquals, AssertionError, assertThrows } from "./mod.ts";
33
import { bold, gray, green, red, stripAnsiCode, yellow } from "@std/fmt/colors";
4+
import { _internals } from "@std/internal";
5+
import { stub } from "@std/testing/mock";
46

57
const createHeader = (): string[] => [
68
"",
@@ -151,9 +153,27 @@ Deno.test({
151153
},
152154
});
153155

154-
Deno.test(
155-
"assertEquals() compares objects structurally if one object's constructor is undefined and the other is Object",
156-
() => {
156+
Deno.test({
157+
name: "assertEquals() throws with [Cannot display] if diffing fails",
158+
fn() {
159+
using _ = stub(_internals, "diff", () => {
160+
throw new Error();
161+
});
162+
assertThrows(
163+
() => assertEquals("1", "2"),
164+
AssertionError,
165+
[
166+
"Values are not equal.",
167+
"[Cannot display]",
168+
].join("\n"),
169+
);
170+
},
171+
});
172+
173+
Deno.test({
174+
name:
175+
"assertEquals() compares objects structurally if one object's constructor is undefined and the other is Object",
176+
fn() {
157177
const a = Object.create(null);
158178
a.prop = "test";
159179
const b = {
@@ -163,50 +183,56 @@ Deno.test(
163183
assertEquals(a, b);
164184
assertEquals(b, a);
165185
},
166-
);
186+
});
167187

168-
Deno.test("assertEquals() orders diff for differently ordered objects", () => {
169-
assertThrows(
170-
() => {
171-
assertEquals(
172-
{
173-
aaaaaaaaaaaaaaaaaaaaaaaa: 0,
174-
bbbbbbbbbbbbbbbbbbbbbbbb: 0,
175-
ccccccccccccccccccccccc: 0,
176-
},
177-
{
178-
ccccccccccccccccccccccc: 1,
179-
aaaaaaaaaaaaaaaaaaaaaaaa: 0,
180-
bbbbbbbbbbbbbbbbbbbbbbbb: 0,
181-
},
182-
);
183-
},
184-
AssertionError,
185-
`
188+
Deno.test({
189+
name: "assertEquals() orders diff for differently ordered objects",
190+
fn() {
191+
assertThrows(
192+
() => {
193+
assertEquals(
194+
{
195+
aaaaaaaaaaaaaaaaaaaaaaaa: 0,
196+
bbbbbbbbbbbbbbbbbbbbbbbb: 0,
197+
ccccccccccccccccccccccc: 0,
198+
},
199+
{
200+
ccccccccccccccccccccccc: 1,
201+
aaaaaaaaaaaaaaaaaaaaaaaa: 0,
202+
bbbbbbbbbbbbbbbbbbbbbbbb: 0,
203+
},
204+
);
205+
},
206+
AssertionError,
207+
`
186208
{
187209
aaaaaaaaaaaaaaaaaaaaaaaa: 0,
188210
bbbbbbbbbbbbbbbbbbbbbbbb: 0,
189211
- ccccccccccccccccccccccc: 0,
190212
+ ccccccccccccccccccccccc: 1,
191213
}`,
192-
);
214+
);
215+
},
193216
});
194217

195-
Deno.test("assertEquals() matches same Set with object keys", () => {
196-
const data = [
197-
{
198-
id: "_1p7ZED73OF98VbT1SzSkjn",
199-
type: { id: "_ETGENUS" },
200-
name: "Thuja",
201-
friendlyId: "g-thuja",
202-
},
203-
{
204-
id: "_567qzghxZmeQ9pw3q09bd3",
205-
type: { id: "_ETGENUS" },
206-
name: "Pinus",
207-
friendlyId: "g-pinus",
208-
},
209-
];
210-
assertEquals(data, data);
211-
assertEquals(new Set(data), new Set(data));
218+
Deno.test({
219+
name: "assertEquals() matches same Set with object keys",
220+
fn() {
221+
const data = [
222+
{
223+
id: "_1p7ZED73OF98VbT1SzSkjn",
224+
type: { id: "_ETGENUS" },
225+
name: "Thuja",
226+
friendlyId: "g-thuja",
227+
},
228+
{
229+
id: "_567qzghxZmeQ9pw3q09bd3",
230+
type: { id: "_ETGENUS" },
231+
name: "Pinus",
232+
friendlyId: "g-pinus",
233+
},
234+
];
235+
assertEquals(data, data);
236+
assertEquals(new Set(data), new Set(data));
237+
},
212238
});

assert/assert_exists_test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,11 @@ Deno.test("assertExists() throws when value is null or undefined", () => {
3232
'Expected actual: "null" to not be null or undefined.',
3333
);
3434
});
35+
36+
Deno.test("assertExists() throws with custom message", () => {
37+
assertThrows(
38+
() => assertExists(undefined, "CUSTOM MESSAGE"),
39+
AssertionError,
40+
'Expected actual: "undefined" to not be null or undefined: CUSTOM MESSAGE',
41+
);
42+
});

assert/assert_instance_of_test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ Deno.test({
3333

3434
// Custom message
3535
assertThrows(
36-
() => assertInstanceOf(new Date(), RegExp, "Custom message"),
36+
() => assertInstanceOf(new Date(), RegExp, "CUSTOM MESSAGE"),
3737
AssertionError,
38-
"Custom message",
38+
"CUSTOM MESSAGE",
3939
);
4040

4141
// Edge cases

assert/assert_is_error.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ export function assertIsError<E extends Error = Error>(
3434
);
3535
}
3636
if (ErrorClass && !(error instanceof ErrorClass)) {
37-
msg = `Expected error to be instance of "${ErrorClass.name}", but was "${
38-
typeof error === "object" ? error?.constructor?.name : "[not an object]"
39-
}"${msgSuffix}`;
37+
msg =
38+
`Expected error to be instance of "${ErrorClass.name}", but was "${error?.constructor?.name}"${msgSuffix}`;
4039
throw new AssertionError(msg);
4140
}
4241
let msgCheck;
@@ -54,11 +53,7 @@ export function assertIsError<E extends Error = Error>(
5453
msgMatches instanceof RegExp
5554
? msgMatches.toString()
5655
: JSON.stringify(msgMatches)
57-
}, but got ${
58-
error instanceof Error
59-
? JSON.stringify(error.message)
60-
: '"[not an Error]"' // TODO(kt3k): show more useful information
61-
}${msgSuffix}`;
56+
}, but got ${JSON.stringify(error?.message)}${msgSuffix}`;
6257
throw new AssertionError(msg);
6358
}
6459
}

assert/assert_is_error_test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,17 @@ Deno.test("assertIsError() throws when given value doesn't match regex ", () =>
5858
`Expected error message to include /egg/, but got "Regex test"`,
5959
);
6060
});
61+
62+
Deno.test("assertIsError() throws with custom message", () => {
63+
assertThrows(
64+
() =>
65+
assertIsError(
66+
new CustomError("failed"),
67+
AnotherCustomError,
68+
"fail",
69+
"CUSTOM MESSAGE",
70+
),
71+
AssertionError,
72+
'Expected error to be instance of "AnotherCustomError", but was "CustomError": CUSTOM MESSAGE',
73+
);
74+
});

assert/assert_match_test.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2-
import { assert, AssertionError, assertMatch } from "./mod.ts";
2+
import { assertThrows } from "./assert_throws.ts";
3+
import { AssertionError, assertMatch } from "./mod.ts";
34

4-
Deno.test("AssertStringMatching", function () {
5+
Deno.test("assertMatch()", () => {
56
assertMatch("[email protected]", RegExp(/[a-zA-Z]+@[a-zA-Z]+.com/));
67
});
78

8-
Deno.test("AssertStringMatchingThrows", function () {
9-
let didThrow = false;
10-
try {
11-
assertMatch("Denosaurus from Jurassic", RegExp(/Raptor/));
12-
} catch (e) {
13-
assert(e instanceof AssertionError);
14-
assert(
15-
e.message ===
16-
`Expected actual: "Denosaurus from Jurassic" to match: "/Raptor/".`,
17-
);
18-
didThrow = true;
19-
}
20-
assert(didThrow);
9+
Deno.test("assertMatch() throws", () => {
10+
assertThrows(
11+
() => assertMatch("Denosaurus from Jurassic", RegExp(/Raptor/)),
12+
AssertionError,
13+
`Expected actual: "Denosaurus from Jurassic" to match: "/Raptor/".`,
14+
);
15+
});
16+
17+
Deno.test("assertMatch() throws with custom message", () => {
18+
assertThrows(
19+
() =>
20+
assertMatch(
21+
"Denosaurus from Jurassic",
22+
RegExp(/Raptor/),
23+
"CUSTOM MESSAGE",
24+
),
25+
AssertionError,
26+
`Expected actual: "Denosaurus from Jurassic" to match: "/Raptor/": CUSTOM MESSAGE`,
27+
);
2128
});

assert/assert_not_equals_test.ts

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
11
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2-
import {
3-
assert,
4-
assertEquals,
5-
AssertionError,
6-
assertNotEquals,
7-
} from "./mod.ts";
2+
import { AssertionError, assertNotEquals, assertThrows } from "./mod.ts";
3+
import { stub } from "@std/testing/mock";
84

9-
Deno.test("NotEquals", function () {
10-
const a = { foo: "bar" };
11-
const b = { bar: "foo" };
12-
assertNotEquals<unknown>(a, b);
5+
Deno.test("assertNotEquals()", () => {
6+
assertNotEquals<unknown>({ foo: "bar" }, { bar: "foo" });
137
assertNotEquals("Denosaurus", "Tyrannosaurus");
148
assertNotEquals(
159
new Date(2019, 0, 3, 4, 20, 1, 10),
1610
new Date(2019, 0, 3, 4, 20, 1, 20),
1711
);
1812
assertNotEquals(new Date("invalid"), new Date(2019, 0, 3, 4, 20, 1, 20));
19-
let didThrow;
20-
try {
21-
assertNotEquals("Raptor", "Raptor");
22-
didThrow = false;
23-
} catch (e) {
24-
assert(e instanceof AssertionError);
25-
didThrow = true;
26-
}
27-
assertEquals(didThrow, true);
13+
});
14+
15+
Deno.test("assertNotEquals() throws", () => {
16+
assertThrows(
17+
() => {
18+
assertNotEquals("foo", "foo");
19+
},
20+
AssertionError,
21+
"Expected actual: foo not to be: foo.",
22+
);
23+
});
24+
25+
Deno.test("assertNotEquals() throws with custom message", () => {
26+
assertThrows(
27+
() => {
28+
assertNotEquals("foo", "foo", "CUSTOM MESSAGE");
29+
},
30+
AssertionError,
31+
"Expected actual: foo not to be: foo: CUSTOM MESSAGE",
32+
);
33+
});
34+
35+
Deno.test("assertNotEquals() throws with [Cannot display]", () => {
36+
using _ = stub(globalThis, "String", () => {
37+
throw new Error();
38+
});
39+
assertThrows(
40+
() => {
41+
assertNotEquals("a", "a");
42+
},
43+
AssertionError,
44+
`Expected actual: [Cannot display] not to be: [Cannot display].`,
45+
);
2846
});
Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
11
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2-
import { assertNotInstanceOf } from "./mod.ts";
2+
import { AssertionError, assertNotInstanceOf, assertThrows } from "./mod.ts";
33

44
Deno.test({
5-
name: "assertNotInstanceOf",
5+
name: "assertNotInstanceOf()",
66
fn() {
77
assertNotInstanceOf("not a number", Number);
88
assertNotInstanceOf(42, String);
99
assertNotInstanceOf(new URL("http://example.com"), Boolean);
1010
},
1111
});
12+
13+
Deno.test({
14+
name: "assertNotInstanceOf() throws",
15+
fn() {
16+
assertThrows(
17+
() => assertNotInstanceOf(new Date(), Date),
18+
AssertionError,
19+
'Expected object to not be an instance of "function".',
20+
);
21+
},
22+
});
23+
24+
Deno.test({
25+
name: "assertNotInstanceOf() throws with custom message",
26+
fn() {
27+
assertThrows(
28+
() => assertNotInstanceOf(new Date(), Date, "CUSTOM MESSAGE"),
29+
AssertionError,
30+
'Expected object to not be an instance of "function": CUSTOM MESSAGE',
31+
);
32+
},
33+
});

0 commit comments

Comments
 (0)