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

fix(expect): always show custom message #6217

Merged
merged 14 commits into from
Dec 17, 2024
211 changes: 131 additions & 80 deletions expect/_matchers.ts

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions expect/_to_be_close_to_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,21 @@ Deno.test("expect().toBeCloseTo() throws error when the numDigits is smaller tha
"toBeCloseTo second argument must be a non-negative integer. Got -1",
);
});

Deno.test("expect().toBeCloseTo() throws custom message", () => {
const msg = "toBeCloseTo Custom Error";

assertThrows(
() => expect(0.2 + 0.11, msg).toBeCloseTo(0.3),
Error,
msg,
);

assertThrows(
() => {
expect(0.2 + 0.1, msg).not.toBeCloseTo(0.3);
},
AssertionError,
msg,
);
});
19 changes: 19 additions & 0 deletions expect/_to_be_falsy_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,22 @@ Deno.test("expect().toBeFalsy()", () => {
expect("").not.toBeFalsy();
}, AssertionError);
});

Deno.test("expect().toBeFalsy() with custom error message message", () => {
const msg = "toBeFalsy Custom Error";
assertThrows(
() => {
expect("hello", msg).toBeFalsy();
},
AssertionError,
msg,
);

assertThrows(
() => {
expect("", msg).not.toBeFalsy();
},
AssertionError,
msg,
);
});
20 changes: 20 additions & 0 deletions expect/_to_be_greater_than_or_equal_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,23 @@ Deno.test("expect().toBeGreaterThanOrEqual()", () => {
expect(11).not.toBeGreaterThanOrEqual(10);
}, AssertionError);
});

Deno.test("expect().toBeGreaterThanOrEqual() with custom error message", () => {
const msg = "toBeGreaterThanOrEqual Custom Error";

assertThrows(
() => {
expect(10, msg).toBeGreaterThan(10);
},
AssertionError,
msg,
);

assertThrows(
() => {
expect(11, msg).not.toBeGreaterThanOrEqual(10);
},
AssertionError,
msg,
);
});
20 changes: 20 additions & 0 deletions expect/_to_be_greater_than_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,23 @@ Deno.test("expect().toBeGreaterThan()", () => {
expect(11).not.toBeGreaterThan(10);
}, AssertionError);
});

Deno.test("expect().toBeGreaterThan() with custom error message message", () => {
const msg = "toBeGreaterThan Custom Error";

assertThrows(
() => {
expect(10, msg).toBeGreaterThan(10);
},
AssertionError,
msg,
);

assertThrows(
() => {
expect(11, msg).not.toBeGreaterThan(10);
},
AssertionError,
msg,
);
});
20 changes: 20 additions & 0 deletions expect/_to_be_less_than_or_equal_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,23 @@ Deno.test("expect().toBeLessThanOrEqual()", () => {
expect(9).not.toBeLessThanOrEqual(10);
}, AssertionError);
});

Deno.test("expect().toBeLessThanOrEqual() with custom error message", () => {
const msg = "toBeLessThanOrEqual Custom Error";

assertThrows(
() => {
expect(11, msg).toBeLessThanOrEqual(10);
},
AssertionError,
msg,
);

assertThrows(
() => {
expect(9, msg).not.toBeLessThanOrEqual(10);
},
AssertionError,
msg,
);
});
20 changes: 20 additions & 0 deletions expect/_to_be_less_than_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,23 @@ Deno.test("expect().toBeLessThan()", () => {
expect(9).not.toBeLessThan(10);
}, AssertionError);
});

Deno.test("expect().toBeLessThan() with custom error message", () => {
const msg = "toBeLessThan Custom Error";

assertThrows(
() => {
expect(10, msg).toBeLessThan(10);
},
AssertionError,
msg,
);

assertThrows(
() => {
expect(9, msg).not.toBeLessThan(10);
},
AssertionError,
msg,
);
});
19 changes: 19 additions & 0 deletions expect/_to_be_truthy_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,22 @@ Deno.test("expect().toBeTruthy()", () => {
expect({}).not.toBeTruthy();
}, AssertionError);
});

Deno.test("expect().toBeTruthy() with custom error message message", () => {
const msg = "toBeTruthy Custom Error";
assertThrows(
() => {
expect(0, msg).toBeTruthy();
},
AssertionError,
msg,
);

assertThrows(
() => {
expect({}, msg).not.toBeTruthy();
},
AssertionError,
msg,
);
});
21 changes: 21 additions & 0 deletions expect/_to_contain_equal_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,24 @@ Deno.test("expect().toContainEqual() throws error when the value is not an array
"The value is not iterable",
);
});

Deno.test("expect().toContainEqual() with custom error message", () => {
const msg = "toContainEqual Custom Error";

assertThrows(
() =>
expect([{ foo: 42 }, { bar: 43 }, { baz: 44 }], msg).toContainEqual({
foo: 4,
}),
AssertionError,
msg,
);
assertThrows(
() =>
expect([{ foo: 42 }, { bar: 43 }, { baz: 44 }], msg).not.toContainEqual({
foo: 42,
}),
AssertionError,
msg,
);
});
20 changes: 20 additions & 0 deletions expect/_to_contain_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,23 @@ Deno.test("expect().toContain()", () => {
'The value "foobarbaz" contains the expected item "bar"',
);
});

