Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(expect): add test of iterableEquality #5222

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});