From f580f8d738caab9b606e72f4917e05637ac57fe1 Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Thu, 28 Nov 2024 16:05:13 +0800 Subject: [PATCH 01/11] feat: add custom message --- expect/_matchers.ts | 211 +++++++++++------- expect/_to_be_close_to_test.ts | 18 ++ expect/_to_be_falsy_test.ts | 19 ++ expect/_to_be_greater_than_or_equal_test.ts | 20 ++ expect/_to_be_greater_than_test.ts | 20 ++ expect/_to_be_less_than_or_equal_test.ts | 20 ++ expect/_to_be_less_than_test.ts | 20 ++ expect/_to_be_truthy_test.ts | 19 ++ expect/_to_contain_equal_test.ts | 21 ++ expect/_to_contain_test.ts | 20 ++ expect/_to_have_been_called_test.ts | 18 ++ expect/_to_have_been_called_times_test.ts | 22 ++ expect/_to_have_been_called_with_test.ts | 22 ++ expect/_to_have_been_last_called_with_test.ts | 24 ++ expect/_to_have_been_nth_called_with_test.ts | 24 ++ expect/_to_have_last_returned_with_test.ts | 24 ++ expect/_to_have_length_test.ts | 20 ++ expect/_to_have_nth_returned_with_test.ts | 26 +++ expect/_to_have_property_test.ts | 24 ++ expect/_to_have_returned_test.ts | 31 +++ expect/_to_have_returned_times_test.ts | 24 ++ expect/_to_have_returned_with_test.ts | 24 ++ expect/_to_match_object_test.ts | 45 ++++ expect/_to_throw_test.ts | 21 ++ 24 files changed, 657 insertions(+), 80 deletions(-) diff --git a/expect/_matchers.ts b/expect/_matchers.ts index 7a9abd5434ea..0e298025a930 100644 --- a/expect/_matchers.ts +++ b/expect/_matchers.ts @@ -89,13 +89,15 @@ export function toBeCloseTo( if (context.isNot) { if (pass) { throw new AssertionError( - `Expected the value not to be close to ${expected} (using ${numDigits} digits), but it is`, + context.customMessage ?? + `Expected the value not to be close to ${expected} (using ${numDigits} digits), but it is`, ); } } else { if (!pass) { throw new AssertionError( - `Expected the value (${value} to be close to ${expected} (using ${numDigits} digits), but it is not`, + context.customMessage ?? + `Expected the value (${value} to be close to ${expected} (using ${numDigits} digits), but it is not`, ); } } @@ -128,13 +130,15 @@ export function toBeFalsy( if (context.isNot) { if (isFalsy) { throw new AssertionError( - `Expected ${context.value} to NOT be falsy`, + context.customMessage ?? + `Expected ${context.value} to NOT be falsy`, ); } } else { if (!isFalsy) { throw new AssertionError( - `Expected ${context.value} to be falsy`, + context.customMessage ?? + `Expected ${context.value} to be falsy`, ); } } @@ -147,13 +151,15 @@ export function toBeTruthy( if (context.isNot) { if (isTruthy) { throw new AssertionError( - `Expected ${context.value} to NOT be truthy`, + context.customMessage ?? + `Expected ${context.value} to NOT be truthy`, ); } } else { if (!isTruthy) { throw new AssertionError( - `Expected ${context.value} to be truthy`, + context.customMessage ?? + `Expected ${context.value} to be truthy`, ); } } @@ -167,13 +173,15 @@ export function toBeGreaterThanOrEqual( if (context.isNot) { if (isGreaterOrEqual) { throw new AssertionError( - `Expected ${context.value} to NOT be greater than or equal ${expected}`, + context.customMessage ?? + `Expected ${context.value} to NOT be greater than or equal ${expected}`, ); } } else { if (!isGreaterOrEqual) { throw new AssertionError( - `Expected ${context.value} to be greater than or equal ${expected}`, + context.customMessage ?? + `Expected ${context.value} to be greater than or equal ${expected}`, ); } } @@ -187,13 +195,15 @@ export function toBeGreaterThan( if (context.isNot) { if (isGreater) { throw new AssertionError( - `Expected ${context.value} to NOT be greater than ${expected}`, + context.customMessage ?? + `Expected ${context.value} to NOT be greater than ${expected}`, ); } } else { if (!isGreater) { throw new AssertionError( - `Expected ${context.value} to be greater than ${expected}`, + context.customMessage ?? + `Expected ${context.value} to be greater than ${expected}`, ); } } @@ -217,13 +227,15 @@ export function toBeLessThanOrEqual( if (context.isNot) { if (isLower) { throw new AssertionError( - `Expected ${context.value} to NOT be lower than or equal ${expected}`, + context.customMessage ?? + `Expected ${context.value} to NOT be lower than or equal ${expected}`, ); } } else { if (!isLower) { throw new AssertionError( - `Expected ${context.value} to be lower than or equal ${expected}`, + context.customMessage ?? + `Expected ${context.value} to be lower than or equal ${expected}`, ); } } @@ -236,13 +248,15 @@ export function toBeLessThan( if (context.isNot) { if (isLower) { throw new AssertionError( - `Expected ${context.value} to NOT be lower than ${expected}`, + context.customMessage ?? + `Expected ${context.value} to NOT be lower than ${expected}`, ); } } else { if (!isLower) { throw new AssertionError( - `Expected ${context.value} to be lower than ${expected}`, + context.customMessage ?? + `Expected ${context.value} to be lower than ${expected}`, ); } } @@ -298,13 +312,15 @@ export function toHaveLength( if (context.isNot) { if (hasLength) { throw new AssertionError( - `Expected value not to have length ${expected}, but it does`, + context.customMessage ?? + `Expected value not to have length ${expected}, but it does`, ); } } else { if (!hasLength) { throw new AssertionError( - `Expected value to have length ${expected}, but it does not: the value has length ${maybeLength}`, + context.customMessage ?? + `Expected value to have length ${expected}, but it does not: the value has length ${maybeLength}`, ); } } @@ -353,17 +369,19 @@ export function toHaveProperty( if (context.isNot) { if (hasProperty) { throw new AssertionError( - `Expected the value not to have the property ${ - propPath.join(".") - }${ofValue}, but it does`, + context.customMessage ?? + `Expected the value not to have the property ${ + propPath.join(".") + }${ofValue}, but it does`, ); } } else { if (!hasProperty) { throw new AssertionError( - `Expected the value to have the property ${ - propPath.join(".") - }${ofValue}, but it does not`, + context.customMessage ?? + `Expected the value to have the property ${ + propPath.join(".") + }${ofValue}, but it does not`, ); } } @@ -382,13 +400,15 @@ export function toContain( if (context.isNot) { if (doesContain) { throw new AssertionError( - `The value ${fmtValue} contains the expected item ${fmtExpected}`, + context.customMessage ?? + `The value ${fmtValue} contains the expected item ${fmtExpected}`, ); } } else { if (!doesContain) { throw new AssertionError( - `The value ${fmtValue} doesn't contain the expected item ${fmtExpected}`, + context.customMessage ?? + `The value ${fmtValue} doesn't contain the expected item ${fmtExpected}`, ); } } @@ -420,7 +440,8 @@ export function toContainEqual( if (context.isNot) { if (doesContain) { throw new AssertionError( - `The value contains the expected item: + context.customMessage ?? + `The value contains the expected item: Value: ${fmtValue} Expected: ${fmtExpected}`, ); @@ -428,7 +449,8 @@ Expected: ${fmtExpected}`, } else { if (!doesContain) { throw new AssertionError( - `The value doesn't contain the expected item: + context.customMessage ?? + `The value doesn't contain the expected item: Value: ${fmtValue} Expected: ${fmtExpected}`, ); @@ -468,11 +490,15 @@ export function toMatchObject( const received = context.value; if (typeof received !== "object" || received === null) { - throw new AssertionError("Received value must be an object"); + throw new AssertionError( + context.customMessage ?? "Received value must be an object", + ); } if (typeof expected !== "object" || expected === null) { - throw new AssertionError("Received value must be an object"); + throw new AssertionError( + context.customMessage ?? "Received value must be an object", + ); } const pass = equal(context.value, expected, { @@ -488,7 +514,8 @@ export function toMatchObject( const actualString = format(context.value); const expectedString = format(expected); throw new AssertionError( - `Expected ${actualString} to NOT match ${expectedString}`, + context.customMessage ?? + `Expected ${actualString} to NOT match ${expectedString}`, ); }; @@ -504,13 +531,15 @@ export function toHaveBeenCalled(context: MatcherContext): MatchResult { if (context.isNot) { if (hasBeenCalled) { throw new AssertionError( - `Expected mock function not to be called, but it was called ${calls.length} time(s)`, + context.customMessage ?? + `Expected mock function not to be called, but it was called ${calls.length} time(s)`, ); } } else { if (!hasBeenCalled) { throw new AssertionError( - `Expected mock function to be called, but it was not called`, + context.customMessage ?? + `Expected mock function to be called, but it was not called`, ); } } @@ -525,13 +554,15 @@ export function toHaveBeenCalledTimes( if (context.isNot) { if (calls.length === expected) { throw new AssertionError( - `Expected mock function not to be called ${expected} time(s), but it was`, + context.customMessage ?? + `Expected mock function not to be called ${expected} time(s), but it was`, ); } } else { if (calls.length !== expected) { throw new AssertionError( - `Expected mock function to be called ${expected} time(s), but it was called ${calls.length} time(s)`, + context.customMessage ?? + `Expected mock function to be called ${expected} time(s), but it was called ${calls.length} time(s)`, ); } } @@ -547,9 +578,10 @@ export function toHaveBeenCalledWith( if (context.isNot) { if (hasBeenCalled) { throw new AssertionError( - `Expected mock function not to be called with ${ - inspectArgs(expected) - }, but it was`, + context.customMessage ?? + `Expected mock function not to be called with ${ + inspectArgs(expected) + }, but it was`, ); } } else { @@ -561,9 +593,10 @@ export function toHaveBeenCalledWith( }`; } throw new AssertionError( - `Expected mock function to be called with ${ - inspectArgs(expected) - }, but it was not.${otherCalls}`, + context.customMessage ?? + `Expected mock function to be called with ${ + inspectArgs(expected) + }, but it was not.${otherCalls}`, ); } } @@ -579,9 +612,10 @@ export function toHaveBeenLastCalledWith( if (context.isNot) { if (hasBeenCalled) { throw new AssertionError( - `Expected mock function not to be last called with ${ - inspectArgs(expected) - }, but it was`, + context.customMessage ?? + `Expected mock function not to be last called with ${ + inspectArgs(expected) + }, but it was`, ); } } else { @@ -589,15 +623,17 @@ export function toHaveBeenLastCalledWith( const lastCall = calls.at(-1); if (!lastCall) { throw new AssertionError( - `Expected mock function to be last called with ${ - inspectArgs(expected) - }, but it was not`, + context.customMessage ?? + `Expected mock function to be last called with ${ + inspectArgs(expected) + }, but it was not`, ); } else { throw new AssertionError( - `Expected mock function to be last called with ${ - inspectArgs(expected) - }, but it was last called with ${inspectArgs(lastCall.args)}`, + context.customMessage ?? + `Expected mock function to be last called with ${ + inspectArgs(expected) + }, but it was last called with ${inspectArgs(lastCall.args)}`, ); } } @@ -620,9 +656,10 @@ export function toHaveBeenNthCalledWith( if (context.isNot) { if (hasBeenCalled) { throw new AssertionError( - `Expected the n-th call (n=${nth}) of mock function is not with ${ - inspectArgs(expected) - }, but it was`, + context.customMessage ?? + `Expected the n-th call (n=${nth}) of mock function is not with ${ + inspectArgs(expected) + }, but it was`, ); } } else { @@ -630,15 +667,17 @@ export function toHaveBeenNthCalledWith( const nthCall = calls[callIndex]; if (!nthCall) { throw new AssertionError( - `Expected the n-th call (n=${nth}) of mock function is with ${ - inspectArgs(expected) - }, but the n-th call does not exist`, + context.customMessage ?? + `Expected the n-th call (n=${nth}) of mock function is with ${ + inspectArgs(expected) + }, but the n-th call does not exist`, ); } else { throw new AssertionError( - `Expected the n-th call (n=${nth}) of mock function is with ${ - inspectArgs(expected) - }, but it was with ${inspectArgs(nthCall.args)}`, + context.customMessage ?? + `Expected the n-th call (n=${nth}) of mock function is with ${ + inspectArgs(expected) + }, but it was with ${inspectArgs(nthCall.args)}`, ); } } @@ -652,13 +691,15 @@ export function toHaveReturned(context: MatcherContext): MatchResult { if (context.isNot) { if (returned.length > 0) { throw new AssertionError( - `Expected the mock function to not have returned, but it returned ${returned.length} times`, + context.customMessage ?? + `Expected the mock function to not have returned, but it returned ${returned.length} times`, ); } } else { if (returned.length === 0) { throw new AssertionError( - `Expected the mock function to have returned, but it did not return`, + context.customMessage ?? + `Expected the mock function to have returned, but it did not return`, ); } } @@ -674,13 +715,15 @@ export function toHaveReturnedTimes( if (context.isNot) { if (returned.length === expected) { throw new AssertionError( - `Expected the mock function to not have returned ${expected} times, but it returned ${returned.length} times`, + context.customMessage ?? + `Expected the mock function to not have returned ${expected} times, but it returned ${returned.length} times`, ); } } else { if (returned.length !== expected) { throw new AssertionError( - `Expected the mock function to have returned ${expected} times, but it returned ${returned.length} times`, + context.customMessage ?? + `Expected the mock function to have returned ${expected} times, but it returned ${returned.length} times`, ); } } @@ -698,17 +741,19 @@ export function toHaveReturnedWith( if (context.isNot) { if (returnedWithExpected) { throw new AssertionError( - `Expected the mock function to not have returned with ${ - inspectArg(expected) - }, but it did`, + context.customMessage ?? + `Expected the mock function to not have returned with ${ + inspectArg(expected) + }, but it did`, ); } } else { if (!returnedWithExpected) { throw new AssertionError( - `Expected the mock function to have returned with ${ - inspectArg(expected) - }, but it did not`, + context.customMessage ?? + `Expected the mock function to have returned with ${ + inspectArg(expected) + }, but it did not`, ); } } @@ -726,17 +771,19 @@ export function toHaveLastReturnedWith( if (context.isNot) { if (lastReturnedWithExpected) { throw new AssertionError( - `Expected the mock function to not have last returned with ${ - inspectArg(expected) - }, but it did`, + context.customMessage ?? + `Expected the mock function to not have last returned with ${ + inspectArg(expected) + }, but it did`, ); } } else { if (!lastReturnedWithExpected) { throw new AssertionError( - `Expected the mock function to have last returned with ${ - inspectArg(expected) - }, but it did not`, + context.customMessage ?? + `Expected the mock function to have last returned with ${ + inspectArg(expected) + }, but it did not`, ); } } @@ -761,17 +808,19 @@ export function toHaveNthReturnedWith( if (context.isNot) { if (nthReturnedWithExpected) { throw new AssertionError( - `Expected the mock function to not have n-th (n=${nth}) returned with ${ - inspectArg(expected) - }, but it did`, + context.customMessage ?? + `Expected the mock function to not have n-th (n=${nth}) returned with ${ + inspectArg(expected) + }, but it did`, ); } } else { if (!nthReturnedWithExpected) { throw new AssertionError( - `Expected the mock function to have n-th (n=${nth}) returned with ${ - inspectArg(expected) - }, but it did not`, + context.customMessage ?? + `Expected the mock function to have n-th (n=${nth}) returned with ${ + inspectArg(expected) + }, but it did not`, ); } } @@ -815,7 +864,9 @@ export function toThrow( context.customMessage, ); isError = true; - throw new AssertionError(`Expected to NOT throw ${expected}`); + throw new AssertionError( + context.customMessage ?? `Expected to NOT throw ${expected}`, + ); } catch (e) { if (isError) { throw e; diff --git a/expect/_to_be_close_to_test.ts b/expect/_to_be_close_to_test.ts index 75b2cfca1fcf..faacc9cbe9f0 100644 --- a/expect/_to_be_close_to_test.ts +++ b/expect/_to_be_close_to_test.ts @@ -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, + ); +}); diff --git a/expect/_to_be_falsy_test.ts b/expect/_to_be_falsy_test.ts index be62a949501f..e6354f056625 100644 --- a/expect/_to_be_falsy_test.ts +++ b/expect/_to_be_falsy_test.ts @@ -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, + ); +}); diff --git a/expect/_to_be_greater_than_or_equal_test.ts b/expect/_to_be_greater_than_or_equal_test.ts index 541d94c82b7f..62d50c2b007d 100644 --- a/expect/_to_be_greater_than_or_equal_test.ts +++ b/expect/_to_be_greater_than_or_equal_test.ts @@ -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, + ); +}); diff --git a/expect/_to_be_greater_than_test.ts b/expect/_to_be_greater_than_test.ts index 9b3044e902ff..debe4092242d 100644 --- a/expect/_to_be_greater_than_test.ts +++ b/expect/_to_be_greater_than_test.ts @@ -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, + ); +}); diff --git a/expect/_to_be_less_than_or_equal_test.ts b/expect/_to_be_less_than_or_equal_test.ts index 7c9a241d35bd..50d770289cd8 100644 --- a/expect/_to_be_less_than_or_equal_test.ts +++ b/expect/_to_be_less_than_or_equal_test.ts @@ -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, + ); +}); diff --git a/expect/_to_be_less_than_test.ts b/expect/_to_be_less_than_test.ts index 8b2fd253df79..506b00585dd5 100644 --- a/expect/_to_be_less_than_test.ts +++ b/expect/_to_be_less_than_test.ts @@ -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, + ); +}); diff --git a/expect/_to_be_truthy_test.ts b/expect/_to_be_truthy_test.ts index 6dab21c8f28b..bdd22fd38e52 100644 --- a/expect/_to_be_truthy_test.ts +++ b/expect/_to_be_truthy_test.ts @@ -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, + ); +}); diff --git a/expect/_to_contain_equal_test.ts b/expect/_to_contain_equal_test.ts index 49fa2637afbe..6811530ec15b 100644 --- a/expect/_to_contain_equal_test.ts +++ b/expect/_to_contain_equal_test.ts @@ -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, + ); +}); diff --git a/expect/_to_contain_test.ts b/expect/_to_contain_test.ts index 0275dc0cb182..fd01143f1537 100644 --- a/expect/_to_contain_test.ts +++ b/expect/_to_contain_test.ts @@ -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, + ); +}); diff --git a/expect/_to_have_been_called_test.ts b/expect/_to_have_been_called_test.ts index 13c712e1a102..767846a340b1 100644 --- a/expect/_to_have_been_called_test.ts +++ b/expect/_to_have_been_called_test.ts @@ -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, + ); +}); diff --git a/expect/_to_have_been_called_times_test.ts b/expect/_to_have_been_called_times_test.ts index 7b620bb73e2f..85c5b5df70cc 100644 --- a/expect/_to_have_been_called_times_test.ts +++ b/expect/_to_have_been_called_times_test.ts @@ -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, + ); +}); diff --git a/expect/_to_have_been_called_with_test.ts b/expect/_to_have_been_called_with_test.ts index 95ecacae103e..3a58572f740c 100644 --- a/expect/_to_have_been_called_with_test.ts +++ b/expect/_to_have_been_called_with_test.ts @@ -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, + ); +}); diff --git a/expect/_to_have_been_last_called_with_test.ts b/expect/_to_have_been_last_called_with_test.ts index f9cd98bc5892..862de7f2c8f4 100644 --- a/expect/_to_have_been_last_called_with_test.ts +++ b/expect/_to_have_been_last_called_with_test.ts @@ -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, + ); +}); diff --git a/expect/_to_have_been_nth_called_with_test.ts b/expect/_to_have_been_nth_called_with_test.ts index 20650ef58bcc..9fa072466283 100644 --- a/expect/_to_have_been_nth_called_with_test.ts +++ b/expect/_to_have_been_nth_called_with_test.ts @@ -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, + ); +}); diff --git a/expect/_to_have_last_returned_with_test.ts b/expect/_to_have_last_returned_with_test.ts index b889cda9141e..b363b8259215 100644 --- a/expect/_to_have_last_returned_with_test.ts +++ b/expect/_to_have_last_returned_with_test.ts @@ -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, + ); +}); diff --git a/expect/_to_have_length_test.ts b/expect/_to_have_length_test.ts index b6ecb5243c88..d1e1522a650d 100644 --- a/expect/_to_have_length_test.ts +++ b/expect/_to_have_length_test.ts @@ -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, + ); +}); diff --git a/expect/_to_have_nth_returned_with_test.ts b/expect/_to_have_nth_returned_with_test.ts index 8865f58bdb99..6d80ba4c1335 100644 --- a/expect/_to_have_nth_returned_with_test.ts +++ b/expect/_to_have_nth_returned_with_test.ts @@ -34,3 +34,29 @@ Deno.test("expect().toHaveNthReturnedWith()", () => { expect(mockFn).toHaveNthReturnedWith(0, 0); }, Error); }); + +Deno.test("expect().toHaveNthReturnedWith() with custom error message", () => { + const msg = "toHaveNthReturnedWith custom error message"; + const mockFn = fn((x: number) => x + 7); + + mockFn(1); + mockFn(10); + mockFn(100); + mockFn(1000); + + assertThrows( + () => { + expect(mockFn, msg).toHaveNthReturnedWith(1, 1); + }, + AssertionError, + msg, + ); + + assertThrows( + () => { + expect(mockFn, msg).not.toHaveNthReturnedWith(1, 8); + }, + AssertionError, + msg, + ); +}); diff --git a/expect/_to_have_property_test.ts b/expect/_to_have_property_test.ts index 1d1b19344392..d91f0470c4f4 100644 --- a/expect/_to_have_property_test.ts +++ b/expect/_to_have_property_test.ts @@ -26,3 +26,27 @@ Deno.test("expect().toHaveProperty() handles null and undefined", () => { expect(null).not.toHaveProperty("foo"); expect(undefined).not.toHaveProperty("foo"); }); + +Deno.test("expect().toHaveProperty() with custom error message", () => { + const msg = "toHaveProperty Custom Error"; + + assertThrows( + () => { + expect({ a: { b: { c: { d: 5 } } } }, msg).toHaveProperty("a.b.c", { + d: 6, + }); + }, + AssertionError, + msg, + ); + + assertThrows( + () => { + expect({ a: { b: { c: { d: 5 } } } }, msg).not.toHaveProperty("a.b.c", { + d: 5, + }); + }, + AssertionError, + msg, + ); +}); diff --git a/expect/_to_have_returned_test.ts b/expect/_to_have_returned_test.ts index 3f19350ef5e3..9a9fd4c8b76d 100644 --- a/expect/_to_have_returned_test.ts +++ b/expect/_to_have_returned_test.ts @@ -29,3 +29,34 @@ Deno.test("expect().toHaveReturned()", () => { expect(mockFn0).not.toHaveReturned(); }, AssertionError); }); + +Deno.test("expect().toHaveReturned() with custom error message", () => { + const msg = "toHaveReturned custom error message"; + const mockFn0 = fn(); + const mockFn1 = fn(() => { + throw new Error("foo"); + }); + + mockFn0(); + try { + mockFn1(); + } catch { + // ignore + } + + assertThrows( + () => { + expect(mockFn1, msg).toHaveReturned(); + }, + AssertionError, + msg, + ); + + assertThrows( + () => { + expect(mockFn0, msg).not.toHaveReturned(); + }, + AssertionError, + msg, + ); +}); diff --git a/expect/_to_have_returned_times_test.ts b/expect/_to_have_returned_times_test.ts index 8dfc7d61fb5d..0ceb66aecb71 100644 --- a/expect/_to_have_returned_times_test.ts +++ b/expect/_to_have_returned_times_test.ts @@ -22,3 +22,27 @@ Deno.test("expect().toHaveReturnedTimes()", () => { expect(mockFn).not.toHaveReturnedTimes(2); }, AssertionError); }); + +Deno.test("expect().toHaveReturnedTimes() with custom error message", () => { + const msg = "toHaveReturnedTimes custom error message"; + const mockFn = fn(); + + mockFn(); + mockFn(); + + assertThrows( + () => { + expect(mockFn, msg).toHaveReturnedTimes(1); + }, + AssertionError, + msg, + ); + + assertThrows( + () => { + expect(mockFn, msg).not.toHaveReturnedTimes(2); + }, + AssertionError, + msg, + ); +}); diff --git a/expect/_to_have_returned_with_test.ts b/expect/_to_have_returned_with_test.ts index 4bc63c0d1342..c3da8f5c6872 100644 --- a/expect/_to_have_returned_with_test.ts +++ b/expect/_to_have_returned_with_test.ts @@ -22,3 +22,27 @@ Deno.test("expect().toHaveReturnedWith()", () => { expect(mockFn).not.toHaveReturnedWith({ foo: 7 }); }, AssertionError); }); + +Deno.test("expect().toHaveReturnedWith() with custom error message", () => { + const msg = "toHaveReturnedWith custom error message"; + const mockFn = fn((x: number) => ({ foo: x + 1 })); + + mockFn(5); + mockFn(6); + + assertThrows( + () => { + expect(mockFn, msg).toHaveReturnedWith({ foo: 5 }); + }, + AssertionError, + msg, + ); + + assertThrows( + () => { + expect(mockFn, msg).not.toHaveReturnedWith({ foo: 7 }); + }, + AssertionError, + msg, + ); +}); diff --git a/expect/_to_match_object_test.ts b/expect/_to_match_object_test.ts index 144e51b929bc..0ab549970790 100644 --- a/expect/_to_match_object_test.ts +++ b/expect/_to_match_object_test.ts @@ -68,3 +68,48 @@ Deno.test("expect(),toMatchObject() with asyAsymmetric matcher", () => { }, }); }); + +Deno.test("expect().toMatchObject() with custom error message", () => { + const house0 = { + bath: true, + bedrooms: 4, + kitchen: { + amenities: ["oven", "stove", "washer"], + area: 20, + wallColor: "white", + }, + }; + const house1 = { + bath: true, + bedrooms: 4, + kitchen: { + amenities: ["oven", "stove"], + area: 20, + wallColor: "white", + }, + }; + const desiredHouse = { + bath: true, + kitchen: { + amenities: ["oven", "stove", "washer"], + wallColor: "white", + }, + }; + const msg = "toMatchObject Custom Error"; + + assertThrows( + () => { + expect([house1], msg).toMatchObject([desiredHouse]); + }, + AssertionError, + msg, + ); + + assertThrows( + () => { + expect(house0, msg).not.toMatchObject(desiredHouse); + }, + AssertionError, + msg, + ); +}); diff --git a/expect/_to_throw_test.ts b/expect/_to_throw_test.ts index a205ebf9801f..1c9e04a28931 100644 --- a/expect/_to_throw_test.ts +++ b/expect/_to_throw_test.ts @@ -100,3 +100,24 @@ Deno.test("expect().toThrow()", () => { 'Expected error message to include /\\d/, but got "hello world".', ); }); + +Deno.test("expect().toThrow() with custom error message", () => { + const msg = "toThrow custom error message"; + assertThrows( + () => { + expect(() => {}, msg).toThrow(); + }, + AssertionError, + msg, + ); + + assertThrows( + () => { + expect(() => { + throw new Error("hello world"); + }, msg).not.toThrow(); + }, + AssertionError, + msg, + ); +}); From 6452186c89ff56351abef15c09ff092d9f38b016 Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Thu, 28 Nov 2024 16:20:08 +0800 Subject: [PATCH 02/11] feat: add test case --- expect/_to_match_object_test.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/expect/_to_match_object_test.ts b/expect/_to_match_object_test.ts index 0ab549970790..6a82353f40b2 100644 --- a/expect/_to_match_object_test.ts +++ b/expect/_to_match_object_test.ts @@ -112,4 +112,12 @@ Deno.test("expect().toMatchObject() with custom error message", () => { AssertionError, msg, ); + + assertThrows( + () => { + expect(null, msg).toMatchObject([desiredHouse]); + }, + AssertionError, + msg, + ); }); From 1ee7c7f45b55520a866ca7f0c82c3fb8b5ee0989 Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Sun, 1 Dec 2024 23:12:42 +0800 Subject: [PATCH 03/11] fix: align to vitest --- assert/is_error.ts | 11 +- expect/_assert_equals_test.ts | 2 +- expect/_assert_not_equals.ts | 2 +- expect/_build_message.ts | 8 +- expect/_matchers.ts | 392 ++++++++++++------ expect/_to_be_close_to_test.ts | 14 +- expect/_to_be_falsy_test.ts | 18 +- expect/_to_be_greater_than_or_equal_test.ts | 18 +- expect/_to_be_greater_than_test.ts | 18 +- expect/_to_be_less_than_or_equal_test.ts | 18 +- expect/_to_be_less_than_test.ts | 18 +- expect/_to_be_truthy_test.ts | 18 +- expect/_to_contain_equal_test.ts | 10 +- expect/_to_contain_test.ts | 18 +- expect/_to_equal_test.ts | 8 +- expect/_to_have_been_called_test.ts | 10 +- expect/_to_have_been_called_times_test.ts | 18 +- expect/_to_have_been_called_with_test.ts | 18 +- expect/_to_have_been_last_called_with_test.ts | 10 +- expect/_to_have_been_nth_called_with_test.ts | 18 +- expect/_to_have_last_returned_with_test.ts | 10 +- expect/_to_have_length_test.ts | 10 +- expect/_to_have_nth_returned_with_test.ts | 18 +- expect/_to_have_property_test.ts | 20 +- expect/_to_have_returned_test.ts | 18 +- expect/_to_have_returned_times_test.ts | 18 +- expect/_to_have_returned_with_test.ts | 18 +- expect/_to_match_object_test.ts | 27 +- expect/_to_throw_test.ts | 17 +- 29 files changed, 409 insertions(+), 394 deletions(-) diff --git a/assert/is_error.ts b/assert/is_error.ts index 8f06aac4a00c..8fc599d437c9 100644 --- a/assert/is_error.ts +++ b/assert/is_error.ts @@ -33,15 +33,15 @@ export function assertIsError( msgMatches?: string | RegExp, msg?: string, ): asserts error is E { - const msgSuffix = msg ? `: ${msg}` : "."; + const msgPrefix = msg ? `${msg}: ` : ""; if (!(error instanceof Error)) { throw new AssertionError( - `Expected "error" to be an Error object${msgSuffix}`, + `${msgPrefix}Expected "error" to be an Error object.`, ); } if (ErrorClass && !(error instanceof ErrorClass)) { msg = - `Expected error to be instance of "${ErrorClass.name}", but was "${error?.constructor?.name}"${msgSuffix}`; + `${msgPrefix}Expected error to be instance of "${ErrorClass.name}", but was "${error?.constructor?.name}".`; throw new AssertionError(msg); } let msgCheck; @@ -55,11 +55,12 @@ export function assertIsError( } if (msgMatches && !msgCheck) { - msg = `Expected error message to include ${ + msg = `${msgPrefix}Expected error message to include ${ msgMatches instanceof RegExp ? msgMatches.toString() : JSON.stringify(msgMatches) - }, but got ${JSON.stringify(error?.message)}${msgSuffix}`; + }, but got ${JSON.stringify(error?.message)}.`; + console.log("msg::", msg); throw new AssertionError(msg); } } diff --git a/expect/_assert_equals_test.ts b/expect/_assert_equals_test.ts index ce84ce9ea0f1..8106455ff8f2 100644 --- a/expect/_assert_equals_test.ts +++ b/expect/_assert_equals_test.ts @@ -145,7 +145,7 @@ Deno.test({ () => assertEquals(1, 2, { msg: "CUSTOM MESSAGE" }), AssertionError, [ - "Values are not equal: CUSTOM MESSAGE", + "CUSTOM MESSAGE: Values are not equal.", ...createHeader(), removed(`- ${yellow("1")}`), added(`+ ${yellow("2")}`), diff --git a/expect/_assert_not_equals.ts b/expect/_assert_not_equals.ts index caecc22af134..fc160aca2452 100644 --- a/expect/_assert_not_equals.ts +++ b/expect/_assert_not_equals.ts @@ -30,6 +30,6 @@ export function assertNotEquals( return; } - const message = buildNotEqualErrorMessage(actual, expected, options); + const message = buildNotEqualErrorMessage(actual, expected, options ?? {}); throw new AssertionError(message); } diff --git a/expect/_build_message.ts b/expect/_build_message.ts index e13fd3058df5..e1eb3a2d1663 100644 --- a/expect/_build_message.ts +++ b/expect/_build_message.ts @@ -21,11 +21,11 @@ export function buildEqualErrorMessage( options: EqualErrorMessageOptions, ): string { const { formatter = format, msg } = options ?? {}; - const msgSuffix = msg ? `: ${msg}` : "."; + const msgPrefix = msg ? `${msg}: ` : ""; const actualString = formatter(actual); const expectedString = formatter(expected); - let message = `Values are not equal${msgSuffix}`; + let message = `${msgPrefix}Values are not equal.`; const stringDiff = isString(actual) && isString(expected); const diffResult = stringDiff @@ -46,6 +46,6 @@ export function buildNotEqualErrorMessage( const actualString = String(actual); const expectedString = String(expected); - const msgSuffix = msg ? `: ${msg}` : "."; - return `Expected actual: ${actualString} not to be: ${expectedString}${msgSuffix}`; + const msgPrefix = msg ? `${msg}: ` : ""; + return `${msgPrefix}Expected actual: ${actualString} not to be: ${expectedString}.`; } diff --git a/expect/_matchers.ts b/expect/_matchers.ts index 0e298025a930..858cb95a1683 100644 --- a/expect/_matchers.ts +++ b/expect/_matchers.ts @@ -88,16 +88,22 @@ export function toBeCloseTo( if (context.isNot) { if (pass) { + const defaultMessage = + `Expected the value not to be close to ${expected} (using ${numDigits} digits), but it is`; throw new AssertionError( - context.customMessage ?? - `Expected the value not to be close to ${expected} (using ${numDigits} digits), but it is`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } else { if (!pass) { + const defaultMessage = + `Expected the value (${value} to be close to ${expected} (using ${numDigits} digits), but it is not`; throw new AssertionError( - context.customMessage ?? - `Expected the value (${value} to be close to ${expected} (using ${numDigits} digits), but it is not`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } @@ -129,16 +135,20 @@ export function toBeFalsy( const isFalsy = !(context.value); if (context.isNot) { if (isFalsy) { + const defaultMessage = `Expected ${context.value} to NOT be falsy`; throw new AssertionError( - context.customMessage ?? - `Expected ${context.value} to NOT be falsy`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } else { if (!isFalsy) { + const defaultMessage = `Expected ${context.value} to be falsy`; throw new AssertionError( - context.customMessage ?? - `Expected ${context.value} to be falsy`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } @@ -150,16 +160,20 @@ export function toBeTruthy( const isTruthy = !!(context.value); if (context.isNot) { if (isTruthy) { + const defaultMessage = `Expected ${context.value} to NOT be truthy`; throw new AssertionError( - context.customMessage ?? - `Expected ${context.value} to NOT be truthy`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } else { if (!isTruthy) { + const defaultMessage = `Expected ${context.value} to be truthy`; throw new AssertionError( - context.customMessage ?? - `Expected ${context.value} to be truthy`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } @@ -172,16 +186,22 @@ export function toBeGreaterThanOrEqual( const isGreaterOrEqual = Number(context.value) >= Number(expected); if (context.isNot) { if (isGreaterOrEqual) { + const defaultMessage = + `Expected ${context.value} to NOT be greater than or equal ${expected}`; throw new AssertionError( - context.customMessage ?? - `Expected ${context.value} to NOT be greater than or equal ${expected}`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } else { if (!isGreaterOrEqual) { + const defaultMessage = + `Expected ${context.value} to be greater than or equal ${expected}`; throw new AssertionError( - context.customMessage ?? - `Expected ${context.value} to be greater than or equal ${expected}`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } @@ -194,16 +214,22 @@ export function toBeGreaterThan( const isGreater = Number(context.value) > Number(expected); if (context.isNot) { if (isGreater) { + const defaultMessage = + `Expected ${context.value} to NOT be greater than ${expected}`; throw new AssertionError( - context.customMessage ?? - `Expected ${context.value} to NOT be greater than ${expected}`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } else { if (!isGreater) { + const defaultMessage = + `Expected ${context.value} to be greater than ${expected}`; throw new AssertionError( - context.customMessage ?? - `Expected ${context.value} to be greater than ${expected}`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } @@ -226,16 +252,22 @@ export function toBeLessThanOrEqual( const isLower = Number(context.value) <= Number(expected); if (context.isNot) { if (isLower) { + const defaultMessage = + `Expected ${context.value} to NOT be lower than or equal ${expected}`; throw new AssertionError( - context.customMessage ?? - `Expected ${context.value} to NOT be lower than or equal ${expected}`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } else { if (!isLower) { + const defaultMessage = + `Expected ${context.value} to be lower than or equal ${expected}`; throw new AssertionError( - context.customMessage ?? - `Expected ${context.value} to be lower than or equal ${expected}`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } @@ -247,16 +279,22 @@ export function toBeLessThan( const isLower = Number(context.value) < Number(expected); if (context.isNot) { if (isLower) { + const defaultMessage = + `Expected ${context.value} to NOT be lower than ${expected}`; throw new AssertionError( - context.customMessage ?? - `Expected ${context.value} to NOT be lower than ${expected}`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } else { if (!isLower) { + const defaultMessage = + `Expected ${context.value} to be lower than ${expected}`; throw new AssertionError( - context.customMessage ?? - `Expected ${context.value} to be lower than ${expected}`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } @@ -311,16 +349,22 @@ export function toHaveLength( if (context.isNot) { if (hasLength) { + const defaultMessage = + `Expected value not to have length ${expected}, but it does`; throw new AssertionError( - context.customMessage ?? - `Expected value not to have length ${expected}, but it does`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } else { if (!hasLength) { + const defaultMessage = + `Expected value to have length ${expected}, but it does not: the value has length ${maybeLength}`; throw new AssertionError( - context.customMessage ?? - `Expected value to have length ${expected}, but it does not: the value has length ${maybeLength}`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } @@ -368,20 +412,24 @@ export function toHaveProperty( if (context.isNot) { if (hasProperty) { + const defaultMessage = `Expected the value not to have the property ${ + propPath.join(".") + }${ofValue}, but it does`; throw new AssertionError( - context.customMessage ?? - `Expected the value not to have the property ${ - propPath.join(".") - }${ofValue}, but it does`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } else { if (!hasProperty) { + const defaultMessage = `Expected the value to have the property ${ + propPath.join(".") + }${ofValue}, but it does not`; throw new AssertionError( - context.customMessage ?? - `Expected the value to have the property ${ - propPath.join(".") - }${ofValue}, but it does not`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } @@ -399,16 +447,22 @@ export function toContain( if (context.isNot) { if (doesContain) { + const defaultMessage = + `The value ${fmtValue} contains the expected item ${fmtExpected}`; throw new AssertionError( - context.customMessage ?? - `The value ${fmtValue} contains the expected item ${fmtExpected}`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } else { if (!doesContain) { + const defaultMessage = + `The value ${fmtValue} doesn't contain the expected item ${fmtExpected}`; throw new AssertionError( - context.customMessage ?? - `The value ${fmtValue} doesn't contain the expected item ${fmtExpected}`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } @@ -439,20 +493,24 @@ export function toContainEqual( if (context.isNot) { if (doesContain) { - throw new AssertionError( - context.customMessage ?? - `The value contains the expected item: + const defaultMessage = `The value contains the expected item: Value: ${fmtValue} -Expected: ${fmtExpected}`, +Expected: ${fmtExpected}`; + throw new AssertionError( + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } else { if (!doesContain) { - throw new AssertionError( - context.customMessage ?? - `The value doesn't contain the expected item: + const defaultMessage = `The value doesn't contain the expected item: Value: ${fmtValue} -Expected: ${fmtExpected}`, +Expected: ${fmtExpected}`; + throw new AssertionError( + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } @@ -489,15 +547,21 @@ export function toMatchObject( ): MatchResult { const received = context.value; + const defaultMsg = "Received value must be an object"; + if (typeof received !== "object" || received === null) { throw new AssertionError( - context.customMessage ?? "Received value must be an object", + context.customMessage + ? `${context.customMessage}: ${defaultMsg}` + : defaultMsg, ); } if (typeof expected !== "object" || expected === null) { throw new AssertionError( - context.customMessage ?? "Received value must be an object", + context.customMessage + ? `${context.customMessage}: ${defaultMsg}` + : defaultMsg, ); } @@ -513,9 +577,12 @@ export function toMatchObject( const triggerError = () => { const actualString = format(context.value); const expectedString = format(expected); + const defaultMessage = + `Expected ${actualString} to NOT match ${expectedString}`; throw new AssertionError( - context.customMessage ?? - `Expected ${actualString} to NOT match ${expectedString}`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); }; @@ -530,16 +597,22 @@ export function toHaveBeenCalled(context: MatcherContext): MatchResult { if (context.isNot) { if (hasBeenCalled) { + const defaultMessage = + `Expected mock function not to be called, but it was called ${calls.length} time(s)`; throw new AssertionError( - context.customMessage ?? - `Expected mock function not to be called, but it was called ${calls.length} time(s)`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } else { if (!hasBeenCalled) { + const defaultMessage = + "Expected mock function to be called, but it was not called"; throw new AssertionError( - context.customMessage ?? - `Expected mock function to be called, but it was not called`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } @@ -553,16 +626,22 @@ export function toHaveBeenCalledTimes( if (context.isNot) { if (calls.length === expected) { + const defaultMessage = + `Expected mock function not to be called ${expected} time(s), but it was`; throw new AssertionError( - context.customMessage ?? - `Expected mock function not to be called ${expected} time(s), but it was`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } else { if (calls.length !== expected) { + const defaultMessage = + `Expected mock function to be called ${expected} time(s), but it was called ${calls.length} time(s)`; throw new AssertionError( - context.customMessage ?? - `Expected mock function to be called ${expected} time(s), but it was called ${calls.length} time(s)`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } @@ -577,11 +656,13 @@ export function toHaveBeenCalledWith( if (context.isNot) { if (hasBeenCalled) { + const defaultMessage = `Expected mock function not to be called with ${ + inspectArgs(expected) + }, but it was`; throw new AssertionError( - context.customMessage ?? - `Expected mock function not to be called with ${ - inspectArgs(expected) - }, but it was`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } else { @@ -592,11 +673,14 @@ export function toHaveBeenCalledWith( calls.map((call) => inspectArgs(call.args)).join("\n ") }`; } + + const defaultMessage = `Expected mock function to be called with ${ + inspectArgs(expected) + }, but it was not.${otherCalls}`; throw new AssertionError( - context.customMessage ?? - `Expected mock function to be called with ${ - inspectArgs(expected) - }, but it was not.${otherCalls}`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } @@ -611,34 +695,42 @@ export function toHaveBeenLastCalledWith( if (context.isNot) { if (hasBeenCalled) { + const defaultMessage = + `Expected mock function not to be last called with ${ + inspectArgs(expected) + }, but it was`; throw new AssertionError( - context.customMessage ?? - `Expected mock function not to be last called with ${ - inspectArgs(expected) - }, but it was`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } else { if (!hasBeenCalled) { const lastCall = calls.at(-1); if (!lastCall) { + const defaultMessage = `Expected mock function to be last called with ${ + inspectArgs(expected) + }, but it was not`; throw new AssertionError( - context.customMessage ?? - `Expected mock function to be last called with ${ - inspectArgs(expected) - }, but it was not`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } else { + const defaultMessage = `Expected mock function to be last called with ${ + inspectArgs(expected) + }, but it was last called with ${inspectArgs(lastCall.args)}`; throw new AssertionError( - context.customMessage ?? - `Expected mock function to be last called with ${ - inspectArgs(expected) - }, but it was last called with ${inspectArgs(lastCall.args)}`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } } } + export function toHaveBeenNthCalledWith( context: MatcherContext, nth: number, @@ -655,29 +747,38 @@ export function toHaveBeenNthCalledWith( if (context.isNot) { if (hasBeenCalled) { + const defaultMessage = + `Expected the n-th call (n=${nth}) of mock function is not with ${ + inspectArgs(expected) + }, but it was`; throw new AssertionError( - context.customMessage ?? - `Expected the n-th call (n=${nth}) of mock function is not with ${ - inspectArgs(expected) - }, but it was`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } else { if (!hasBeenCalled) { const nthCall = calls[callIndex]; if (!nthCall) { + const defaultMessage = + `Expected the n-th call (n=${nth}) of mock function is with ${ + inspectArgs(expected) + }, but the n-th call does not exist`; throw new AssertionError( - context.customMessage ?? - `Expected the n-th call (n=${nth}) of mock function is with ${ - inspectArgs(expected) - }, but the n-th call does not exist`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } else { + const defaultMessage = + `Expected the n-th call (n=${nth}) of mock function is with ${ + inspectArgs(expected) + }, but it was with ${inspectArgs(nthCall.args)}`; throw new AssertionError( - context.customMessage ?? - `Expected the n-th call (n=${nth}) of mock function is with ${ - inspectArgs(expected) - }, but it was with ${inspectArgs(nthCall.args)}`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } @@ -690,16 +791,22 @@ export function toHaveReturned(context: MatcherContext): MatchResult { if (context.isNot) { if (returned.length > 0) { + const defaultMessage = + `Expected the mock function to not have returned, but it returned ${returned.length} times`; throw new AssertionError( - context.customMessage ?? - `Expected the mock function to not have returned, but it returned ${returned.length} times`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } else { if (returned.length === 0) { + const defaultMessage = + `Expected the mock function to have returned, but it did not return`; throw new AssertionError( - context.customMessage ?? - `Expected the mock function to have returned, but it did not return`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } @@ -714,16 +821,22 @@ export function toHaveReturnedTimes( if (context.isNot) { if (returned.length === expected) { + const defaultMessage = + `Expected the mock function to not have returned ${expected} times, but it returned ${returned.length} times`; throw new AssertionError( - context.customMessage ?? - `Expected the mock function to not have returned ${expected} times, but it returned ${returned.length} times`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } else { if (returned.length !== expected) { + const defaultMessage = + `Expected the mock function to have returned ${expected} times, but it returned ${returned.length} times`; throw new AssertionError( - context.customMessage ?? - `Expected the mock function to have returned ${expected} times, but it returned ${returned.length} times`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } @@ -740,20 +853,26 @@ export function toHaveReturnedWith( if (context.isNot) { if (returnedWithExpected) { + const defaultMessage = + `Expected the mock function to not have returned with ${ + inspectArg(expected) + }, but it did`; throw new AssertionError( - context.customMessage ?? - `Expected the mock function to not have returned with ${ - inspectArg(expected) - }, but it did`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } else { if (!returnedWithExpected) { + const defaultMessage = + `Expected the mock function to have returned with ${ + inspectArg(expected) + }, but it did not`; throw new AssertionError( - context.customMessage ?? - `Expected the mock function to have returned with ${ - inspectArg(expected) - }, but it did not`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } @@ -770,20 +889,26 @@ export function toHaveLastReturnedWith( if (context.isNot) { if (lastReturnedWithExpected) { + const defaultMessage = + `Expected the mock function to not have last returned with ${ + inspectArg(expected) + }, but it did`; throw new AssertionError( - context.customMessage ?? - `Expected the mock function to not have last returned with ${ - inspectArg(expected) - }, but it did`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } else { if (!lastReturnedWithExpected) { + const defaultMessage = + `Expected the mock function to have last returned with ${ + inspectArg(expected) + }, but it did not`; throw new AssertionError( - context.customMessage ?? - `Expected the mock function to have last returned with ${ - inspectArg(expected) - }, but it did not`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } @@ -807,20 +932,26 @@ export function toHaveNthReturnedWith( if (context.isNot) { if (nthReturnedWithExpected) { + const defaultMessage = + `Expected the mock function to not have n-th (n=${nth}) returned with ${ + inspectArg(expected) + }, but it did`; throw new AssertionError( - context.customMessage ?? - `Expected the mock function to not have n-th (n=${nth}) returned with ${ - inspectArg(expected) - }, but it did`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } else { if (!nthReturnedWithExpected) { + const defaultMessage = + `Expected the mock function to have n-th (n=${nth}) returned with ${ + inspectArg(expected) + }, but it did not`; throw new AssertionError( - context.customMessage ?? - `Expected the mock function to have n-th (n=${nth}) returned with ${ - inspectArg(expected) - }, but it did not`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } } @@ -864,8 +995,11 @@ export function toThrow( context.customMessage, ); isError = true; + const defaultMessage = `Expected to NOT throw ${expected}`; throw new AssertionError( - context.customMessage ?? `Expected to NOT throw ${expected}`, + context.customMessage + ? `${context.customMessage}: ${defaultMessage}` + : defaultMessage, ); } catch (e) { if (isError) { diff --git a/expect/_to_be_close_to_test.ts b/expect/_to_be_close_to_test.ts index faacc9cbe9f0..f49c31889099 100644 --- a/expect/_to_be_close_to_test.ts +++ b/expect/_to_be_close_to_test.ts @@ -31,17 +31,13 @@ Deno.test("expect().toBeCloseTo() throws error when the numDigits is smaller tha Deno.test("expect().toBeCloseTo() throws custom message", () => { const msg = "toBeCloseTo Custom Error"; - assertThrows( + expect( () => expect(0.2 + 0.11, msg).toBeCloseTo(0.3), - Error, msg, - ); + ).toThrow(new RegExp(`^${msg}`)); - assertThrows( - () => { - expect(0.2 + 0.1, msg).not.toBeCloseTo(0.3); - }, - AssertionError, + expect( + () => expect(0.2 + 0.1, msg).not.toBeCloseTo(0.3), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_be_falsy_test.ts b/expect/_to_be_falsy_test.ts index e6354f056625..5ad753e11e49 100644 --- a/expect/_to_be_falsy_test.ts +++ b/expect/_to_be_falsy_test.ts @@ -35,19 +35,13 @@ Deno.test("expect().toBeFalsy()", () => { Deno.test("expect().toBeFalsy() with custom error message message", () => { const msg = "toBeFalsy Custom Error"; - assertThrows( - () => { - expect("hello", msg).toBeFalsy(); - }, - AssertionError, + expect( + () => expect("hello", msg).toBeFalsy(), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); - assertThrows( - () => { - expect("", msg).not.toBeFalsy(); - }, - AssertionError, + expect( + () => expect("", msg).not.toBeFalsy(), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_be_greater_than_or_equal_test.ts b/expect/_to_be_greater_than_or_equal_test.ts index 62d50c2b007d..88e3613d1f6f 100644 --- a/expect/_to_be_greater_than_or_equal_test.ts +++ b/expect/_to_be_greater_than_or_equal_test.ts @@ -21,19 +21,13 @@ Deno.test("expect().toBeGreaterThanOrEqual()", () => { Deno.test("expect().toBeGreaterThanOrEqual() with custom error message", () => { const msg = "toBeGreaterThanOrEqual Custom Error"; - assertThrows( - () => { - expect(10, msg).toBeGreaterThan(10); - }, - AssertionError, + expect( + () => expect(10, msg).toBeGreaterThan(10), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); - assertThrows( - () => { - expect(11, msg).not.toBeGreaterThanOrEqual(10); - }, - AssertionError, + expect( + () => expect(11, msg).not.toBeGreaterThanOrEqual(10), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_be_greater_than_test.ts b/expect/_to_be_greater_than_test.ts index debe4092242d..96b0e8aff6a2 100644 --- a/expect/_to_be_greater_than_test.ts +++ b/expect/_to_be_greater_than_test.ts @@ -24,19 +24,13 @@ Deno.test("expect().toBeGreaterThan()", () => { Deno.test("expect().toBeGreaterThan() with custom error message message", () => { const msg = "toBeGreaterThan Custom Error"; - assertThrows( - () => { - expect(10, msg).toBeGreaterThan(10); - }, - AssertionError, + expect( + () => expect(10, msg).toBeGreaterThan(10), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); - assertThrows( - () => { - expect(11, msg).not.toBeGreaterThan(10); - }, - AssertionError, + expect( + () => expect(11, msg).not.toBeGreaterThan(10), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_be_less_than_or_equal_test.ts b/expect/_to_be_less_than_or_equal_test.ts index 50d770289cd8..7e2b0092c4e7 100644 --- a/expect/_to_be_less_than_or_equal_test.ts +++ b/expect/_to_be_less_than_or_equal_test.ts @@ -24,19 +24,13 @@ Deno.test("expect().toBeLessThanOrEqual()", () => { Deno.test("expect().toBeLessThanOrEqual() with custom error message", () => { const msg = "toBeLessThanOrEqual Custom Error"; - assertThrows( - () => { - expect(11, msg).toBeLessThanOrEqual(10); - }, - AssertionError, + expect( + () => expect(11, msg).toBeLessThanOrEqual(10), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); - assertThrows( - () => { - expect(9, msg).not.toBeLessThanOrEqual(10); - }, - AssertionError, + expect( + () => expect(9, msg).not.toBeLessThanOrEqual(10), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_be_less_than_test.ts b/expect/_to_be_less_than_test.ts index 506b00585dd5..ec6459e5edf0 100644 --- a/expect/_to_be_less_than_test.ts +++ b/expect/_to_be_less_than_test.ts @@ -24,19 +24,13 @@ Deno.test("expect().toBeLessThan()", () => { Deno.test("expect().toBeLessThan() with custom error message", () => { const msg = "toBeLessThan Custom Error"; - assertThrows( - () => { - expect(10, msg).toBeLessThan(10); - }, - AssertionError, + expect( + () => expect(10, msg).toBeLessThan(10), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); - assertThrows( - () => { - expect(9, msg).not.toBeLessThan(10); - }, - AssertionError, + expect( + () => expect(9, msg).not.toBeLessThan(10), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_be_truthy_test.ts b/expect/_to_be_truthy_test.ts index bdd22fd38e52..80b47302fe01 100644 --- a/expect/_to_be_truthy_test.ts +++ b/expect/_to_be_truthy_test.ts @@ -39,19 +39,13 @@ Deno.test("expect().toBeTruthy()", () => { Deno.test("expect().toBeTruthy() with custom error message message", () => { const msg = "toBeTruthy Custom Error"; - assertThrows( - () => { - expect(0, msg).toBeTruthy(); - }, - AssertionError, + expect( + () => expect(0, msg).toBeTruthy(), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); - assertThrows( - () => { - expect({}, msg).not.toBeTruthy(); - }, - AssertionError, + expect( + () => expect({}, msg).not.toBeTruthy(), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_contain_equal_test.ts b/expect/_to_contain_equal_test.ts index 6811530ec15b..4cd9ab2ab4a1 100644 --- a/expect/_to_contain_equal_test.ts +++ b/expect/_to_contain_equal_test.ts @@ -54,20 +54,18 @@ Deno.test("expect().toContainEqual() throws error when the value is not an array Deno.test("expect().toContainEqual() with custom error message", () => { const msg = "toContainEqual Custom Error"; - assertThrows( + expect( () => expect([{ foo: 42 }, { bar: 43 }, { baz: 44 }], msg).toContainEqual({ foo: 4, }), - AssertionError, msg, - ); - assertThrows( + ).toThrow(new RegExp(`^${msg}`)); + expect( () => expect([{ foo: 42 }, { bar: 43 }, { baz: 44 }], msg).not.toContainEqual({ foo: 42, }), - AssertionError, msg, - ); + ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_contain_test.ts b/expect/_to_contain_test.ts index fd01143f1537..c7bc485ed07e 100644 --- a/expect/_to_contain_test.ts +++ b/expect/_to_contain_test.ts @@ -39,18 +39,12 @@ 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, + expect( + () => expect(arr, msg).not.toContain(2), msg, - ); - assertThrows( - () => { - expect("foobarbaz", msg).not.toContain("bar"); - }, - AssertionError, + ).toThrow(new RegExp(`${msg}`)); + expect( + () => expect("foobarbaz", msg).not.toContain("bar"), msg, - ); + ).toThrow(new RegExp(`${msg}`)); }); diff --git a/expect/_to_equal_test.ts b/expect/_to_equal_test.ts index 8f65a08cecf6..d52f21c628b2 100644 --- a/expect/_to_equal_test.ts +++ b/expect/_to_equal_test.ts @@ -142,7 +142,7 @@ Deno.test({ () => expect(1, "CUSTOM MESSAGE").toEqual(2), AssertionError, [ - "Values are not equal: CUSTOM MESSAGE", + "CUSTOM MESSAGE: Values are not equal.", ...createHeader(), removed(`- ${yellow("1")}`), added(`+ ${yellow("2")}`), @@ -303,3 +303,9 @@ Deno.test("expect().toEqual() handles iterators", () => { iter1.foo = 1; expect(iter0).not.toEqual(iter1); }); + +Deno.test("expect.toEqual with custom message", () => { + expect( + () => expect(42, "toEqual Custom Message").toEqual(43), + ).toThrow(/^toEqual Custom Message:/); +}); diff --git a/expect/_to_have_been_called_test.ts b/expect/_to_have_been_called_test.ts index 767846a340b1..1002ea29013e 100644 --- a/expect/_to_have_been_called_test.ts +++ b/expect/_to_have_been_called_test.ts @@ -34,15 +34,13 @@ Deno.test("expect().toHaveBeenCalled() with custom error message", () => { const mockFn = fn(); mockFn(); - assertThrows( + expect( () => expect(mockFn, msg).not.toHaveBeenCalled(), - Error, msg, - ); + ).toThrow(new RegExp(`^${msg}`)); - assertThrows( + expect( () => expect(fn(), msg).toHaveBeenCalled(), - Error, msg, - ); + ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_been_called_times_test.ts b/expect/_to_have_been_called_times_test.ts index 85c5b5df70cc..1973e4ce03ba 100644 --- a/expect/_to_have_been_called_times_test.ts +++ b/expect/_to_have_been_called_times_test.ts @@ -25,19 +25,13 @@ Deno.test("expect().toHaveBeenCalledTimes() with custom error message", () => { const mockFn = fn(); mockFn(); - assertThrows( - () => { - expect(mockFn, msg).toHaveBeenCalledTimes(2); - }, - AssertionError, + expect( + () => expect(mockFn, msg).toHaveBeenCalledTimes(2), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); - assertThrows( - () => { - expect(mockFn, msg).not.toHaveBeenCalledTimes(1); - }, - AssertionError, + expect( + () => expect(mockFn, msg).not.toHaveBeenCalledTimes(1), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_been_called_with_test.ts b/expect/_to_have_been_called_with_test.ts index 3a58572f740c..270a572beb4f 100644 --- a/expect/_to_have_been_called_with_test.ts +++ b/expect/_to_have_been_called_with_test.ts @@ -26,19 +26,13 @@ Deno.test("expect().toHaveBeenCalledWith() with custom error message", () => { const mockFn = fn(); mockFn("hello", "deno"); - assertThrows( - () => { - expect(mockFn, msg).toHaveBeenCalledWith("hello", "DENO"); - }, - AssertionError, + expect( + () => expect(mockFn, msg).toHaveBeenCalledWith("hello", "DENO"), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); - assertThrows( - () => { - expect(mockFn, msg).not.toHaveBeenCalledWith("hello", "deno"); - }, - AssertionError, + expect( + () => expect(mockFn, msg).not.toHaveBeenCalledWith("hello", "deno"), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_been_last_called_with_test.ts b/expect/_to_have_been_last_called_with_test.ts index 862de7f2c8f4..9868d8268813 100644 --- a/expect/_to_have_been_last_called_with_test.ts +++ b/expect/_to_have_been_last_called_with_test.ts @@ -41,19 +41,17 @@ Deno.test("expect().toHaveBeenLastCalledWith() with custom error message", () => mockFn(1, 2, 3); mockFn(4, 5, 6); - assertThrows( + expect( () => { expect(mockFn, msg).toHaveBeenLastCalledWith(1, 2, 3); }, - AssertionError, msg, - ); + ).toThrow(new RegExp(`^${msg}`)); - assertThrows( + expect( () => { expect(mockFn, msg).not.toHaveBeenLastCalledWith(4, 5, 6); }, - AssertionError, msg, - ); + ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_been_nth_called_with_test.ts b/expect/_to_have_been_nth_called_with_test.ts index 9fa072466283..a031b2343122 100644 --- a/expect/_to_have_been_nth_called_with_test.ts +++ b/expect/_to_have_been_nth_called_with_test.ts @@ -63,18 +63,12 @@ Deno.test("expect().toHaveBeenNthCalledWith() with custom error message", () => mockFn(4, 5, 6); mockFn(7, 8, 9); - assertThrows( - () => { - expect(mockFn, msg).not.toHaveBeenNthCalledWith(1, 1, 2, 3); - }, - AssertionError, + expect( + () => expect(mockFn, msg).not.toHaveBeenNthCalledWith(1, 1, 2, 3), msg, - ); - assertThrows( - () => { - expect(mockFn, msg).toHaveBeenNthCalledWith(1, 4, 5, 6); - }, - AssertionError, + ).toThrow(new RegExp(`^${msg}`)); + expect( + () => expect(mockFn, msg).toHaveBeenNthCalledWith(1, 4, 5, 6), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_last_returned_with_test.ts b/expect/_to_have_last_returned_with_test.ts index b363b8259215..811432beb90c 100644 --- a/expect/_to_have_last_returned_with_test.ts +++ b/expect/_to_have_last_returned_with_test.ts @@ -30,19 +30,17 @@ Deno.test("expect().toHaveLastReturnedWith() with custom error message", () => { mockFn(1); mockFn(4); - assertThrows( + expect( () => { expect(mockFn, msg).toHaveLastReturnedWith(4); }, - AssertionError, msg, - ); + ).toThrow(new RegExp(`^${msg}`)); - assertThrows( + expect( () => { expect(mockFn, msg).not.toHaveLastReturnedWith(7); }, - AssertionError, msg, - ); + ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_length_test.ts b/expect/_to_have_length_test.ts index d1e1522a650d..1fb34f08014e 100644 --- a/expect/_to_have_length_test.ts +++ b/expect/_to_have_length_test.ts @@ -28,19 +28,17 @@ Deno.test("expect().toHaveLength()", () => { Deno.test("expect().toHaveLength() with custom error message", () => { const msg = "toHaveLength Custom Error"; - assertThrows( + expect( () => { expect([1, 2, 3], msg).toHaveLength(4); }, - AssertionError, msg, - ); + ).toThrow(new RegExp(`^${msg}`)); - assertThrows( + expect( () => { expect("abc", msg).not.toHaveLength(3); }, - AssertionError, msg, - ); + ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_nth_returned_with_test.ts b/expect/_to_have_nth_returned_with_test.ts index 6d80ba4c1335..2bc9c2640eec 100644 --- a/expect/_to_have_nth_returned_with_test.ts +++ b/expect/_to_have_nth_returned_with_test.ts @@ -44,19 +44,13 @@ Deno.test("expect().toHaveNthReturnedWith() with custom error message", () => { mockFn(100); mockFn(1000); - assertThrows( - () => { - expect(mockFn, msg).toHaveNthReturnedWith(1, 1); - }, - AssertionError, + expect( + () => expect(mockFn, msg).toHaveNthReturnedWith(1, 1), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); - assertThrows( - () => { - expect(mockFn, msg).not.toHaveNthReturnedWith(1, 8); - }, - AssertionError, + expect( + () => expect(mockFn, msg).not.toHaveNthReturnedWith(1, 8), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_property_test.ts b/expect/_to_have_property_test.ts index d91f0470c4f4..3a38b349d41e 100644 --- a/expect/_to_have_property_test.ts +++ b/expect/_to_have_property_test.ts @@ -30,23 +30,19 @@ Deno.test("expect().toHaveProperty() handles null and undefined", () => { Deno.test("expect().toHaveProperty() with custom error message", () => { const msg = "toHaveProperty Custom Error"; - assertThrows( - () => { + expect( + () => expect({ a: { b: { c: { d: 5 } } } }, msg).toHaveProperty("a.b.c", { d: 6, - }); - }, - AssertionError, + }), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); - assertThrows( - () => { + expect( + () => expect({ a: { b: { c: { d: 5 } } } }, msg).not.toHaveProperty("a.b.c", { d: 5, - }); - }, - AssertionError, + }), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_returned_test.ts b/expect/_to_have_returned_test.ts index 9a9fd4c8b76d..c12a69095aef 100644 --- a/expect/_to_have_returned_test.ts +++ b/expect/_to_have_returned_test.ts @@ -44,19 +44,13 @@ Deno.test("expect().toHaveReturned() with custom error message", () => { // ignore } - assertThrows( - () => { - expect(mockFn1, msg).toHaveReturned(); - }, - AssertionError, + expect( + () => expect(mockFn1, msg).toHaveReturned(), msg, - ); + ).toThrow(new RegExp(`${msg}`)); - assertThrows( - () => { - expect(mockFn0, msg).not.toHaveReturned(); - }, - AssertionError, + expect( + () => expect(mockFn0, msg).not.toHaveReturned(), msg, - ); + ).toThrow(new RegExp(`${msg}`)); }); diff --git a/expect/_to_have_returned_times_test.ts b/expect/_to_have_returned_times_test.ts index 0ceb66aecb71..7bb1f986d495 100644 --- a/expect/_to_have_returned_times_test.ts +++ b/expect/_to_have_returned_times_test.ts @@ -30,19 +30,13 @@ Deno.test("expect().toHaveReturnedTimes() with custom error message", () => { mockFn(); mockFn(); - assertThrows( - () => { - expect(mockFn, msg).toHaveReturnedTimes(1); - }, - AssertionError, + expect( + () => expect(mockFn, msg).toHaveReturnedTimes(1), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); - assertThrows( - () => { - expect(mockFn, msg).not.toHaveReturnedTimes(2); - }, - AssertionError, + expect( + () => expect(mockFn, msg).not.toHaveReturnedTimes(2), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_returned_with_test.ts b/expect/_to_have_returned_with_test.ts index c3da8f5c6872..d41fbbc916e5 100644 --- a/expect/_to_have_returned_with_test.ts +++ b/expect/_to_have_returned_with_test.ts @@ -30,19 +30,13 @@ Deno.test("expect().toHaveReturnedWith() with custom error message", () => { mockFn(5); mockFn(6); - assertThrows( - () => { - expect(mockFn, msg).toHaveReturnedWith({ foo: 5 }); - }, - AssertionError, + expect( + () => expect(mockFn, msg).toHaveReturnedWith({ foo: 5 }), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); - assertThrows( - () => { - expect(mockFn, msg).not.toHaveReturnedWith({ foo: 7 }); - }, - AssertionError, + expect( + () => expect(mockFn, msg).not.toHaveReturnedWith({ foo: 7 }), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_match_object_test.ts b/expect/_to_match_object_test.ts index 6a82353f40b2..c9eb890c4ae9 100644 --- a/expect/_to_match_object_test.ts +++ b/expect/_to_match_object_test.ts @@ -97,27 +97,18 @@ Deno.test("expect().toMatchObject() with custom error message", () => { }; const msg = "toMatchObject Custom Error"; - assertThrows( - () => { - expect([house1], msg).toMatchObject([desiredHouse]); - }, - AssertionError, + expect( + () => expect([house1], msg).toMatchObject([desiredHouse]), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); - assertThrows( - () => { - expect(house0, msg).not.toMatchObject(desiredHouse); - }, - AssertionError, + expect( + () => expect(house0, msg).not.toMatchObject(desiredHouse), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); - assertThrows( - () => { - expect(null, msg).toMatchObject([desiredHouse]); - }, - AssertionError, + expect( + () => expect(null, msg).toMatchObject([desiredHouse]), msg, - ); + ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_throw_test.ts b/expect/_to_throw_test.ts index 1c9e04a28931..bc01dde44efc 100644 --- a/expect/_to_throw_test.ts +++ b/expect/_to_throw_test.ts @@ -103,21 +103,6 @@ Deno.test("expect().toThrow()", () => { Deno.test("expect().toThrow() with custom error message", () => { const msg = "toThrow custom error message"; - assertThrows( - () => { - expect(() => {}, msg).toThrow(); - }, - AssertionError, - msg, - ); - assertThrows( - () => { - expect(() => { - throw new Error("hello world"); - }, msg).not.toThrow(); - }, - AssertionError, - msg, - ); + expect(() => {}, msg).not.toThrow(new RegExp(`^${msg}`)); }); From 35d2ddfc7c25a426c04b4b9bbd2059341eac47e5 Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Sun, 1 Dec 2024 23:13:02 +0800 Subject: [PATCH 04/11] chore: remove console --- assert/is_error.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/assert/is_error.ts b/assert/is_error.ts index 8fc599d437c9..d3bbe34a0854 100644 --- a/assert/is_error.ts +++ b/assert/is_error.ts @@ -60,7 +60,6 @@ export function assertIsError( ? msgMatches.toString() : JSON.stringify(msgMatches) }, but got ${JSON.stringify(error?.message)}.`; - console.log("msg::", msg); throw new AssertionError(msg); } } From 7df6e2d9311edfc7bcd188a35612b87cd6c85453 Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Sun, 15 Dec 2024 10:25:36 +0800 Subject: [PATCH 05/11] fix: ci --- assert/is_error_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assert/is_error_test.ts b/assert/is_error_test.ts index 1e380ae592ac..3bbff1142eec 100644 --- a/assert/is_error_test.ts +++ b/assert/is_error_test.ts @@ -76,6 +76,6 @@ Deno.test("assertIsError() throws with custom message", () => { "CUSTOM MESSAGE", ), AssertionError, - 'Expected error to be instance of "AnotherCustomError", but was "CustomError": CUSTOM MESSAGE', + 'CUSTOM MESSAGE: Expected error to be instance of "AnotherCustomError", but was "CustomError"', ); }); From 6fcc5746b4e4a5404b8d45d289fe3f33279bb960 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Tue, 17 Dec 2024 16:42:44 +0900 Subject: [PATCH 06/11] revert change in assert --- assert/is_error.ts | 10 +++++----- assert/is_error_test.ts | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/assert/is_error.ts b/assert/is_error.ts index d3bbe34a0854..8f06aac4a00c 100644 --- a/assert/is_error.ts +++ b/assert/is_error.ts @@ -33,15 +33,15 @@ export function assertIsError( msgMatches?: string | RegExp, msg?: string, ): asserts error is E { - const msgPrefix = msg ? `${msg}: ` : ""; + const msgSuffix = msg ? `: ${msg}` : "."; if (!(error instanceof Error)) { throw new AssertionError( - `${msgPrefix}Expected "error" to be an Error object.`, + `Expected "error" to be an Error object${msgSuffix}`, ); } if (ErrorClass && !(error instanceof ErrorClass)) { msg = - `${msgPrefix}Expected error to be instance of "${ErrorClass.name}", but was "${error?.constructor?.name}".`; + `Expected error to be instance of "${ErrorClass.name}", but was "${error?.constructor?.name}"${msgSuffix}`; throw new AssertionError(msg); } let msgCheck; @@ -55,11 +55,11 @@ export function assertIsError( } if (msgMatches && !msgCheck) { - msg = `${msgPrefix}Expected error message to include ${ + msg = `Expected error message to include ${ msgMatches instanceof RegExp ? msgMatches.toString() : JSON.stringify(msgMatches) - }, but got ${JSON.stringify(error?.message)}.`; + }, but got ${JSON.stringify(error?.message)}${msgSuffix}`; throw new AssertionError(msg); } } diff --git a/assert/is_error_test.ts b/assert/is_error_test.ts index 3bbff1142eec..1e380ae592ac 100644 --- a/assert/is_error_test.ts +++ b/assert/is_error_test.ts @@ -76,6 +76,6 @@ Deno.test("assertIsError() throws with custom message", () => { "CUSTOM MESSAGE", ), AssertionError, - 'CUSTOM MESSAGE: Expected error to be instance of "AnotherCustomError", but was "CustomError"', + 'Expected error to be instance of "AnotherCustomError", but was "CustomError": CUSTOM MESSAGE', ); }); From 320f091433e01b33aef07cfa5552ad3ccb47c936 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Tue, 17 Dec 2024 16:57:23 +0900 Subject: [PATCH 07/11] vendor assertIsError --- expect/_assert_is_error.ts | 65 ++++++++++++++++++++++++++++++++++++++ expect/_matchers.ts | 2 +- 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 expect/_assert_is_error.ts diff --git a/expect/_assert_is_error.ts b/expect/_assert_is_error.ts new file mode 100644 index 000000000000..4000c72d28af --- /dev/null +++ b/expect/_assert_is_error.ts @@ -0,0 +1,65 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. +// This module is browser compatible. +import { AssertionError } from "@std/assert/assertion-error"; +import { stripAnsiCode } from "@std/internal/styles"; + +/** + * Make an assertion that `error` is an `Error`. + * If not then an error will be thrown. + * An error class and a string that should be included in the + * error message can also be asserted. + * + * @example Usage + * ```ts ignore + * import { assertIsError } from "@std/assert"; + * + * assertIsError(null); // Throws + * assertIsError(new RangeError("Out of range")); // Doesn't throw + * assertIsError(new RangeError("Out of range"), SyntaxError); // Throws + * assertIsError(new RangeError("Out of range"), SyntaxError, "Out of range"); // Doesn't throw + * assertIsError(new RangeError("Out of range"), SyntaxError, "Within range"); // Throws + * ``` + * + * @typeParam E The type of the error to assert. + * @param error The error to assert. + * @param ErrorClass The optional error class to assert. + * @param msgMatches The optional string or RegExp to assert in the error message. + * @param msg The optional message to display if the assertion fails. + */ +export function assertIsError( + error: unknown, + // deno-lint-ignore no-explicit-any + ErrorClass?: abstract new (...args: any[]) => E, + msgMatches?: string | RegExp, + msg?: string, +): asserts error is E { + const msgPrefix = msg ? `${msg}: ` : ""; + if (!(error instanceof Error)) { + throw new AssertionError( + `${msgPrefix}Expected "error" to be an Error object.`, + ); + } + if (ErrorClass && !(error instanceof ErrorClass)) { + msg = + `${msgPrefix}Expected error to be instance of "${ErrorClass.name}", but was "${error?.constructor?.name}".`; + throw new AssertionError(msg); + } + let msgCheck; + if (typeof msgMatches === "string") { + msgCheck = stripAnsiCode(error.message).includes( + stripAnsiCode(msgMatches), + ); + } + if (msgMatches instanceof RegExp) { + msgCheck = msgMatches.test(stripAnsiCode(error.message)); + } + + if (msgMatches && !msgCheck) { + msg = `${msgPrefix}Expected error message to include ${ + msgMatches instanceof RegExp + ? msgMatches.toString() + : JSON.stringify(msgMatches) + }, but got ${JSON.stringify(error?.message)}.`; + throw new AssertionError(msg); + } +} diff --git a/expect/_matchers.ts b/expect/_matchers.ts index f7716ec1da3f..76c5f22cd8bb 100644 --- a/expect/_matchers.ts +++ b/expect/_matchers.ts @@ -3,7 +3,7 @@ import { assertNotStrictEquals } from "@std/assert/not-strict-equals"; import { assertStrictEquals } from "@std/assert/strict-equals"; import { assertInstanceOf } from "@std/assert/instance-of"; -import { assertIsError } from "@std/assert/is-error"; +import { assertIsError } from "./_assert_is_error.ts"; import { assertNotInstanceOf } from "@std/assert/not-instance-of"; import { assertMatch } from "@std/assert/match"; import { assertNotMatch } from "@std/assert/not-match"; From 838920652597f171c4b5f2221f8536dc9c1863ef Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Tue, 17 Dec 2024 17:00:29 +0900 Subject: [PATCH 08/11] fix toThrow test case --- expect/_to_throw_test.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/expect/_to_throw_test.ts b/expect/_to_throw_test.ts index bc01dde44efc..f817559163b9 100644 --- a/expect/_to_throw_test.ts +++ b/expect/_to_throw_test.ts @@ -104,5 +104,11 @@ Deno.test("expect().toThrow()", () => { Deno.test("expect().toThrow() with custom error message", () => { const msg = "toThrow custom error message"; - expect(() => {}, msg).not.toThrow(new RegExp(`^${msg}`)); + assertThrows( + () => { + expect(() => {}, msg).toThrow(); + }, + AssertionError, + 'toThrow custom error message: Expected "error" to be an Error object.', + ); }); From 20451949cb3945d18a6fb09f87b350056b7ca745 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Tue, 17 Dec 2024 17:11:22 +0900 Subject: [PATCH 09/11] clean up --- expect/_to_be_close_to_test.ts | 2 -- expect/_to_be_falsy_test.ts | 2 -- expect/_to_be_greater_than_or_equal_test.ts | 2 -- expect/_to_be_greater_than_test.ts | 2 -- expect/_to_be_less_than_or_equal_test.ts | 2 -- expect/_to_be_less_than_test.ts | 2 -- expect/_to_be_truthy_test.ts | 2 -- expect/_to_contain_equal_test.ts | 2 -- expect/_to_contain_test.ts | 2 -- expect/_to_have_been_called_test.ts | 2 -- expect/_to_have_been_called_times_test.ts | 2 -- expect/_to_have_been_called_with_test.ts | 2 -- expect/_to_have_been_last_called_with_test.ts | 2 -- expect/_to_have_been_nth_called_with_test.ts | 2 -- expect/_to_have_last_returned_with_test.ts | 2 -- expect/_to_have_length_test.ts | 2 -- expect/_to_have_nth_returned_with_test.ts | 2 -- expect/_to_have_property_test.ts | 2 -- expect/_to_have_returned_test.ts | 2 -- expect/_to_have_returned_times_test.ts | 2 -- expect/_to_have_returned_with_test.ts | 2 -- expect/_to_match_object_test.ts | 3 --- 22 files changed, 45 deletions(-) diff --git a/expect/_to_be_close_to_test.ts b/expect/_to_be_close_to_test.ts index f49c31889099..c7a5579baed3 100644 --- a/expect/_to_be_close_to_test.ts +++ b/expect/_to_be_close_to_test.ts @@ -33,11 +33,9 @@ Deno.test("expect().toBeCloseTo() throws custom message", () => { expect( () => expect(0.2 + 0.11, msg).toBeCloseTo(0.3), - msg, ).toThrow(new RegExp(`^${msg}`)); expect( () => expect(0.2 + 0.1, msg).not.toBeCloseTo(0.3), - msg, ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_be_falsy_test.ts b/expect/_to_be_falsy_test.ts index 5ad753e11e49..0dbfaca03028 100644 --- a/expect/_to_be_falsy_test.ts +++ b/expect/_to_be_falsy_test.ts @@ -37,11 +37,9 @@ Deno.test("expect().toBeFalsy() with custom error message message", () => { const msg = "toBeFalsy Custom Error"; expect( () => expect("hello", msg).toBeFalsy(), - msg, ).toThrow(new RegExp(`^${msg}`)); expect( () => expect("", msg).not.toBeFalsy(), - msg, ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_be_greater_than_or_equal_test.ts b/expect/_to_be_greater_than_or_equal_test.ts index 88e3613d1f6f..adec97cabf24 100644 --- a/expect/_to_be_greater_than_or_equal_test.ts +++ b/expect/_to_be_greater_than_or_equal_test.ts @@ -23,11 +23,9 @@ Deno.test("expect().toBeGreaterThanOrEqual() with custom error message", () => { expect( () => expect(10, msg).toBeGreaterThan(10), - msg, ).toThrow(new RegExp(`^${msg}`)); expect( () => expect(11, msg).not.toBeGreaterThanOrEqual(10), - msg, ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_be_greater_than_test.ts b/expect/_to_be_greater_than_test.ts index 96b0e8aff6a2..9fb94e3659af 100644 --- a/expect/_to_be_greater_than_test.ts +++ b/expect/_to_be_greater_than_test.ts @@ -26,11 +26,9 @@ Deno.test("expect().toBeGreaterThan() with custom error message message", () => expect( () => expect(10, msg).toBeGreaterThan(10), - msg, ).toThrow(new RegExp(`^${msg}`)); expect( () => expect(11, msg).not.toBeGreaterThan(10), - msg, ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_be_less_than_or_equal_test.ts b/expect/_to_be_less_than_or_equal_test.ts index 7e2b0092c4e7..cdec605937ef 100644 --- a/expect/_to_be_less_than_or_equal_test.ts +++ b/expect/_to_be_less_than_or_equal_test.ts @@ -26,11 +26,9 @@ Deno.test("expect().toBeLessThanOrEqual() with custom error message", () => { expect( () => expect(11, msg).toBeLessThanOrEqual(10), - msg, ).toThrow(new RegExp(`^${msg}`)); expect( () => expect(9, msg).not.toBeLessThanOrEqual(10), - msg, ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_be_less_than_test.ts b/expect/_to_be_less_than_test.ts index ec6459e5edf0..3be151f6bd55 100644 --- a/expect/_to_be_less_than_test.ts +++ b/expect/_to_be_less_than_test.ts @@ -26,11 +26,9 @@ Deno.test("expect().toBeLessThan() with custom error message", () => { expect( () => expect(10, msg).toBeLessThan(10), - msg, ).toThrow(new RegExp(`^${msg}`)); expect( () => expect(9, msg).not.toBeLessThan(10), - msg, ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_be_truthy_test.ts b/expect/_to_be_truthy_test.ts index 80b47302fe01..c14211fd5de5 100644 --- a/expect/_to_be_truthy_test.ts +++ b/expect/_to_be_truthy_test.ts @@ -41,11 +41,9 @@ Deno.test("expect().toBeTruthy() with custom error message message", () => { const msg = "toBeTruthy Custom Error"; expect( () => expect(0, msg).toBeTruthy(), - msg, ).toThrow(new RegExp(`^${msg}`)); expect( () => expect({}, msg).not.toBeTruthy(), - msg, ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_contain_equal_test.ts b/expect/_to_contain_equal_test.ts index 4cd9ab2ab4a1..dd9f4f084b9d 100644 --- a/expect/_to_contain_equal_test.ts +++ b/expect/_to_contain_equal_test.ts @@ -59,13 +59,11 @@ Deno.test("expect().toContainEqual() with custom error message", () => { expect([{ foo: 42 }, { bar: 43 }, { baz: 44 }], msg).toContainEqual({ foo: 4, }), - msg, ).toThrow(new RegExp(`^${msg}`)); expect( () => expect([{ foo: 42 }, { bar: 43 }, { baz: 44 }], msg).not.toContainEqual({ foo: 42, }), - msg, ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_contain_test.ts b/expect/_to_contain_test.ts index c7bc485ed07e..ef63cc9241e1 100644 --- a/expect/_to_contain_test.ts +++ b/expect/_to_contain_test.ts @@ -41,10 +41,8 @@ Deno.test("expect().toContain() with custom error message", () => { expect( () => expect(arr, msg).not.toContain(2), - msg, ).toThrow(new RegExp(`${msg}`)); expect( () => expect("foobarbaz", msg).not.toContain("bar"), - msg, ).toThrow(new RegExp(`${msg}`)); }); diff --git a/expect/_to_have_been_called_test.ts b/expect/_to_have_been_called_test.ts index 1002ea29013e..c32734952315 100644 --- a/expect/_to_have_been_called_test.ts +++ b/expect/_to_have_been_called_test.ts @@ -36,11 +36,9 @@ Deno.test("expect().toHaveBeenCalled() with custom error message", () => { expect( () => expect(mockFn, msg).not.toHaveBeenCalled(), - msg, ).toThrow(new RegExp(`^${msg}`)); expect( () => expect(fn(), msg).toHaveBeenCalled(), - msg, ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_been_called_times_test.ts b/expect/_to_have_been_called_times_test.ts index 1973e4ce03ba..094ee02b7179 100644 --- a/expect/_to_have_been_called_times_test.ts +++ b/expect/_to_have_been_called_times_test.ts @@ -27,11 +27,9 @@ Deno.test("expect().toHaveBeenCalledTimes() with custom error message", () => { expect( () => expect(mockFn, msg).toHaveBeenCalledTimes(2), - msg, ).toThrow(new RegExp(`^${msg}`)); expect( () => expect(mockFn, msg).not.toHaveBeenCalledTimes(1), - msg, ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_been_called_with_test.ts b/expect/_to_have_been_called_with_test.ts index 270a572beb4f..d1793b0ae146 100644 --- a/expect/_to_have_been_called_with_test.ts +++ b/expect/_to_have_been_called_with_test.ts @@ -28,11 +28,9 @@ Deno.test("expect().toHaveBeenCalledWith() with custom error message", () => { expect( () => expect(mockFn, msg).toHaveBeenCalledWith("hello", "DENO"), - msg, ).toThrow(new RegExp(`^${msg}`)); expect( () => expect(mockFn, msg).not.toHaveBeenCalledWith("hello", "deno"), - msg, ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_been_last_called_with_test.ts b/expect/_to_have_been_last_called_with_test.ts index 9868d8268813..848252055d99 100644 --- a/expect/_to_have_been_last_called_with_test.ts +++ b/expect/_to_have_been_last_called_with_test.ts @@ -45,13 +45,11 @@ Deno.test("expect().toHaveBeenLastCalledWith() with custom error message", () => () => { expect(mockFn, msg).toHaveBeenLastCalledWith(1, 2, 3); }, - msg, ).toThrow(new RegExp(`^${msg}`)); expect( () => { expect(mockFn, msg).not.toHaveBeenLastCalledWith(4, 5, 6); }, - msg, ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_been_nth_called_with_test.ts b/expect/_to_have_been_nth_called_with_test.ts index a031b2343122..8c868ac58fa7 100644 --- a/expect/_to_have_been_nth_called_with_test.ts +++ b/expect/_to_have_been_nth_called_with_test.ts @@ -65,10 +65,8 @@ Deno.test("expect().toHaveBeenNthCalledWith() with custom error message", () => expect( () => expect(mockFn, msg).not.toHaveBeenNthCalledWith(1, 1, 2, 3), - msg, ).toThrow(new RegExp(`^${msg}`)); expect( () => expect(mockFn, msg).toHaveBeenNthCalledWith(1, 4, 5, 6), - msg, ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_last_returned_with_test.ts b/expect/_to_have_last_returned_with_test.ts index 811432beb90c..d7aa8b766844 100644 --- a/expect/_to_have_last_returned_with_test.ts +++ b/expect/_to_have_last_returned_with_test.ts @@ -34,13 +34,11 @@ Deno.test("expect().toHaveLastReturnedWith() with custom error message", () => { () => { expect(mockFn, msg).toHaveLastReturnedWith(4); }, - msg, ).toThrow(new RegExp(`^${msg}`)); expect( () => { expect(mockFn, msg).not.toHaveLastReturnedWith(7); }, - msg, ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_length_test.ts b/expect/_to_have_length_test.ts index 1fb34f08014e..615d9b89faae 100644 --- a/expect/_to_have_length_test.ts +++ b/expect/_to_have_length_test.ts @@ -32,13 +32,11 @@ Deno.test("expect().toHaveLength() with custom error message", () => { () => { expect([1, 2, 3], msg).toHaveLength(4); }, - msg, ).toThrow(new RegExp(`^${msg}`)); expect( () => { expect("abc", msg).not.toHaveLength(3); }, - msg, ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_nth_returned_with_test.ts b/expect/_to_have_nth_returned_with_test.ts index 2bc9c2640eec..86630c9ba3bf 100644 --- a/expect/_to_have_nth_returned_with_test.ts +++ b/expect/_to_have_nth_returned_with_test.ts @@ -46,11 +46,9 @@ Deno.test("expect().toHaveNthReturnedWith() with custom error message", () => { expect( () => expect(mockFn, msg).toHaveNthReturnedWith(1, 1), - msg, ).toThrow(new RegExp(`^${msg}`)); expect( () => expect(mockFn, msg).not.toHaveNthReturnedWith(1, 8), - msg, ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_property_test.ts b/expect/_to_have_property_test.ts index 3a38b349d41e..f2ccbf0c314c 100644 --- a/expect/_to_have_property_test.ts +++ b/expect/_to_have_property_test.ts @@ -35,7 +35,6 @@ Deno.test("expect().toHaveProperty() with custom error message", () => { expect({ a: { b: { c: { d: 5 } } } }, msg).toHaveProperty("a.b.c", { d: 6, }), - msg, ).toThrow(new RegExp(`^${msg}`)); expect( @@ -43,6 +42,5 @@ Deno.test("expect().toHaveProperty() with custom error message", () => { expect({ a: { b: { c: { d: 5 } } } }, msg).not.toHaveProperty("a.b.c", { d: 5, }), - msg, ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_returned_test.ts b/expect/_to_have_returned_test.ts index c12a69095aef..aa2d998bcbef 100644 --- a/expect/_to_have_returned_test.ts +++ b/expect/_to_have_returned_test.ts @@ -46,11 +46,9 @@ Deno.test("expect().toHaveReturned() with custom error message", () => { expect( () => expect(mockFn1, msg).toHaveReturned(), - msg, ).toThrow(new RegExp(`${msg}`)); expect( () => expect(mockFn0, msg).not.toHaveReturned(), - msg, ).toThrow(new RegExp(`${msg}`)); }); diff --git a/expect/_to_have_returned_times_test.ts b/expect/_to_have_returned_times_test.ts index 7bb1f986d495..544cd5de9e18 100644 --- a/expect/_to_have_returned_times_test.ts +++ b/expect/_to_have_returned_times_test.ts @@ -32,11 +32,9 @@ Deno.test("expect().toHaveReturnedTimes() with custom error message", () => { expect( () => expect(mockFn, msg).toHaveReturnedTimes(1), - msg, ).toThrow(new RegExp(`^${msg}`)); expect( () => expect(mockFn, msg).not.toHaveReturnedTimes(2), - msg, ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_returned_with_test.ts b/expect/_to_have_returned_with_test.ts index d41fbbc916e5..95e56123626a 100644 --- a/expect/_to_have_returned_with_test.ts +++ b/expect/_to_have_returned_with_test.ts @@ -32,11 +32,9 @@ Deno.test("expect().toHaveReturnedWith() with custom error message", () => { expect( () => expect(mockFn, msg).toHaveReturnedWith({ foo: 5 }), - msg, ).toThrow(new RegExp(`^${msg}`)); expect( () => expect(mockFn, msg).not.toHaveReturnedWith({ foo: 7 }), - msg, ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_match_object_test.ts b/expect/_to_match_object_test.ts index bb38809c109d..c936e7848cc6 100644 --- a/expect/_to_match_object_test.ts +++ b/expect/_to_match_object_test.ts @@ -104,17 +104,14 @@ Deno.test("expect().toMatchObject() with custom error message", () => { expect( () => expect([house1], msg).toMatchObject([desiredHouse]), - msg, ).toThrow(new RegExp(`^${msg}`)); expect( () => expect(house0, msg).not.toMatchObject(desiredHouse), - msg, ).toThrow(new RegExp(`^${msg}`)); expect( () => expect(null, msg).toMatchObject([desiredHouse]), - msg, ).toThrow(new RegExp(`^${msg}`)); }); From 2ff42268a8c3c3a1e300ee3f1ffec32fd2dcd93b Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Tue, 17 Dec 2024 17:19:31 +0900 Subject: [PATCH 10/11] change formatting --- expect/_to_be_close_to_test.ts | 12 ++++++------ expect/_to_be_falsy_test.ts | 9 ++------- expect/_to_be_greater_than_or_equal_test.ts | 12 ++++++------ expect/_to_be_greater_than_test.ts | 12 ++++++------ expect/_to_be_less_than_or_equal_test.ts | 12 ++++++------ expect/_to_be_less_than_test.ts | 11 ++++------- expect/_to_be_truthy_test.ts | 9 ++------- expect/_to_contain_equal_test.ts | 18 ++++++++---------- expect/_to_contain_test.ts | 10 ++++------ expect/_to_equal_test.ts | 6 +++--- expect/_to_have_been_called_test.ts | 12 ++++++------ expect/_to_have_been_called_times_test.ts | 12 ++++++------ expect/_to_have_been_called_with_test.ts | 10 ++++------ expect/_to_have_been_last_called_with_test.ts | 18 +++++++----------- expect/_to_have_been_nth_called_with_test.ts | 11 +++++------ expect/_to_have_last_returned_with_test.ts | 18 +++++++----------- expect/_to_have_length_test.ts | 18 +++++++----------- expect/_to_have_nth_returned_with_test.ts | 12 ++++++------ expect/_to_have_property_test.ts | 16 ++++++---------- expect/_to_have_returned_test.ts | 12 ++++++------ expect/_to_have_returned_times_test.ts | 12 ++++++------ expect/_to_have_returned_with_test.ts | 12 ++++++------ expect/_to_match_object_test.ts | 18 +++++++++--------- expect/_to_throw_test.ts | 4 +--- 24 files changed, 129 insertions(+), 167 deletions(-) diff --git a/expect/_to_be_close_to_test.ts b/expect/_to_be_close_to_test.ts index c7a5579baed3..587ee72bea8c 100644 --- a/expect/_to_be_close_to_test.ts +++ b/expect/_to_be_close_to_test.ts @@ -31,11 +31,11 @@ Deno.test("expect().toBeCloseTo() throws error when the numDigits is smaller tha Deno.test("expect().toBeCloseTo() throws custom message", () => { const msg = "toBeCloseTo Custom Error"; - expect( - () => expect(0.2 + 0.11, msg).toBeCloseTo(0.3), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(0.2 + 0.11, msg).toBeCloseTo(0.3)).toThrow( + new RegExp(`^${msg}`), + ); - expect( - () => expect(0.2 + 0.1, msg).not.toBeCloseTo(0.3), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(0.2 + 0.1, msg).not.toBeCloseTo(0.3)).toThrow( + new RegExp(`^${msg}`), + ); }); diff --git a/expect/_to_be_falsy_test.ts b/expect/_to_be_falsy_test.ts index 0dbfaca03028..dc3a0ae78e40 100644 --- a/expect/_to_be_falsy_test.ts +++ b/expect/_to_be_falsy_test.ts @@ -35,11 +35,6 @@ Deno.test("expect().toBeFalsy()", () => { Deno.test("expect().toBeFalsy() with custom error message message", () => { const msg = "toBeFalsy Custom Error"; - expect( - () => expect("hello", msg).toBeFalsy(), - ).toThrow(new RegExp(`^${msg}`)); - - expect( - () => expect("", msg).not.toBeFalsy(), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect("hello", msg).toBeFalsy()).toThrow(new RegExp(`^${msg}`)); + expect(() => expect("", msg).not.toBeFalsy()).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_be_greater_than_or_equal_test.ts b/expect/_to_be_greater_than_or_equal_test.ts index adec97cabf24..f5c8242c0099 100644 --- a/expect/_to_be_greater_than_or_equal_test.ts +++ b/expect/_to_be_greater_than_or_equal_test.ts @@ -21,11 +21,11 @@ Deno.test("expect().toBeGreaterThanOrEqual()", () => { Deno.test("expect().toBeGreaterThanOrEqual() with custom error message", () => { const msg = "toBeGreaterThanOrEqual Custom Error"; - expect( - () => expect(10, msg).toBeGreaterThan(10), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(10, msg).toBeGreaterThan(10)).toThrow( + new RegExp(`^${msg}`), + ); - expect( - () => expect(11, msg).not.toBeGreaterThanOrEqual(10), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(11, msg).not.toBeGreaterThanOrEqual(10)).toThrow( + new RegExp(`^${msg}`), + ); }); diff --git a/expect/_to_be_greater_than_test.ts b/expect/_to_be_greater_than_test.ts index 9fb94e3659af..5ec1ba83ff1b 100644 --- a/expect/_to_be_greater_than_test.ts +++ b/expect/_to_be_greater_than_test.ts @@ -24,11 +24,11 @@ Deno.test("expect().toBeGreaterThan()", () => { Deno.test("expect().toBeGreaterThan() with custom error message message", () => { const msg = "toBeGreaterThan Custom Error"; - expect( - () => expect(10, msg).toBeGreaterThan(10), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(10, msg).toBeGreaterThan(10)).toThrow( + new RegExp(`^${msg}`), + ); - expect( - () => expect(11, msg).not.toBeGreaterThan(10), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(11, msg).not.toBeGreaterThan(10)).toThrow( + new RegExp(`^${msg}`), + ); }); diff --git a/expect/_to_be_less_than_or_equal_test.ts b/expect/_to_be_less_than_or_equal_test.ts index cdec605937ef..72c7162575e3 100644 --- a/expect/_to_be_less_than_or_equal_test.ts +++ b/expect/_to_be_less_than_or_equal_test.ts @@ -24,11 +24,11 @@ Deno.test("expect().toBeLessThanOrEqual()", () => { Deno.test("expect().toBeLessThanOrEqual() with custom error message", () => { const msg = "toBeLessThanOrEqual Custom Error"; - expect( - () => expect(11, msg).toBeLessThanOrEqual(10), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(11, msg).toBeLessThanOrEqual(10)).toThrow( + new RegExp(`^${msg}`), + ); - expect( - () => expect(9, msg).not.toBeLessThanOrEqual(10), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(9, msg).not.toBeLessThanOrEqual(10)).toThrow( + new RegExp(`^${msg}`), + ); }); diff --git a/expect/_to_be_less_than_test.ts b/expect/_to_be_less_than_test.ts index 3be151f6bd55..0916a6170b18 100644 --- a/expect/_to_be_less_than_test.ts +++ b/expect/_to_be_less_than_test.ts @@ -24,11 +24,8 @@ Deno.test("expect().toBeLessThan()", () => { Deno.test("expect().toBeLessThan() with custom error message", () => { const msg = "toBeLessThan Custom Error"; - expect( - () => expect(10, msg).toBeLessThan(10), - ).toThrow(new RegExp(`^${msg}`)); - - expect( - () => expect(9, msg).not.toBeLessThan(10), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(10, msg).toBeLessThan(10)).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(9, msg).not.toBeLessThan(10)).toThrow( + new RegExp(`^${msg}`), + ); }); diff --git a/expect/_to_be_truthy_test.ts b/expect/_to_be_truthy_test.ts index c14211fd5de5..76f5d5bd92dd 100644 --- a/expect/_to_be_truthy_test.ts +++ b/expect/_to_be_truthy_test.ts @@ -39,11 +39,6 @@ Deno.test("expect().toBeTruthy()", () => { Deno.test("expect().toBeTruthy() with custom error message message", () => { const msg = "toBeTruthy Custom Error"; - expect( - () => expect(0, msg).toBeTruthy(), - ).toThrow(new RegExp(`^${msg}`)); - - expect( - () => expect({}, msg).not.toBeTruthy(), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(0, msg).toBeTruthy()).toThrow(new RegExp(`^${msg}`)); + expect(() => expect({}, msg).not.toBeTruthy()).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_contain_equal_test.ts b/expect/_to_contain_equal_test.ts index dd9f4f084b9d..2686a1077d60 100644 --- a/expect/_to_contain_equal_test.ts +++ b/expect/_to_contain_equal_test.ts @@ -54,16 +54,14 @@ Deno.test("expect().toContainEqual() throws error when the value is not an array Deno.test("expect().toContainEqual() with custom error message", () => { const msg = "toContainEqual Custom Error"; - expect( - () => - expect([{ foo: 42 }, { bar: 43 }, { baz: 44 }], msg).toContainEqual({ - foo: 4, - }), + expect(() => + expect([{ foo: 42 }, { bar: 43 }, { baz: 44 }], msg).toContainEqual({ + foo: 4, + }) ).toThrow(new RegExp(`^${msg}`)); - expect( - () => - expect([{ foo: 42 }, { bar: 43 }, { baz: 44 }], msg).not.toContainEqual({ - foo: 42, - }), + expect(() => + expect([{ foo: 42 }, { bar: 43 }, { baz: 44 }], msg).not.toContainEqual({ + foo: 42, + }) ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_contain_test.ts b/expect/_to_contain_test.ts index ef63cc9241e1..f65f937091d9 100644 --- a/expect/_to_contain_test.ts +++ b/expect/_to_contain_test.ts @@ -39,10 +39,8 @@ Deno.test("expect().toContain() with custom error message", () => { const arr = [1, 2, 3]; const msg = "toContain Custom Error"; - expect( - () => expect(arr, msg).not.toContain(2), - ).toThrow(new RegExp(`${msg}`)); - expect( - () => expect("foobarbaz", msg).not.toContain("bar"), - ).toThrow(new RegExp(`${msg}`)); + expect(() => expect(arr, msg).not.toContain(2)).toThrow(new RegExp(`${msg}`)); + expect(() => expect("foobarbaz", msg).not.toContain("bar")).toThrow( + new RegExp(`${msg}`), + ); }); diff --git a/expect/_to_equal_test.ts b/expect/_to_equal_test.ts index d52f21c628b2..f1f502450ab3 100644 --- a/expect/_to_equal_test.ts +++ b/expect/_to_equal_test.ts @@ -305,7 +305,7 @@ Deno.test("expect().toEqual() handles iterators", () => { }); Deno.test("expect.toEqual with custom message", () => { - expect( - () => expect(42, "toEqual Custom Message").toEqual(43), - ).toThrow(/^toEqual Custom Message:/); + expect(() => expect(42, "toEqual Custom Message").toEqual(43)).toThrow( + /^toEqual Custom Message:/, + ); }); diff --git a/expect/_to_have_been_called_test.ts b/expect/_to_have_been_called_test.ts index c32734952315..21720d2011ab 100644 --- a/expect/_to_have_been_called_test.ts +++ b/expect/_to_have_been_called_test.ts @@ -34,11 +34,11 @@ Deno.test("expect().toHaveBeenCalled() with custom error message", () => { const mockFn = fn(); mockFn(); - expect( - () => expect(mockFn, msg).not.toHaveBeenCalled(), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(mockFn, msg).not.toHaveBeenCalled()).toThrow( + new RegExp(`^${msg}`), + ); - expect( - () => expect(fn(), msg).toHaveBeenCalled(), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(fn(), msg).toHaveBeenCalled()).toThrow( + new RegExp(`^${msg}`), + ); }); diff --git a/expect/_to_have_been_called_times_test.ts b/expect/_to_have_been_called_times_test.ts index 094ee02b7179..fb797547ca94 100644 --- a/expect/_to_have_been_called_times_test.ts +++ b/expect/_to_have_been_called_times_test.ts @@ -25,11 +25,11 @@ Deno.test("expect().toHaveBeenCalledTimes() with custom error message", () => { const mockFn = fn(); mockFn(); - expect( - () => expect(mockFn, msg).toHaveBeenCalledTimes(2), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(mockFn, msg).toHaveBeenCalledTimes(2)).toThrow( + new RegExp(`^${msg}`), + ); - expect( - () => expect(mockFn, msg).not.toHaveBeenCalledTimes(1), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(mockFn, msg).not.toHaveBeenCalledTimes(1)).toThrow( + new RegExp(`^${msg}`), + ); }); diff --git a/expect/_to_have_been_called_with_test.ts b/expect/_to_have_been_called_with_test.ts index d1793b0ae146..be37dc3c7bbe 100644 --- a/expect/_to_have_been_called_with_test.ts +++ b/expect/_to_have_been_called_with_test.ts @@ -26,11 +26,9 @@ Deno.test("expect().toHaveBeenCalledWith() with custom error message", () => { const mockFn = fn(); mockFn("hello", "deno"); - expect( - () => expect(mockFn, msg).toHaveBeenCalledWith("hello", "DENO"), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(mockFn, msg).toHaveBeenCalledWith("hello", "DENO")) + .toThrow(new RegExp(`^${msg}`)); - expect( - () => expect(mockFn, msg).not.toHaveBeenCalledWith("hello", "deno"), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(mockFn, msg).not.toHaveBeenCalledWith("hello", "deno")) + .toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_been_last_called_with_test.ts b/expect/_to_have_been_last_called_with_test.ts index 848252055d99..46e5586c6ce7 100644 --- a/expect/_to_have_been_last_called_with_test.ts +++ b/expect/_to_have_been_last_called_with_test.ts @@ -41,15 +41,11 @@ Deno.test("expect().toHaveBeenLastCalledWith() with custom error message", () => mockFn(1, 2, 3); mockFn(4, 5, 6); - expect( - () => { - expect(mockFn, msg).toHaveBeenLastCalledWith(1, 2, 3); - }, - ).toThrow(new RegExp(`^${msg}`)); - - expect( - () => { - expect(mockFn, msg).not.toHaveBeenLastCalledWith(4, 5, 6); - }, - ).toThrow(new RegExp(`^${msg}`)); + expect(() => { + expect(mockFn, msg).toHaveBeenLastCalledWith(1, 2, 3); + }).toThrow(new RegExp(`^${msg}`)); + + expect(() => { + expect(mockFn, msg).not.toHaveBeenLastCalledWith(4, 5, 6); + }).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_been_nth_called_with_test.ts b/expect/_to_have_been_nth_called_with_test.ts index 8c868ac58fa7..bef370828867 100644 --- a/expect/_to_have_been_nth_called_with_test.ts +++ b/expect/_to_have_been_nth_called_with_test.ts @@ -63,10 +63,9 @@ Deno.test("expect().toHaveBeenNthCalledWith() with custom error message", () => mockFn(4, 5, 6); mockFn(7, 8, 9); - expect( - () => expect(mockFn, msg).not.toHaveBeenNthCalledWith(1, 1, 2, 3), - ).toThrow(new RegExp(`^${msg}`)); - expect( - () => expect(mockFn, msg).toHaveBeenNthCalledWith(1, 4, 5, 6), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(mockFn, msg).not.toHaveBeenNthCalledWith(1, 1, 2, 3)) + .toThrow(new RegExp(`^${msg}`)); + expect(() => expect(mockFn, msg).toHaveBeenNthCalledWith(1, 4, 5, 6)).toThrow( + new RegExp(`^${msg}`), + ); }); diff --git a/expect/_to_have_last_returned_with_test.ts b/expect/_to_have_last_returned_with_test.ts index d7aa8b766844..37448c997166 100644 --- a/expect/_to_have_last_returned_with_test.ts +++ b/expect/_to_have_last_returned_with_test.ts @@ -30,15 +30,11 @@ Deno.test("expect().toHaveLastReturnedWith() with custom error message", () => { mockFn(1); mockFn(4); - expect( - () => { - expect(mockFn, msg).toHaveLastReturnedWith(4); - }, - ).toThrow(new RegExp(`^${msg}`)); - - expect( - () => { - expect(mockFn, msg).not.toHaveLastReturnedWith(7); - }, - ).toThrow(new RegExp(`^${msg}`)); + expect(() => { + expect(mockFn, msg).toHaveLastReturnedWith(4); + }).toThrow(new RegExp(`^${msg}`)); + + expect(() => { + expect(mockFn, msg).not.toHaveLastReturnedWith(7); + }).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_length_test.ts b/expect/_to_have_length_test.ts index 615d9b89faae..eeb8279b9108 100644 --- a/expect/_to_have_length_test.ts +++ b/expect/_to_have_length_test.ts @@ -28,15 +28,11 @@ Deno.test("expect().toHaveLength()", () => { Deno.test("expect().toHaveLength() with custom error message", () => { const msg = "toHaveLength Custom Error"; - expect( - () => { - expect([1, 2, 3], msg).toHaveLength(4); - }, - ).toThrow(new RegExp(`^${msg}`)); - - expect( - () => { - expect("abc", msg).not.toHaveLength(3); - }, - ).toThrow(new RegExp(`^${msg}`)); + expect(() => { + expect([1, 2, 3], msg).toHaveLength(4); + }).toThrow(new RegExp(`^${msg}`)); + + expect(() => { + expect("abc", msg).not.toHaveLength(3); + }).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_nth_returned_with_test.ts b/expect/_to_have_nth_returned_with_test.ts index 86630c9ba3bf..d3e39539501a 100644 --- a/expect/_to_have_nth_returned_with_test.ts +++ b/expect/_to_have_nth_returned_with_test.ts @@ -44,11 +44,11 @@ Deno.test("expect().toHaveNthReturnedWith() with custom error message", () => { mockFn(100); mockFn(1000); - expect( - () => expect(mockFn, msg).toHaveNthReturnedWith(1, 1), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(mockFn, msg).toHaveNthReturnedWith(1, 1)).toThrow( + new RegExp(`^${msg}`), + ); - expect( - () => expect(mockFn, msg).not.toHaveNthReturnedWith(1, 8), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(mockFn, msg).not.toHaveNthReturnedWith(1, 8)).toThrow( + new RegExp(`^${msg}`), + ); }); diff --git a/expect/_to_have_property_test.ts b/expect/_to_have_property_test.ts index f2ccbf0c314c..3bef52521c9c 100644 --- a/expect/_to_have_property_test.ts +++ b/expect/_to_have_property_test.ts @@ -30,17 +30,13 @@ Deno.test("expect().toHaveProperty() handles null and undefined", () => { Deno.test("expect().toHaveProperty() with custom error message", () => { const msg = "toHaveProperty Custom Error"; - expect( - () => - expect({ a: { b: { c: { d: 5 } } } }, msg).toHaveProperty("a.b.c", { - d: 6, - }), + expect(() => + expect({ a: { b: { c: { d: 5 } } } }, msg).toHaveProperty("a.b.c", { d: 6 }) ).toThrow(new RegExp(`^${msg}`)); - expect( - () => - expect({ a: { b: { c: { d: 5 } } } }, msg).not.toHaveProperty("a.b.c", { - d: 5, - }), + expect(() => + expect({ a: { b: { c: { d: 5 } } } }, msg).not.toHaveProperty("a.b.c", { + d: 5, + }) ).toThrow(new RegExp(`^${msg}`)); }); diff --git a/expect/_to_have_returned_test.ts b/expect/_to_have_returned_test.ts index aa2d998bcbef..7dd311bba547 100644 --- a/expect/_to_have_returned_test.ts +++ b/expect/_to_have_returned_test.ts @@ -44,11 +44,11 @@ Deno.test("expect().toHaveReturned() with custom error message", () => { // ignore } - expect( - () => expect(mockFn1, msg).toHaveReturned(), - ).toThrow(new RegExp(`${msg}`)); + expect(() => expect(mockFn1, msg).toHaveReturned()).toThrow( + new RegExp(`${msg}`), + ); - expect( - () => expect(mockFn0, msg).not.toHaveReturned(), - ).toThrow(new RegExp(`${msg}`)); + expect(() => expect(mockFn0, msg).not.toHaveReturned()).toThrow( + new RegExp(`${msg}`), + ); }); diff --git a/expect/_to_have_returned_times_test.ts b/expect/_to_have_returned_times_test.ts index 544cd5de9e18..ecf40443912d 100644 --- a/expect/_to_have_returned_times_test.ts +++ b/expect/_to_have_returned_times_test.ts @@ -30,11 +30,11 @@ Deno.test("expect().toHaveReturnedTimes() with custom error message", () => { mockFn(); mockFn(); - expect( - () => expect(mockFn, msg).toHaveReturnedTimes(1), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(mockFn, msg).toHaveReturnedTimes(1)).toThrow( + new RegExp(`^${msg}`), + ); - expect( - () => expect(mockFn, msg).not.toHaveReturnedTimes(2), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(mockFn, msg).not.toHaveReturnedTimes(2)).toThrow( + new RegExp(`^${msg}`), + ); }); diff --git a/expect/_to_have_returned_with_test.ts b/expect/_to_have_returned_with_test.ts index 95e56123626a..2719fd8da9f6 100644 --- a/expect/_to_have_returned_with_test.ts +++ b/expect/_to_have_returned_with_test.ts @@ -30,11 +30,11 @@ Deno.test("expect().toHaveReturnedWith() with custom error message", () => { mockFn(5); mockFn(6); - expect( - () => expect(mockFn, msg).toHaveReturnedWith({ foo: 5 }), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(mockFn, msg).toHaveReturnedWith({ foo: 5 })).toThrow( + new RegExp(`^${msg}`), + ); - expect( - () => expect(mockFn, msg).not.toHaveReturnedWith({ foo: 7 }), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(mockFn, msg).not.toHaveReturnedWith({ foo: 7 })).toThrow( + new RegExp(`^${msg}`), + ); }); diff --git a/expect/_to_match_object_test.ts b/expect/_to_match_object_test.ts index c936e7848cc6..b16c912fbcd8 100644 --- a/expect/_to_match_object_test.ts +++ b/expect/_to_match_object_test.ts @@ -102,17 +102,17 @@ Deno.test("expect().toMatchObject() with custom error message", () => { }; const msg = "toMatchObject Custom Error"; - expect( - () => expect([house1], msg).toMatchObject([desiredHouse]), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect([house1], msg).toMatchObject([desiredHouse])).toThrow( + new RegExp(`^${msg}`), + ); - expect( - () => expect(house0, msg).not.toMatchObject(desiredHouse), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(house0, msg).not.toMatchObject(desiredHouse)).toThrow( + new RegExp(`^${msg}`), + ); - expect( - () => expect(null, msg).toMatchObject([desiredHouse]), - ).toThrow(new RegExp(`^${msg}`)); + expect(() => expect(null, msg).toMatchObject([desiredHouse])).toThrow( + new RegExp(`^${msg}`), + ); }); Deno.test("expect().toMatchObject() throws the correct error messages", () => { diff --git a/expect/_to_throw_test.ts b/expect/_to_throw_test.ts index f817559163b9..00bd8a4f1566 100644 --- a/expect/_to_throw_test.ts +++ b/expect/_to_throw_test.ts @@ -105,9 +105,7 @@ Deno.test("expect().toThrow() with custom error message", () => { const msg = "toThrow custom error message"; assertThrows( - () => { - expect(() => {}, msg).toThrow(); - }, + () => expect(() => {}, msg).toThrow(), AssertionError, 'toThrow custom error message: Expected "error" to be an Error object.', ); From 1e6c9d875170cb774b9eb77d4b208ea7ef3c1dfd Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Tue, 17 Dec 2024 17:22:58 +0900 Subject: [PATCH 11/11] remove misleading example --- expect/_assert_is_error.ts | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/expect/_assert_is_error.ts b/expect/_assert_is_error.ts index 4000c72d28af..b343d6a48257 100644 --- a/expect/_assert_is_error.ts +++ b/expect/_assert_is_error.ts @@ -9,17 +9,6 @@ import { stripAnsiCode } from "@std/internal/styles"; * An error class and a string that should be included in the * error message can also be asserted. * - * @example Usage - * ```ts ignore - * import { assertIsError } from "@std/assert"; - * - * assertIsError(null); // Throws - * assertIsError(new RangeError("Out of range")); // Doesn't throw - * assertIsError(new RangeError("Out of range"), SyntaxError); // Throws - * assertIsError(new RangeError("Out of range"), SyntaxError, "Out of range"); // Doesn't throw - * assertIsError(new RangeError("Out of range"), SyntaxError, "Within range"); // Throws - * ``` - * * @typeParam E The type of the error to assert. * @param error The error to assert. * @param ErrorClass The optional error class to assert.