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): test edge cases of matchers #5226

Merged
merged 1 commit 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
8 changes: 8 additions & 0 deletions expect/_to_be_close_to_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ Deno.test("expect().toBeCloseTo()", () => {
expect(0.2 + 0.1).not.toBeCloseTo(0.3);
});
});

Deno.test("expect().toBeCloseTo() throws error when the numDigits is smaller than 0", () => {
assertThrows(
() => expect(0.2 + 0.1).toBeCloseTo(0.3, -1),
Error,
"toBeCloseTo second argument must be a non-negative integer. Got -1",
);
});
21 changes: 21 additions & 0 deletions expect/_to_contain_equal_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,24 @@ Value: [{foo: 42},{bar: 43},{baz: 44}]
Expected: {foo: 42}`,
);
});

Deno.test("expect().toContainEqual() handles null and undefined", () => {
assertThrows(
() => expect(null).not.toContainEqual("foo"),
AssertionError,
"The value is null or undefined",
);
assertThrows(
() => expect(undefined).not.toContainEqual("foo"),
AssertionError,
"The value is null or undefined",
);
});

Deno.test("expect().toContainEqual() throws error when the value is not an array", () => {
assertThrows(
() => expect({ foo: 42 }).toContainEqual({ foo: 42 }),
AssertionError,
"The value is not iterable",
);
});
22 changes: 21 additions & 1 deletion expect/_to_have_been_called_test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { assertThrows } from "../assert/assert_throws.ts";
import { expect } from "./expect.ts";
import { fn } from "./fn.ts";

Deno.test("expect().toHaveBeenCalled()", () => {
Deno.test("expect().toHaveBeenCalled() checks the mock call", () => {
const mockFn = fn();
mockFn();

expect(mockFn).toHaveBeenCalled();

assertThrows(
() => expect(mockFn).not.toHaveBeenCalled(),
Error,
"Expected mock function not to be called, but it was called 1 time(s)",
);
});

Deno.test("expect().toHaveBeenCalled() handles the case when the mock is not called", () => {
const mockFn = fn();

expect(mockFn).not.toHaveBeenCalled();

assertThrows(
() => expect(mockFn).toHaveBeenCalled(),
Error,
"Expected mock function to be called, but it was not called",
);
});
13 changes: 12 additions & 1 deletion expect/_to_have_been_last_called_with_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { expect } from "./expect.ts";
import { fn } from "./fn.ts";
import { AssertionError, assertThrows } from "@std/assert";

Deno.test("expect().toHaveBeenLastCalledWith()", () => {
Deno.test("expect().toHaveBeenLastCalledWith() checks the last call of mock function", () => {
const mockFn = fn();

mockFn(1, 2, 3);
Expand All @@ -22,3 +22,14 @@ Deno.test("expect().toHaveBeenLastCalledWith()", () => {
expect(mockFn).not.toHaveBeenLastCalledWith(4, 5, 6);
}, AssertionError);
});

Deno.test("expect().toHaveBeenLastCalledWith() handles the case when the mock is not called", () => {
const mockFn = fn();

expect(mockFn).not.toHaveBeenLastCalledWith(1, 2, 3);
assertThrows(
() => expect(mockFn).toHaveBeenLastCalledWith(1, 2, 3),
AssertionError,
"Expected mock function to be last called with 1, 2, 3, but it was not.",
);
});
5 changes: 5 additions & 0 deletions expect/_to_have_property_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ Deno.test("expect().toHaveProperty()", () => {
expect({ a: { b: { c: { d: 5 } } } }).not.toHaveProperty("a.b.c", { d: 5 });
}, AssertionError);
});

Deno.test("expect().toHaveProperty() handles null and undefined", () => {
expect(null).not.toHaveProperty("foo");
expect(undefined).not.toHaveProperty("foo");
});