Skip to content

Commit e002758

Browse files
authoredOct 12, 2024··
fix: fix error diff of toBeNaN, toBeUndefined, toBeNull, toBeTruthy, toBeFalsy (#6697)
1 parent 5e6de27 commit e002758

File tree

3 files changed

+119
-9
lines changed

3 files changed

+119
-9
lines changed
 

‎packages/expect/src/jest-expect.ts

+30-9
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
309309
Boolean(obj),
310310
'expected #{this} to be truthy',
311311
'expected #{this} to not be truthy',
312+
true,
312313
obj,
313-
false,
314314
)
315315
})
316316
def('toBeFalsy', function () {
@@ -319,8 +319,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
319319
!obj,
320320
'expected #{this} to be falsy',
321321
'expected #{this} to not be falsy',
322-
obj,
323322
false,
323+
obj,
324324
)
325325
})
326326
def('toBeGreaterThan', function (expected: number | bigint) {
@@ -331,8 +331,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
331331
actual > expected,
332332
`expected ${actual} to be greater than ${expected}`,
333333
`expected ${actual} to be not greater than ${expected}`,
334-
actual,
335334
expected,
335+
actual,
336336
false,
337337
)
338338
})
@@ -344,8 +344,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
344344
actual >= expected,
345345
`expected ${actual} to be greater than or equal to ${expected}`,
346346
`expected ${actual} to be not greater than or equal to ${expected}`,
347-
actual,
348347
expected,
348+
actual,
349349
false,
350350
)
351351
})
@@ -357,8 +357,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
357357
actual < expected,
358358
`expected ${actual} to be less than ${expected}`,
359359
`expected ${actual} to be not less than ${expected}`,
360-
actual,
361360
expected,
361+
actual,
362362
false,
363363
)
364364
})
@@ -370,19 +370,40 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
370370
actual <= expected,
371371
`expected ${actual} to be less than or equal to ${expected}`,
372372
`expected ${actual} to be not less than or equal to ${expected}`,
373-
actual,
374373
expected,
374+
actual,
375375
false,
376376
)
377377
})
378378
def('toBeNaN', function () {
379-
return this.be.NaN
379+
const obj = utils.flag(this, 'object')
380+
this.assert(
381+
Number.isNaN(obj),
382+
'expected #{this} to be NaN',
383+
'expected #{this} not to be NaN',
384+
Number.NaN,
385+
obj,
386+
)
380387
})
381388
def('toBeUndefined', function () {
382-
return this.be.undefined
389+
const obj = utils.flag(this, 'object')
390+
this.assert(
391+
undefined === obj,
392+
'expected #{this} to be undefined',
393+
'expected #{this} not to be undefined',
394+
undefined,
395+
obj,
396+
)
383397
})
384398
def('toBeNull', function () {
385-
return this.be.null
399+
const obj = utils.flag(this, 'object')
400+
this.assert(
401+
obj === null,
402+
'expected #{this} to be null',
403+
'expected #{this} not to be null',
404+
null,
405+
obj,
406+
)
386407
})
387408
def('toBeDefined', function () {
388409
const obj = utils.flag(this, 'object')

‎test/core/test/__snapshots__/jest-expect.test.ts.snap

+81
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,87 @@ exports[`asymmetric matcher error 23`] = `
311311
}
312312
`;
313313
314+
exports[`diff 1`] = `
315+
{
316+
"actual": "undefined",
317+
"diff": "- Expected:
318+
true
319+
320+
+ Received:
321+
undefined",
322+
"expected": "true",
323+
"message": "expected undefined to be truthy",
324+
}
325+
`;
326+
327+
exports[`diff 2`] = `
328+
{
329+
"actual": "Object {
330+
"hello": "world",
331+
}",
332+
"diff": "- Expected:
333+
false
334+
335+
+ Received:
336+
Object {
337+
"hello": "world",
338+
}",
339+
"expected": "false",
340+
"message": "expected { hello: 'world' } to be falsy",
341+
}
342+
`;
343+
344+
exports[`diff 3`] = `
345+
{
346+
"actual": "Object {
347+
"hello": "world",
348+
}",
349+
"diff": "- Expected:
350+
NaN
351+
352+
+ Received:
353+
Object {
354+
"hello": "world",
355+
}",
356+
"expected": "NaN",
357+
"message": "expected { hello: 'world' } to be NaN",
358+
}
359+
`;
360+
361+
exports[`diff 4`] = `
362+
{
363+
"actual": "Object {
364+
"hello": "world",
365+
}",
366+
"diff": "- Expected:
367+
undefined
368+
369+
+ Received:
370+
Object {
371+
"hello": "world",
372+
}",
373+
"expected": "undefined",
374+
"message": "expected { hello: 'world' } to be undefined",
375+
}
376+
`;
377+
378+
exports[`diff 5`] = `
379+
{
380+
"actual": "Object {
381+
"hello": "world",
382+
}",
383+
"diff": "- Expected:
384+
null
385+
386+
+ Received:
387+
Object {
388+
"hello": "world",
389+
}",
390+
"expected": "null",
391+
"message": "expected { hello: 'world' } to be null",
392+
}
393+
`;
394+
314395
exports[`toHaveBeenNthCalledWith error 1`] = `
315396
{
316397
"actual": "Array [

‎test/core/test/jest-expect.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1354,3 +1354,11 @@ it('toMatch/toContain diff', () => {
13541354
})
13551355

13561356
it('timeout', () => new Promise(resolve => setTimeout(resolve, 500)))
1357+
1358+
it('diff', () => {
1359+
snapshotError(() => expect(undefined).toBeTruthy())
1360+
snapshotError(() => expect({ hello: 'world' }).toBeFalsy())
1361+
snapshotError(() => expect({ hello: 'world' }).toBeNaN())
1362+
snapshotError(() => expect({ hello: 'world' }).toBeUndefined())
1363+
snapshotError(() => expect({ hello: 'world' }).toBeNull())
1364+
})

0 commit comments

Comments
 (0)
Please sign in to comment.