test(expect): test edge cases of matchers (#5226)

This commit is contained in:
Yoshiya Hinosawa 2024-07-01 18:13:47 +09:00 committed by GitHub
parent ef71997cb4
commit 9a4dffff98
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 67 additions and 2 deletions

View File

@ -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",
);
});

View File

@ -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",
);
});

View File

@ -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",
);
});

View File

@ -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);
@ -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.",
);
});

View File

@ -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");
});