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(expect)!: loosen toMatchObject and objectContaining for Proxy.get #6675

Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion packages/expect/src/jest-asymmetric-matchers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isObject } from '@vitest/utils'
import type { ChaiPlugin, MatcherState } from './types'
import { GLOBAL_EXPECT } from './constants'
import { getState } from './state'
Expand Down Expand Up @@ -147,7 +148,7 @@ export class ObjectContaining extends AsymmetricMatcher<
const matcherContext = this.getMatcherContext()
for (const property in this.sample) {
if (
!this.hasProperty(other, property)
!isObject(other)
|| !equals(
this.sample[property],
other[property],
Expand Down
3 changes: 1 addition & 2 deletions packages/expect/src/jest-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,7 @@ export function subsetEquality(
seenReferences.set(subset[key], true)
}
const result
= object != null
&& hasPropertyInObject(object, key)
= isObject(object)
&& equals(object[key], subset[key], [
...filteredCustomTesters,
subsetEqualityWithContext(seenReferences),
Expand Down
72 changes: 72 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,78 @@ exports[`asymmetric matcher error 23`] = `
}
`;

exports[`proxy 1`] = `
{
"actual": "Object {}",
"diff": "- Expected
+ Received

- Object {
- "key": "no",
- }
+ Object {}",
"expected": "Object {
"key": "no",
}",
"message": "expected {} to match object { key: 'no' }",
}
`;

exports[`proxy 2`] = `
{
"actual": "Object {
"key": "value",
}",
"diff": "- Expected
+ Received

Object {
- "key": "no",
+ "key": "value",
}",
"expected": "Object {
"key": "no",
}",
"message": "expected { key: 'value' } to match object { key: 'no' }",
}
`;

exports[`proxy equality 1`] = `
{
"actual": "Object {}",
"diff": "- Expected
+ Received

- Object {
- "key": "no",
- }
+ Object {}",
"expected": "Object {
"key": "no",
}",
"message": "expected {} to match object { key: 'no' }",
}
`;

exports[`proxy equality 2`] = `
{
"actual": "Object {
"key": "value",
}",
"diff": "- Expected
+ Received

Object {
- "key": "no",
+ "key": "value",
}",
"expected": "Object {
"key": "no",
}",
"message": "expected { key: 'value' } to match object { key: 'no' }",
}
`;

exports[`toHaveBeenNthCalledWith error 1`] = `
{
"actual": "Array [
Expand Down
50 changes: 50 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,53 @@ it('toMatch/toContain diff', () => {
})

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

it('proxy equality', () => {
const proxy = new Proxy(
{},
{
get(target, prop, receiver) {
if (prop === 'key') {
return 'value'
}
return Reflect.get(target, prop, receiver)
},
},
)

expect(proxy).toMatchObject({ key: 'value' })
snapshotError(() => expect(proxy).toMatchObject({ key: 'no' }))

const proxyWithKeys = new Proxy(
{},
{
get(target, prop, receiver) {
if (prop === 'key') {
return 'value'
}
return Reflect.get(target, prop, receiver)
},
// need more handlers to enumerate keys for diff
ownKeys(target) {
return [...Reflect.ownKeys(target), 'key']
},
getOwnPropertyDescriptor(target, prop) {
if (prop === 'key') {
return { configurable: true, enumerable: true }
}
return Reflect.getOwnPropertyDescriptor(target, prop)
},
},
)

expect(proxyWithKeys).toMatchObject({ key: 'value' })
snapshotError(() => expect(proxyWithKeys).toMatchObject({ key: 'no' }))

// objectContaining
expect(proxy).toEqual(expect.objectContaining({ key: 'value' }))
expect(proxyWithKeys).toEqual(expect.objectContaining({ key: 'value' }))

// toEqual
expect(proxy).not.toEqual({ key: 'value' })
expect(proxyWithKeys).toEqual({ key: 'value' }) // shouldn't succeed?
})
Loading