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)!: pass current equality testers to asymmetric matcher #6825

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 7 additions & 9 deletions packages/expect/src/jest-asymmetric-matchers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ChaiPlugin, MatcherState } from './types'
import type { ChaiPlugin, MatcherState, Tester } from './types'
import { GLOBAL_EXPECT } from './constants'
import {
diff,
Expand All @@ -17,7 +17,7 @@ import {
import { getState } from './state'

export interface AsymmetricMatcherInterface {
asymmetricMatch: (other: unknown) => boolean
asymmetricMatch: (other: unknown, customTesters?: Array<Tester>) => boolean
toString: () => string
getExpectedType?: () => string
toAsymmetricMatcher?: () => string
Expand Down Expand Up @@ -48,7 +48,7 @@ export abstract class AsymmetricMatcher<
}
}

abstract asymmetricMatch(other: unknown): boolean
abstract asymmetricMatch(other: unknown, customTesters?: Array<Tester>): boolean
abstract toString(): string
getExpectedType?(): string
toAsymmetricMatcher?(): string;
Expand Down Expand Up @@ -134,7 +134,7 @@ export class ObjectContaining extends AsymmetricMatcher<
return this.hasProperty(this.getPrototype(obj), property)
}

asymmetricMatch(other: any) {
asymmetricMatch(other: any, customTesters?: Array<Tester>) {
if (typeof this.sample !== 'object') {
throw new TypeError(
`You must provide an object to ${this.toString()}, not '${typeof this
Expand All @@ -144,14 +144,13 @@ export class ObjectContaining extends AsymmetricMatcher<

let result = true

const matcherContext = this.getMatcherContext()
for (const property in this.sample) {
if (
!this.hasProperty(other, property)
|| !equals(
this.sample[property],
other[property],
matcherContext.customTesters,
customTesters,
)
) {
result = false
Expand All @@ -176,21 +175,20 @@ export class ArrayContaining<T = unknown> extends AsymmetricMatcher<Array<T>> {
super(sample, inverse)
}

asymmetricMatch(other: Array<T>) {
asymmetricMatch(other: Array<T>, customTesters?: Array<Tester>) {
if (!Array.isArray(this.sample)) {
throw new TypeError(
`You must provide an array to ${this.toString()}, not '${typeof this
.sample}'.`,
)
}

const matcherContext = this.getMatcherContext()
const result
= this.sample.length === 0
|| (Array.isArray(other)
&& this.sample.every(item =>
other.some(another =>
equals(item, another, matcherContext.customTesters),
equals(item, another, customTesters),
),
))

Expand Down
11 changes: 6 additions & 5 deletions packages/expect/src/jest-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

import type { AsymmetricMatcher } from './jest-asymmetric-matchers'
import type { Tester, TesterContext } from './types'
import { isObject } from '@vitest/utils'

Expand All @@ -38,7 +39,7 @@ export function equals(

const functionToString = Function.prototype.toString

export function isAsymmetric(obj: any) {
export function isAsymmetric(obj: any): obj is AsymmetricMatcher<any> {
return (
!!obj
&& typeof obj === 'object'
Expand Down Expand Up @@ -67,7 +68,7 @@ export function hasAsymmetric(obj: any, seen = new Set()): boolean {
return false
}

function asymmetricMatch(a: any, b: any) {
function asymmetricMatch(a: any, b: any, customTesters: Array<Tester>) {
const asymmetricA = isAsymmetric(a)
const asymmetricB = isAsymmetric(b)

Expand All @@ -76,11 +77,11 @@ function asymmetricMatch(a: any, b: any) {
}

if (asymmetricA) {
return a.asymmetricMatch(b)
return a.asymmetricMatch(b, customTesters)
}

if (asymmetricB) {
return b.asymmetricMatch(a)
return b.asymmetricMatch(a, customTesters)
}
}

Expand All @@ -96,7 +97,7 @@ function eq(
): boolean {
let result = true

const asymmetricResult = asymmetricMatch(a, b)
const asymmetricResult = asymmetricMatch(a, b, customTesters)
if (asymmetricResult !== undefined) {
return asymmetricResult
}
Expand Down
30 changes: 30 additions & 0 deletions test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,36 @@ describe('jest-expect', () => {
}).toThrowErrorMatchingInlineSnapshot(`[AssertionError: expected { sum: 0.30000000000000004 } to deeply equal { sum: NumberCloseTo 0.4 (2 digits) }]`)
})

it('asymmetric matchers and equality testers', () => {
// iterable equality testers
expect([new Set(['x'])]).toEqual(
expect.arrayContaining([new Set(['x'])]),
)
expect([new Set()]).not.toEqual(
expect.arrayContaining([new Set(['x'])]),
)
expect({ foo: new Set(['x']) }).toEqual(
expect.objectContaining({ foo: new Set(['x']) }),
)
expect({ foo: new Set() }).not.toEqual(
expect.objectContaining({ foo: new Set(['x']) }),
)

// `toStrictEqual` testers
class Stock {
constructor(public type: string) {}
}
expect([new Stock('x')]).toEqual(
expect.arrayContaining([{ type: 'x' }]),
)
expect([new Stock('x')]).not.toStrictEqual(
expect.arrayContaining([{ type: 'x' }]),
)
expect([new Stock('x')]).toStrictEqual(
expect.arrayContaining([new Stock('x')]),
)
})

it('asymmetric matchers negate', () => {
expect('bar').toEqual(expect.not.stringContaining('zoo'))
expect('bar').toEqual(expect.not.stringMatching(/zoo/))
Expand Down
Loading