Skip to content

Commit

Permalink
test(assert): improve test coverage (#4679)
Browse files Browse the repository at this point in the history
* test(assert): improve test coverage

* chore: format

* chore: lint

* nits

---------

Co-authored-by: Asher Gomez <[email protected]>
  • Loading branch information
mbhrznr and iuioiua authored May 7, 2024
1 parent 5e99c21 commit b649680
Show file tree
Hide file tree
Showing 25 changed files with 394 additions and 174 deletions.
8 changes: 8 additions & 0 deletions assert/assert_almost_equals_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,11 @@ Deno.test("assertAlmostEquals() throws when special numbers do not match", () =>
'Expected actual: "Infinity" to be close to "NaN"',
);
});

Deno.test("assertAlmostEquals() throws with custom message", () => {
assertThrows(
() => assertAlmostEquals(-Infinity, +Infinity, 1e-17, "CUSTOM MESSAGE"),
AssertionError,
`Expected actual: "-Infinity" to be close to "Infinity": delta "Infinity" is greater than "1e-17": CUSTOM MESSAGE`,
);
});
17 changes: 17 additions & 0 deletions assert/assert_array_includes_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ missing: [
);
});

Deno.test("assertArrayIncludes() throws with custom message", () => {
assertThrows(
() => assertArrayIncludes(["a"], ["b"], "CUSTOM MESSAGE"),
AssertionError,
`
Expected actual: "[
"a",
]" to include: "[
"b",
]": CUSTOM MESSAGE
missing: [
"b",
]
`.trim(),
);
});

