From 69ba4a574198f996e30b2a8b28fed3b94b61d264 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa <hi.ogawa.zz@gmail.com> Date: Sat, 12 Oct 2024 16:58:26 +0900 Subject: [PATCH 1/3] fix(expect): fix `toBeTruthy` and `toBeFalsy` diff --- packages/expect/src/jest-expect.ts | 12 ++++---- .../__snapshots__/jest-expect.test.ts.snap | 30 +++++++++++++++++++ test/core/test/jest-expect.test.ts | 5 ++++ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/packages/expect/src/jest-expect.ts b/packages/expect/src/jest-expect.ts index a038ceada951..f3de46d13cab 100644 --- a/packages/expect/src/jest-expect.ts +++ b/packages/expect/src/jest-expect.ts @@ -309,8 +309,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { Boolean(obj), 'expected #{this} to be truthy', 'expected #{this} to not be truthy', + true, obj, - false, ) }) def('toBeFalsy', function () { @@ -319,8 +319,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { !obj, 'expected #{this} to be falsy', 'expected #{this} to not be falsy', - obj, false, + obj, ) }) def('toBeGreaterThan', function (expected: number | bigint) { @@ -331,8 +331,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { actual > expected, `expected ${actual} to be greater than ${expected}`, `expected ${actual} to be not greater than ${expected}`, - actual, expected, + actual, false, ) }) @@ -344,8 +344,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { actual >= expected, `expected ${actual} to be greater than or equal to ${expected}`, `expected ${actual} to be not greater than or equal to ${expected}`, - actual, expected, + actual, false, ) }) @@ -357,8 +357,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { actual < expected, `expected ${actual} to be less than ${expected}`, `expected ${actual} to be not less than ${expected}`, - actual, expected, + actual, false, ) }) @@ -370,8 +370,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { actual <= expected, `expected ${actual} to be less than or equal to ${expected}`, `expected ${actual} to be not less than or equal to ${expected}`, - actual, expected, + actual, false, ) }) diff --git a/test/core/test/__snapshots__/jest-expect.test.ts.snap b/test/core/test/__snapshots__/jest-expect.test.ts.snap index 85b30fbf13c6..035c52bb4b03 100644 --- a/test/core/test/__snapshots__/jest-expect.test.ts.snap +++ b/test/core/test/__snapshots__/jest-expect.test.ts.snap @@ -311,6 +311,36 @@ exports[`asymmetric matcher error 23`] = ` } `; +exports[`diff 1`] = ` +{ + "actual": "undefined", + "diff": "- Expected: +true + ++ Received: +undefined", + "expected": "true", + "message": "expected undefined to be truthy", +} +`; + +exports[`diff 2`] = ` +{ + "actual": "Object { + "k": "v", +}", + "diff": "- Expected: +false + ++ Received: +Object { + "k": "v", +}", + "expected": "false", + "message": "expected { k: 'v' } to be falsy", +} +`; + exports[`toHaveBeenNthCalledWith error 1`] = ` { "actual": "Array [ diff --git a/test/core/test/jest-expect.test.ts b/test/core/test/jest-expect.test.ts index b97d1a7c6297..52bba2e0f50f 100644 --- a/test/core/test/jest-expect.test.ts +++ b/test/core/test/jest-expect.test.ts @@ -1354,3 +1354,8 @@ it('toMatch/toContain diff', () => { }) it('timeout', () => new Promise(resolve => setTimeout(resolve, 500))) + +it('diff', () => { + snapshotError(() => expect(undefined).toBeTruthy()) + snapshotError(() => expect({ k: 'v' }).toBeFalsy()) +}) From 9b2352c2582c5acdc5c4f04f75f4984730e334dd Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa <hi.ogawa.zz@gmail.com> Date: Sat, 12 Oct 2024 17:19:57 +0900 Subject: [PATCH 2/3] fix: diff for `toBeNaN/toBeUndefined/toBeNull` --- packages/expect/src/jest-expect.ts | 27 ++++++++- .../__snapshots__/jest-expect.test.ts.snap | 57 ++++++++++++++++++- test/core/test/jest-expect.test.ts | 5 +- 3 files changed, 82 insertions(+), 7 deletions(-) diff --git a/packages/expect/src/jest-expect.ts b/packages/expect/src/jest-expect.ts index f3de46d13cab..d880c52501e9 100644 --- a/packages/expect/src/jest-expect.ts +++ b/packages/expect/src/jest-expect.ts @@ -376,13 +376,34 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { ) }) def('toBeNaN', function () { - return this.be.NaN + const obj = utils.flag(this, 'object') + this.assert( + Number.isNaN(obj), + 'expected #{this} to be NaN', + 'expected #{this} not to be NaN', + NaN, + obj, + ) }) def('toBeUndefined', function () { - return this.be.undefined + const obj = utils.flag(this, 'object') + this.assert( + undefined === obj, + 'expected #{this} to be undefined', + 'expected #{this} not to be undefined', + undefined, + obj, + ) }) def('toBeNull', function () { - return this.be.null + const obj = utils.flag(this, 'object') + this.assert( + null === obj, + 'expected #{this} to be null', + 'expected #{this} not to be null', + null, + obj, + ) }) def('toBeDefined', function () { const obj = utils.flag(this, 'object') diff --git a/test/core/test/__snapshots__/jest-expect.test.ts.snap b/test/core/test/__snapshots__/jest-expect.test.ts.snap index 035c52bb4b03..ced7d75d7410 100644 --- a/test/core/test/__snapshots__/jest-expect.test.ts.snap +++ b/test/core/test/__snapshots__/jest-expect.test.ts.snap @@ -327,17 +327,68 @@ undefined", exports[`diff 2`] = ` { "actual": "Object { - "k": "v", + "hello": "world", }", "diff": "- Expected: false + Received: Object { - "k": "v", + "hello": "world", }", "expected": "false", - "message": "expected { k: 'v' } to be falsy", + "message": "expected { hello: 'world' } to be falsy", +} +`; + +exports[`diff 3`] = ` +{ + "actual": "Object { + "hello": "world", +}", + "diff": "- Expected: +NaN + ++ Received: +Object { + "hello": "world", +}", + "expected": "NaN", + "message": "expected { hello: 'world' } to be NaN", +} +`; + +exports[`diff 4`] = ` +{ + "actual": "Object { + "hello": "world", +}", + "diff": "- Expected: +undefined + ++ Received: +Object { + "hello": "world", +}", + "expected": "undefined", + "message": "expected { hello: 'world' } to be undefined", +} +`; + +exports[`diff 5`] = ` +{ + "actual": "Object { + "hello": "world", +}", + "diff": "- Expected: +null + ++ Received: +Object { + "hello": "world", +}", + "expected": "null", + "message": "expected { hello: 'world' } to be null", } `; diff --git a/test/core/test/jest-expect.test.ts b/test/core/test/jest-expect.test.ts index 52bba2e0f50f..620e0240bc02 100644 --- a/test/core/test/jest-expect.test.ts +++ b/test/core/test/jest-expect.test.ts @@ -1357,5 +1357,8 @@ it('timeout', () => new Promise(resolve => setTimeout(resolve, 500))) it('diff', () => { snapshotError(() => expect(undefined).toBeTruthy()) - snapshotError(() => expect({ k: 'v' }).toBeFalsy()) + snapshotError(() => expect({ hello: 'world' }).toBeFalsy()) + snapshotError(() => expect({ hello: 'world' }).toBeNaN()) + snapshotError(() => expect({ hello: 'world' }).toBeUndefined()) + snapshotError(() => expect({ hello: 'world' }).toBeNull()) }) From eebd9e401ead2eedce6f27c4c70b2e7f7130d4ba Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa <hi.ogawa.zz@gmail.com> Date: Sat, 12 Oct 2024 17:44:42 +0900 Subject: [PATCH 3/3] chore: lint --- packages/expect/src/jest-expect.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/expect/src/jest-expect.ts b/packages/expect/src/jest-expect.ts index d880c52501e9..3d8a232c06ef 100644 --- a/packages/expect/src/jest-expect.ts +++ b/packages/expect/src/jest-expect.ts @@ -381,7 +381,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { Number.isNaN(obj), 'expected #{this} to be NaN', 'expected #{this} not to be NaN', - NaN, + Number.NaN, obj, ) }) @@ -398,7 +398,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { def('toBeNull', function () { const obj = utils.flag(this, 'object') this.assert( - null === obj, + obj === null, 'expected #{this} to be null', 'expected #{this} not to be null', null,