Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix error diff of toBeNaN, toBeUndefined, toBeNull, toBeTruthy, toBeFalsy #6697

Merged
merged 3 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
!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 @@
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 @@
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 @@
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 @@
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',
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,
)
Comment on lines +389 to +396
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chai's .be.undefined doesn't include diff https://github.com/chaijs/chai/blob/1b1780582c07eefb3781333e24316865b9db7ae8/lib/chai/core/assertions.js#L830-L836, so I copied these three from chai and added expected (4th arg) and actual (5th arg)

})
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
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())
})
Loading