// https://github.com/denoland/deno_std/issues/3372
Deno.test("assertArrayIncludes() type-checks failing cases", () => {
// @ts-expect-error 2nd arg - 'string' is not assignable to 'ArrayLikeArg<string>'.
Expand Down
106 changes: 66 additions & 40 deletions assert/assert_equals_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals, AssertionError, assertThrows } from "./mod.ts";
import { bold, gray, green, red, stripAnsiCode, yellow } from "@std/fmt/colors";
import { _internals } from "@std/internal";
import { stub } from "@std/testing/mock";

const createHeader = (): string[] => [
"",
Expand Down Expand Up @@ -151,9 +153,27 @@ Deno.test({
},
});

Deno.test(
"assertEquals() compares objects structurally if one object's constructor is undefined and the other is Object",
() => {
Deno.test({
name: "assertEquals() throws with [Cannot display] if diffing fails",
fn() {
using _ = stub(_internals, "diff", () => {
throw new Error();
});
assertThrows(
() => assertEquals("1", "2"),
AssertionError,
[
"Values are not equal.",
"[Cannot display]",
].join("\n"),
);
},
});

Deno.test({
name:
"assertEquals() compares objects structurally if one object's constructor is undefined and the other is Object",
fn() {
const a = Object.create(null);
a.prop = "test";
const b = {
Expand All @@ -163,50 +183,56 @@ Deno.test(
assertEquals(a, b);
assertEquals(b, a);
},
);
});

Deno.test("assertEquals() orders diff for differently ordered objects", () => {
assertThrows(
() => {
assertEquals(
{
aaaaaaaaaaaaaaaaaaaaaaaa: 0,
bbbbbbbbbbbbbbbbbbbbbbbb: 0,
ccccccccccccccccccccccc: 0,
},
{
ccccccccccccccccccccccc: 1,
aaaaaaaaaaaaaaaaaaaaaaaa: 0,
bbbbbbbbbbbbbbbbbbbbbbbb: 0,
},
);
},
AssertionError,
`
Deno.test({
name: "assertEquals() orders diff for differently ordered objects",
fn() {
assertThrows(
() => {
assertEquals(
{
aaaaaaaaaaaaaaaaaaaaaaaa: 0,
bbbbbbbbbbbbbbbbbbbbbbbb: 0,
ccccccccccccccccccccccc: 0,
},
{
ccccccccccccccccccccccc: 1,
aaaaaaaaaaaaaaaaaaaaaaaa: 0,
bbbbbbbbbbbbbbbbbbbbbbbb: 0,
},
);
},
AssertionError,
`
{
aaaaaaaaaaaaaaaaaaaaaaaa: 0,
bbbbbbbbbbbbbbbbbbbbbbbb: 0,
- ccccccccccccccccccccccc: 0,
+ ccccccccccccccccccccccc: 1,
}`,
);
);
},
});

Deno.test("assertEquals() matches same Set with object keys", () => {
const data = [
{
id: "_1p7ZED73OF98VbT1SzSkjn",
type: { id: "_ETGENUS" },
name: "Thuja",
friendlyId: "g-thuja",
},
{
id: "_567qzghxZmeQ9pw3q09bd3",
type: { id: "_ETGENUS" },
name: "Pinus",
friendlyId: "g-pinus",
},
];
assertEquals(data, data);
assertEquals(new Set(data), new Set(data));
Deno.test({
name: "assertEquals() matches same Set with object keys",
fn() {
const data = [
{
id: "_1p7ZED73OF98VbT1SzSkjn",
type: { id: "_ETGENUS" },
name: "Thuja",
friendlyId: "g-thuja",
},
{
id: "_567qzghxZmeQ9pw3q09bd3",
type: { id: "_ETGENUS" },
name: "Pinus",
friendlyId: "g-pinus",
},
];
assertEquals(data, data);
assertEquals(new Set(data), new Set(data));
},
});
8 changes: 8 additions & 0 deletions assert/assert_exists_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ Deno.test("assertExists() throws when value is null or undefined", () => {
'Expected actual: "null" to not be null or undefined.',
);
});

Deno.test("assertExists() throws with custom message", () => {
assertThrows(
() => assertExists(undefined, "CUSTOM MESSAGE"),
AssertionError,
'Expected actual: "undefined" to not be null or undefined: CUSTOM MESSAGE',
);
});
4 changes: 2 additions & 2 deletions assert/assert_instance_of_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ Deno.test({

// Custom message
assertThrows(
() => assertInstanceOf(new Date(), RegExp, "Custom message"),
() => assertInstanceOf(new Date(), RegExp, "CUSTOM MESSAGE"),
AssertionError,
"Custom message",
"CUSTOM MESSAGE",
);

// Edge cases
Expand Down
11 changes: 3 additions & 8 deletions assert/assert_is_error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ export function assertIsError<E extends Error = Error>(
);
}
if (ErrorClass && !(error instanceof ErrorClass)) {
msg = `Expected error to be instance of "${ErrorClass.name}", but was "${
typeof error === "object" ? error?.constructor?.name : "[not an object]"
}"${msgSuffix}`;
msg =
`Expected error to be instance of "${ErrorClass.name}", but was "${error?.constructor?.name}"${msgSuffix}`;
throw new AssertionError(msg);
}
let msgCheck;
Expand All @@ -54,11 +53,7 @@ export function assertIsError<E extends Error = Error>(
msgMatches instanceof RegExp
? msgMatches.toString()
: JSON.stringify(msgMatches)
}, but got ${
error instanceof Error
? JSON.stringify(error.message)
: '"[not an Error]"' // TODO(kt3k): show more useful information
}${msgSuffix}`;
}, but got ${JSON.stringify(error?.message)}${msgSuffix}`;
throw new AssertionError(msg);
}
}
14 changes: 14 additions & 0 deletions assert/assert_is_error_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,17 @@ Deno.test("assertIsError() throws when given value doesn't match regex ", () =>
`Expected error message to include /egg/, but got "Regex test"`,
);
});

Deno.test("assertIsError() throws with custom message", () => {
assertThrows(
() =>
assertIsError(
new CustomError("failed"),
AnotherCustomError,
"fail",
"CUSTOM MESSAGE",
),
AssertionError,
'Expected error to be instance of "AnotherCustomError", but was "CustomError": CUSTOM MESSAGE',
);
});
37 changes: 22 additions & 15 deletions assert/assert_match_test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assert, AssertionError, assertMatch } from "./mod.ts";
import { assertThrows } from "./assert_throws.ts";
import { AssertionError, assertMatch } from "./mod.ts";

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

