Skip to content
Merged
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,6 +1,6 @@
/* eslint-disable unicorn/no-instanceof-builtins -- we check both */

import type { ChaiPlugin, MatcherState } from './types'
import type { ChaiPlugin, MatcherState, Tester } from './types'
import { GLOBAL_EXPECT } from './constants'
import {
diff,
Expand All @@ -19,7 +19,7 @@ import {
import { getState } from './state'

export interface AsymmetricMatcherInterface {
asymmetricMatch: (other: unknown) => boolean
asymmetricMatch: (other: unknown, customTesters?: Array<Tester>) => boolean
Copy link
Contributor Author

Choose a reason for hiding this comment

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

TODO: see if we can keep the API same without extending arguments.

toString: () => string
getExpectedType?: () => string
toAsymmetricMatcher?: () => string
Expand Down Expand Up @@ -50,7 +50,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 @@ -147,7 +147,7 @@ export class ObjectContaining extends AsymmetricMatcher<
]
}

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

let result = true

const matcherContext = this.getMatcherContext()
const properties = this.getProperties(this.sample)
for (const property of properties) {
if (
Expand All @@ -171,7 +170,7 @@ export class ObjectContaining extends AsymmetricMatcher<
if (!equals(
value,
otherValue,
matcherContext.customTesters,
customTesters,
)
) {
result = false
Expand All @@ -196,21 +195,20 @@ export class ArrayContaining<T = unknown> extends AsymmetricMatcher<Array<T>> {
super(sample, inverse)
}

asymmetricMatch(other: Array<T>): boolean {
asymmetricMatch(other: Array<T>, customTesters?: Array<Tester>): boolean {
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): boolean {
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: Set<any> = 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 @@ -203,6 +203,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