Skip to content

Commit

Permalink
fix: fix error diff of `toBeNaN, toBeUndefined, toBeNull, toBeTruthy,…
Browse files Browse the repository at this point in the history
… toBeFalsy` (#6697)
  • Loading branch information
hi-ogawa authored Oct 12, 2024
1 parent 5e6de27 commit e002758
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 9 deletions.
39 changes: 30 additions & 9 deletions packages/expect/src/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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) {
Expand All @@ -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,
)
})
Expand All @@ -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,
)
})
Expand All @@ -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,
)
})
Expand All @@ -370,19 +370,40 @@ 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,
)
})
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',
Number.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(
obj === null,
'expected #{this} to be null',
'expected #{this} not to be null',
null,
obj,
)
})
def('toBeDefined', function () {
const obj = utils.flag(this, 'object')
Expand Down
81 changes: 81 additions & 0 deletions test/core/test/__snapshots__/jest-expect.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,87 @@ 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 {
"hello": "world",
}",
"diff": "- Expected:
false
+ Received:
Object {
"hello": "world",
}",
"expected": "false",
"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",
}
`;
exports[`toHaveBeenNthCalledWith error 1`] = `
{
"actual": "Array [
Expand Down
8 changes: 8 additions & 0 deletions test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1354,3 +1354,11 @@ it('toMatch/toContain diff', () => {
})

it('timeout', () => new Promise(resolve => setTimeout(resolve, 500)))

it('diff', () => {
snapshotError(() => expect(undefined).toBeTruthy())
snapshotError(() => expect({ hello: 'world' }).toBeFalsy())
snapshotError(() => expect({ hello: 'world' }).toBeNaN())
snapshotError(() => expect({ hello: 'world' }).toBeUndefined())
snapshotError(() => expect({ hello: 'world' }).toBeNull())
})

0 comments on commit e002758

Please sign in to comment.