Skip to content

Commit

Permalink
fix(testing): assertEquals now considers constructors equal if one …
Browse files Browse the repository at this point in the history
…is nullable and the other is Object (#1159)
  • Loading branch information
dsherret committed Aug 23, 2021
1 parent 09844c2 commit 2fb3577
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion testing/asserts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export function equal(c: unknown, d: unknown): boolean {
return true;
}
if (a && typeof a === "object" && b && typeof b === "object") {
if (a && b && (a.constructor !== b.constructor)) {
if (a && b && !constructorsEqual(a, b)) {
return false;
}
if (a instanceof WeakMap || b instanceof WeakMap) {
Expand Down Expand Up @@ -211,6 +211,13 @@ export function equal(c: unknown, d: unknown): boolean {
})(c, d);
}

// deno-lint-ignore ban-types
function constructorsEqual(a: object, b: object) {
return a.constructor === b.constructor ||
a.constructor === Object && !b.constructor ||
!a.constructor && b.constructor === Object;
}

/** Make an assertion, error will be thrown if `expr` does not have truthy value. */
export function assert(expr: unknown, msg = ""): asserts expr {
if (!expr) {
Expand Down
11 changes: 11 additions & 0 deletions testing/asserts_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,17 @@ Deno.test("Assert Throws Async Non-Error Fail", () => {
);
});

Deno.test("assertEquals compares objects structurally if one object's constructor is undefined and the other is Object", () => {
const a = Object.create(null);
a.prop = "test";
const b = {
prop: "test",
};

assertEquals(a, b);
assertEquals(b, a);
});

Deno.test("assertEquals diff for differently ordered objects", () => {
assertThrows(
() => {
Expand Down

0 comments on commit 2fb3577

Please sign in to comment.