Skip to content

Commit

Permalink
fix: diff for toBeNaN/toBeUndefined/toBeNull
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Oct 12, 2024
1 parent 69ba4a5 commit 9b2352c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
27 changes: 24 additions & 3 deletions packages/expect/src/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Check failure on line 384 in packages/expect/src/jest-expect.ts

View workflow job for this annotation

GitHub Actions / Lint: node-latest, ubuntu-latest

Prefer `Number.NaN` over `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,

Check failure on line 401 in packages/expect/src/jest-expect.ts

View workflow job for this annotation

GitHub Actions / Lint: node-latest, ubuntu-latest

Expected literal to be on the right side of ===
'expected #{this} to be null',
'expected #{this} not to be null',
null,
obj,
)
})
def('toBeDefined', function () {
const obj = utils.flag(this, 'object')
Expand Down
57 changes: 54 additions & 3 deletions test/core/test/__snapshots__/jest-expect.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
`;
Expand Down
5 changes: 4 additions & 1 deletion test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})

0 comments on commit 9b2352c

Please sign in to comment.