Skip to content

Commit

Permalink
test(expect): add test of iterableEquality (#5222)
Browse files Browse the repository at this point in the history
Co-authored-by: Asher Gomez <[email protected]>
  • Loading branch information
kt3k and iuioiua authored Jul 1, 2024
1 parent 76cf563 commit 90025e9
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions expect/_to_equal_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,39 @@ Deno.test("expect().toEqual() matches when Error Objects are equal", () => {
const expectErrObjectWithEmail = new Error("missing param: email");
expect(getError()).not.toEqual(expectErrObjectWithEmail);
});

Deno.test("expect().toEqual() handles Sets", () => {
expect(new Set([1, 2, 3])).toEqual(new Set([1, 2, 3]));
expect(new Set([1, 2, 3])).not.toEqual(new Set([1, 2]));
expect(new Set([1, 2, 3])).not.toEqual(new Set([1, 2, 4]));
expect(new Set([1, 2, 3, 4])).not.toEqual(new Map([[1, 2], [3, 4]]));
expect(new Set([1, 2, new Set([0, 1])])).toEqual(
new Set([1, 2, new Set([0, 1])]),
);

// It handles circular reference structures
const a = new Set<unknown>([1, 2]);
a.add(a);
const b = new Set<unknown>([1, 2]);
b.add(b);
expect(a).toEqual(b);
});

Deno.test("expect().toEqual() handles Maps", () => {
expect(new Map([[1, 2], [3, 4]])).toEqual(new Map([[1, 2], [3, 4]]));
expect(new Map([[1, 2], [3, 4]])).not.toEqual(new Map([[1, 2], [3, 5]]));
});

// TODO(kt3k): Iterator global exists in the runtime but not in the TypeScript
// Remove the below lines when `Iterator` global is available in TypeScript
// deno-lint-ignore no-explicit-any
const Iterator = (globalThis as any).Iterator;
Deno.test("expect().toEqual() handles iterators", () => {
expect(Iterator.from([1, 2, 3])).toEqual(Iterator.from([1, 2, 3]));
expect(Iterator.from([1, 2, 3])).not.toEqual(Iterator.from([1, 2, 4]));
expect(Iterator.from([1, 2, 3])).not.toEqual(Iterator.from([1, 2, 3, 4]));
const iter0 = Iterator.from([1, 2, 3]);
const iter1 = Iterator.from([1, 2, 3]);
iter1.foo = 1;
expect(iter0).not.toEqual(iter1);
});

0 comments on commit 90025e9

Please sign in to comment.