Deno.test("expect().toContain() with custom error message", () => {
const arr = [1, 2, 3];
const msg = "toContain Custom Error";

assertThrows(
() => {
expect(arr, msg).not.toContain(2);
},
AssertionError,
msg,
);
assertThrows(
() => {
expect("foobarbaz", msg).not.toContain("bar");
},
AssertionError,
msg,
);
});
18 changes: 18 additions & 0 deletions expect/_to_have_been_called_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,21 @@ Deno.test("expect().toHaveBeenCalled() handles the case when the mock is not cal
"Expected mock function to be called, but it was not called",
);
});

Deno.test("expect().toHaveBeenCalled() with custom error message", () => {
const msg = "toHaveBeenCalled Custom Error";
const mockFn = fn();
mockFn();

assertThrows(
() => expect(mockFn, msg).not.toHaveBeenCalled(),
Error,
msg,
);

assertThrows(
() => expect(fn(), msg).toHaveBeenCalled(),
Error,
msg,
);
});
22 changes: 22 additions & 0 deletions expect/_to_have_been_called_times_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,25 @@ Deno.test("expect().toHaveBeenCalledTimes()", () => {
expect(mockFn).not.toHaveBeenCalledTimes(1);
}, AssertionError);
});

Deno.test("expect().toHaveBeenCalledTimes() with custom error message", () => {
const msg = "toHaveBeenCalledTimes Custom Error";
const mockFn = fn();
mockFn();

assertThrows(
() => {
expect(mockFn, msg).toHaveBeenCalledTimes(2);
},
AssertionError,
msg,
);

assertThrows(
() => {
expect(mockFn, msg).not.toHaveBeenCalledTimes(1);
},
AssertionError,
msg,
);
});
22 changes: 22 additions & 0 deletions expect/_to_have_been_called_with_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,25 @@ Deno.test("expect().toHaveBeenCalledWith()", () => {
expect(mockFn).not.toHaveBeenCalledWith("hello", "deno");
});
});

Deno.test("expect().toHaveBeenCalledWith() with custom error message", () => {
const msg = "toHaveBeenCalledWith custom error message";
const mockFn = fn();
mockFn("hello", "deno");

assertThrows(
() => {
expect(mockFn, msg).toHaveBeenCalledWith("hello", "DENO");
},
AssertionError,
msg,
);

assertThrows(
() => {
expect(mockFn, msg).not.toHaveBeenCalledWith("hello", "deno");
},
AssertionError,
msg,
);
});
24 changes: 24 additions & 0 deletions expect/_to_have_been_last_called_with_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,27 @@ Deno.test("expect().toHaveBeenLastCalledWith() handles the case when the mock is
"Expected mock function to be last called with 1, 2, 3, but it was not",
);
});

Deno.test("expect().toHaveBeenLastCalledWith() with custom error message", () => {
const msg = "toHaveBeenLastCalledWith custom error message";
const mockFn = fn();

mockFn(1, 2, 3);
mockFn(4, 5, 6);

assertThrows(
() => {
expect(mockFn, msg).toHaveBeenLastCalledWith(1, 2, 3);
},
AssertionError,
msg,
);

assertThrows(
() => {
expect(mockFn, msg).not.toHaveBeenLastCalledWith(4, 5, 6);
},
AssertionError,
msg,
);
});
24 changes: 24 additions & 0 deletions expect/_to_have_been_nth_called_with_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,27 @@ Deno.test("expect().toHaveBeenNthCalledWith() throw when n is not a positive int
"nth must be greater than 0: received 0",
);
});

Deno.test("expect().toHaveBeenNthCalledWith() with custom error message", () => {
const msg = "toHaveBeenNthCalledWith custom error message";
const mockFn = fn();

mockFn(1, 2, 3);
mockFn(4, 5, 6);
mockFn(7, 8, 9);

assertThrows(
() => {
expect(mockFn, msg).not.toHaveBeenNthCalledWith(1, 1, 2, 3);
},
AssertionError,
msg,
);
assertThrows(
() => {
expect(mockFn, msg).toHaveBeenNthCalledWith(1, 4, 5, 6);
},
AssertionError,
msg,
);
});
24 changes: 24 additions & 0 deletions expect/_to_have_last_returned_with_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,27 @@ Deno.test("expect().toHaveLastReturnedWith()", () => {
expect(mockFn).not.toHaveLastReturnedWith(7);
}, AssertionError);
});

Deno.test("expect().toHaveLastReturnedWith() with custom error message", () => {
const msg = "toHaveLastReturnedWith custom error message";
const mockFn = fn((x: number) => x + 3);

mockFn(1);
mockFn(4);

assertThrows(
() => {
expect(mockFn, msg).toHaveLastReturnedWith(4);
},
AssertionError,
msg,
);

assertThrows(
() => {
expect(mockFn, msg).not.toHaveLastReturnedWith(7);
},
AssertionError,
msg,
);
});
20 changes: 20 additions & 0 deletions expect/_to_have_length_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,23 @@ Deno.test("expect().toHaveLength()", () => {
expect("abc").not.toHaveLength(3);
}, AssertionError);
});

Deno.test("expect().toHaveLength() with custom error message", () => {
const msg = "toHaveLength Custom Error";

assertThrows(
() => {
expect([1, 2, 3], msg).toHaveLength(4);
},
AssertionError,
msg,
);

assertThrows(
() => {
expect("abc", msg).not.toHaveLength(3);
},
AssertionError,
msg,
);
});
Loading