Skip to content

Commit

Permalink
fix: array reference in subset (#2)
Browse files Browse the repository at this point in the history
* wip

* fix: array reference in subset
  • Loading branch information
hi-ogawa committed Apr 16, 2024
1 parent a13e9ab commit c2274b6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
21 changes: 11 additions & 10 deletions packages/expect/src/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,18 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
const pass = jestEquals(actual, expected, [...customTesters, iterableEquality, subsetEquality])
const isNot = utils.flag(this, 'negate') as boolean
const { subset: actualSubset, stripped } = getObjectSubset(actual, expected)
const msg = utils.getMessage(
this,
[
pass,
'expected #{this} to match object #{exp}',
'expected #{this} to not match object #{exp}',
expected,
actualSubset,
],
)
if ((pass && isNot) || (!pass && !isNot)) {
const msg = utils.getMessage(
this,
[
pass,
'expected #{this} to match object #{exp}',
'expected #{this} to not match object #{exp}',
expected,
actualSubset,
false,
],
)
const message = stripped === 0 ? msg : `${msg}\n(${stripped} matching ${stripped === 1 ? 'property' : 'properties'} omitted from actual)`
throw new AssertionError(message, { showDiff: true, expected, actual: actualSubset })
}
Expand Down
2 changes: 1 addition & 1 deletion packages/expect/src/jest-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ export function subsetEquality(object: unknown, subset: unknown, customTesters:
return undefined

return Object.keys(subset).every((key) => {
if (isObjectWithKeys(subset[key])) {
if (typeof subset[key] === 'object') {
if (seenReferences.has(subset[key]))
return equals(object[key], subset[key], filteredCustomTesters)

Expand Down
6 changes: 2 additions & 4 deletions test/core/test/expect-circular.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ describe('circular equality', () => {
return obj
}
expect(gen()).toEqual(gen())
// TODO
expect(() => expect(gen()).toMatchObject(gen())).toThrow()
expect(gen()).toMatchObject(gen())
})

test('object, array', () => {
Expand All @@ -56,7 +55,6 @@ describe('circular equality', () => {
return a
}
expect(gen()).toEqual(gen())
// TODO
expect(() => expect(gen()).toMatchObject(gen())).toThrow()
expect(gen()).toMatchObject(gen())
})
})

0 comments on commit c2274b6

Please sign in to comment.