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): improve expect test cases #5221

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions expect/_extend_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { expect } from "./expect.ts";
import type { MatcherContext, Tester } from "./_types.ts";
import { AssertionError, assertThrows } from "@std/assert";

declare module "./_types.ts" {
interface Expected {
Expand Down Expand Up @@ -97,4 +98,15 @@ Deno.test("expect.extend() api test case", () => {
expect(book1a).not.toEqualBook(book2);
expect(book1a).not.toEqualBook(1);
expect(book1a).not.toEqualBook(null);

assertThrows(
() => expect(book1a).toEqualBook(book2),
AssertionError,
"Expected Book object: Book 2. Actual Book object: Book 1",
);
assertThrows(
() => expect(book1a).not.toEqualBook(book1b),
AssertionError,
"Expected Book object: Book 1. Actual Book object: Book 1",
);
});
51 changes: 51 additions & 0 deletions expect/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { expect } from "./expect.ts";
import { AssertionError, assertRejects, assertThrows } from "@std/assert";

Deno.test("expect().resolves.toEqual()", async () => {
await expect(Promise.resolve(42)).resolves.toEqual(42);
await expect(Promise.resolve(42)).resolves.not.toEqual(0);

assertThrows(
() => expect(42).resolves.toEqual(42),
AssertionError,
"expected value must be Promiselike",
);
assertThrows(
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
() => expect(null).resolves.toEqual(42),
AssertionError,
"expected value must be Promiselike",
);
});

Deno.test("expect().rejects.toEqual()", async () => {
await expect(Promise.reject(42)).rejects.toEqual(42);
await expect(Promise.reject(42)).rejects.not.toEqual(0);

assertThrows(
() => expect(42).rejects.toEqual(42),
AssertionError,
"expected value must be a PromiseLike",
);
await assertRejects(
() => expect(Promise.resolve(42)).rejects.toEqual(42),
AssertionError,
"Promise did not reject. resolved to 42",
);
});

Deno.test("expect()[nonExistentMatcher]()", () => {
assertThrows(
// deno-lint-ignore no-explicit-any
() => (expect(() => {}) as any)["nonExistentMatcher"](),
TypeError,
"matcher not found: nonExistentMatcher",
);
assertThrows(
// deno-lint-ignore no-explicit-any
() => (expect(() => {}) as any)[Symbol()](),
TypeError,
"matcher not found",
);
});