Deno.test("AssertStringMatchingThrows", function () {
let didThrow = false;
try {
assertMatch("Denosaurus from Jurassic", RegExp(/Raptor/));
} catch (e) {
assert(e instanceof AssertionError);
assert(
e.message ===
`Expected actual: "Denosaurus from Jurassic" to match: "/Raptor/".`,
);
didThrow = true;
}
assert(didThrow);
Deno.test("assertMatch() throws", () => {
assertThrows(
() => assertMatch("Denosaurus from Jurassic", RegExp(/Raptor/)),
AssertionError,
`Expected actual: "Denosaurus from Jurassic" to match: "/Raptor/".`,
);
});

Deno.test("assertMatch() throws with custom message", () => {
assertThrows(
() =>
assertMatch(
"Denosaurus from Jurassic",
RegExp(/Raptor/),
"CUSTOM MESSAGE",
),
AssertionError,
`Expected actual: "Denosaurus from Jurassic" to match: "/Raptor/": CUSTOM MESSAGE`,
);
});
56 changes: 37 additions & 19 deletions assert/assert_not_equals_test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import {
assert,
assertEquals,
AssertionError,
assertNotEquals,
} from "./mod.ts";
import { AssertionError, assertNotEquals, assertThrows } from "./mod.ts";
import { stub } from "@std/testing/mock";

Deno.test("NotEquals", function () {
const a = { foo: "bar" };
const b = { bar: "foo" };
assertNotEquals<unknown>(a, b);
Deno.test("assertNotEquals()", () => {
assertNotEquals<unknown>({ foo: "bar" }, { bar: "foo" });
assertNotEquals("Denosaurus", "Tyrannosaurus");
assertNotEquals(
new Date(2019, 0, 3, 4, 20, 1, 10),
new Date(2019, 0, 3, 4, 20, 1, 20),
);
assertNotEquals(new Date("invalid"), new Date(2019, 0, 3, 4, 20, 1, 20));
let didThrow;
try {
assertNotEquals("Raptor", "Raptor");
didThrow = false;
} catch (e) {
assert(e instanceof AssertionError);
didThrow = true;
}
assertEquals(didThrow, true);
});

Deno.test("assertNotEquals() throws", () => {
assertThrows(
() => {
assertNotEquals("foo", "foo");
},
AssertionError,
"Expected actual: foo not to be: foo.",
);
});

Deno.test("assertNotEquals() throws with custom message", () => {
assertThrows(
() => {
assertNotEquals("foo", "foo", "CUSTOM MESSAGE");
},
AssertionError,
"Expected actual: foo not to be: foo: CUSTOM MESSAGE",
);
});

Deno.test("assertNotEquals() throws with [Cannot display]", () => {
using _ = stub(globalThis, "String", () => {
throw new Error();
});
assertThrows(
() => {
assertNotEquals("a", "a");
},
AssertionError,
`Expected actual: [Cannot display] not to be: [Cannot display].`,
);
});
26 changes: 24 additions & 2 deletions assert/assert_not_instance_of_test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertNotInstanceOf } from "./mod.ts";
import { AssertionError, assertNotInstanceOf, assertThrows } from "./mod.ts";

Deno.test({
name: "assertNotInstanceOf",
name: "assertNotInstanceOf()",
fn() {
assertNotInstanceOf("not a number", Number);
assertNotInstanceOf(42, String);
assertNotInstanceOf(new URL("http://example.com"), Boolean);
},
});

Deno.test({
name: "assertNotInstanceOf() throws",
fn() {
assertThrows(
() => assertNotInstanceOf(new Date(), Date),
AssertionError,
'Expected object to not be an instance of "function".',
);
},
});

Deno.test({
name: "assertNotInstanceOf() throws with custom message",
fn() {
assertThrows(
() => assertNotInstanceOf(new Date(), Date, "CUSTOM MESSAGE"),
AssertionError,
'Expected object to not be an instance of "function": CUSTOM MESSAGE',
);
},
});
Loading

0 comments on commit b649680

Please sign in to comment.