mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 09:31:22 -05:00
fix(std/testing): invalid dates assertion equality (#7230)
This commit is contained in:
parent
00b6762412
commit
84086e7d32
2 changed files with 30 additions and 0 deletions
|
@ -95,6 +95,13 @@ export function equal(c: unknown, d: unknown): boolean {
|
|||
return String(a) === String(b);
|
||||
}
|
||||
if (a instanceof Date && b instanceof Date) {
|
||||
const aTime = a.getTime();
|
||||
const bTime = b.getTime();
|
||||
// Check for NaN equality manually since NaN is not
|
||||
// equal to itself.
|
||||
if (Number.isNaN(aTime) && Number.isNaN(bTime)) {
|
||||
return true;
|
||||
}
|
||||
return a.getTime() === b.getTime();
|
||||
}
|
||||
if (Object.is(a, b)) {
|
||||
|
|
|
@ -50,6 +50,9 @@ Deno.test("testingEqual", function (): void {
|
|||
new Date(2019, 0, 3, 4, 20, 1, 20),
|
||||
),
|
||||
);
|
||||
assert(equal(new Date("Invalid"), new Date("Invalid")));
|
||||
assert(!equal(new Date("Invalid"), new Date(2019, 0, 3)));
|
||||
assert(!equal(new Date("Invalid"), new Date(2019, 0, 3, 4, 20, 1, 10)));
|
||||
assert(equal(new Set([1]), new Set([1])));
|
||||
assert(!equal(new Set([1]), new Set([2])));
|
||||
assert(equal(new Set([1, 2, 3]), new Set([3, 2, 1])));
|
||||
|
@ -141,6 +144,10 @@ Deno.test("testingNotEquals", function (): void {
|
|||
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");
|
||||
|
@ -340,6 +347,7 @@ Deno.test({
|
|||
assertEquals(10, 10);
|
||||
assertEquals("abc", "abc");
|
||||
assertEquals({ a: 10, b: { c: "1" } }, { a: 10, b: { c: "1" } });
|
||||
assertEquals(new Date("invalid"), new Date("invalid"));
|
||||
},
|
||||
});
|
||||
|
||||
|
@ -431,6 +439,21 @@ Deno.test({
|
|||
"",
|
||||
].join("\n"),
|
||||
);
|
||||
assertThrows(
|
||||
(): void =>
|
||||
assertEquals(
|
||||
new Date("invalid"),
|
||||
new Date(2019, 0, 3, 4, 20, 1, 20),
|
||||
),
|
||||
AssertionError,
|
||||
[
|
||||
"Values are not equal:",
|
||||
...createHeader(),
|
||||
removed(`- ${new Date("invalid")}`),
|
||||
added(`+ ${new Date(2019, 0, 3, 4, 20, 1, 20).toISOString()}`),
|
||||
"",
|
||||
].join("\n"),
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue