Skip to content

Commit

Permalink
feat: allow extending toEqual (fix #2875)
Browse files Browse the repository at this point in the history
  • Loading branch information
tigranmk committed Jan 5, 2024
1 parent a73c1c2 commit a69fae9
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 40 deletions.
3 changes: 2 additions & 1 deletion examples/vitesse/src/auto-import.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,5 @@ declare global {
declare global {
// @ts-ignore
export type { Component, ComponentPublicInstance, ComputedRef, InjectionKey, PropType, Ref, VNode } from 'vue'
}
export type { Component, ComponentPublicInstance, ComputedRef, ExtractDefaultPropTypes, ExtractPropTypes, ExtractPublicPropTypes, InjectionKey, PropType, Ref, VNode, WritableComputedRef } from 'vue'
}
1 change: 1 addition & 0 deletions packages/expect/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export * from './constants'
export * from './types'
export { getState, setState } from './state'
export { JestChaiExpect } from './jest-expect'
export { addCustomEqualityTesters } from './jest-matcher-utils'
export { JestExtend } from './jest-extend'
export { setupColors } from '@vitest/utils'
10 changes: 6 additions & 4 deletions packages/expect/src/jest-asymmetric-matchers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ChaiPlugin, MatcherState } from './types'
import { GLOBAL_EXPECT } from './constants'
import { getState } from './state'
import { diff, getMatcherUtils, stringify } from './jest-matcher-utils'
import { diff, getCustomEqualityTesters, getMatcherUtils, stringify } from './jest-matcher-utils'

import { equals, isA, iterableEquality, pluralize, subsetEquality } from './jest-utils'

Expand All @@ -26,7 +26,7 @@ export abstract class AsymmetricMatcher<
...getState(expect || (globalThis as any)[GLOBAL_EXPECT]),
equals,
isNot: this.inverse,
customTesters: [],
customTesters: getCustomEqualityTesters(),
utils: {
...getMatcherUtils(),
diff,
Expand Down Expand Up @@ -116,8 +116,9 @@ export class ObjectContaining extends AsymmetricMatcher<Record<string, unknown>>

let result = true

const matcherContext = this.getMatcherContext()
for (const property in this.sample) {
if (!this.hasProperty(other, property) || !equals(this.sample[property], other[property])) {
if (!this.hasProperty(other, property) || !equals(this.sample[property], other[property], matcherContext.customTesters)) {
result = false
break
}
Expand Down Expand Up @@ -149,11 +150,12 @@ export class ArrayContaining<T = unknown> extends AsymmetricMatcher<Array<T>> {
)
}

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

return this.inverse ? !result : result
Expand Down
15 changes: 9 additions & 6 deletions packages/expect/src/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { Test } from '@vitest/runner'
import type { Assertion, ChaiPlugin } from './types'
import { arrayBufferEquality, generateToBeMessage, iterableEquality, equals as jestEquals, sparseArrayEquality, subsetEquality, typeEquality } from './jest-utils'
import type { AsymmetricMatcher } from './jest-asymmetric-matchers'
import { diff, stringify } from './jest-matcher-utils'
import { diff, getCustomEqualityTesters, stringify } from './jest-matcher-utils'
import { JEST_MATCHERS_OBJECT } from './constants'
import { recordAsyncExpect, wrapSoft } from './utils'

Expand All @@ -23,6 +23,7 @@ declare class DOMTokenList {
export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
const { AssertionError } = chai
const c = () => getColors()
const customTesters = getCustomEqualityTesters()

function def(name: keyof Assertion | (keyof Assertion)[], fn: ((this: Chai.AssertionStatic & Assertion, ...args: any[]) => any)) {
const addMethod = (n: keyof Assertion) => {
Expand Down Expand Up @@ -80,7 +81,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
const equal = jestEquals(
actual,
expected,
[iterableEquality],
[...customTesters, iterableEquality],
)

return this.assert(
Expand All @@ -98,6 +99,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
obj,
expected,
[
...customTesters,
iterableEquality,
typeEquality,
sparseArrayEquality,
Expand Down Expand Up @@ -125,6 +127,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
actual,
expected,
[
...customTesters,
iterableEquality,
typeEquality,
sparseArrayEquality,
Expand All @@ -140,7 +143,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
const toEqualPass = jestEquals(
actual,
expected,
[iterableEquality],
[...customTesters, iterableEquality],
)

if (toEqualPass)
Expand All @@ -159,7 +162,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
def('toMatchObject', function (expected) {
const actual = this._obj
return this.assert(
jestEquals(actual, expected, [iterableEquality, subsetEquality]),
jestEquals(actual, expected, [...customTesters, iterableEquality, subsetEquality]),
'expected #{this} to match object #{exp}',
'expected #{this} to not match object #{exp}',
expected,
Expand Down Expand Up @@ -208,7 +211,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
def('toContainEqual', function (expected) {
const obj = utils.flag(this, 'object')
const index = Array.from(obj).findIndex((item) => {
return jestEquals(item, expected)
return jestEquals(item, expected, customTesters)
})

this.assert(
Expand Down Expand Up @@ -339,7 +342,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
return utils.getPathInfo(actual, propertyName)
}
const { value, exists } = getValue()
const pass = exists && (args.length === 1 || jestEquals(expected, value))
const pass = exists && (args.length === 1 || jestEquals(expected, value, customTesters))

const valueString = args.length === 1 ? '' : ` with value ${utils.objDisplay(expected)}`

Expand Down
5 changes: 2 additions & 3 deletions packages/expect/src/jest-extend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ASYMMETRIC_MATCHERS_OBJECT, JEST_MATCHERS_OBJECT } from './constants'
import { AsymmetricMatcher } from './jest-asymmetric-matchers'
import { getState } from './state'

import { diff, getMatcherUtils, stringify } from './jest-matcher-utils'
import { diff, getCustomEqualityTesters, getMatcherUtils, stringify } from './jest-matcher-utils'

import {
equals,
Expand All @@ -33,8 +33,7 @@ function getMatcherState(assertion: Chai.AssertionStatic & Chai.Assertion, expec

const matcherState: MatcherState = {
...getState(expect),
// TODO: implement via expect.addEqualityTesters
customTesters: [],
customTesters: getCustomEqualityTesters(),
isNot,
utils: jestUtils,
promise,
Expand Down
23 changes: 21 additions & 2 deletions packages/expect/src/jest-matcher-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getColors, stringify } from '@vitest/utils'
import type { MatcherHintOptions } from './types'
import { getColors, getType, stringify } from '@vitest/utils'
import type { MatcherHintOptions, Tester } from './types'
import { JEST_MATCHERS_OBJECT } from './constants'

export { diff } from '@vitest/utils/diff'
export { stringify }
Expand Down Expand Up @@ -101,3 +102,21 @@ export function getMatcherUtils() {
printExpected,
}
}

export function addCustomEqualityTesters(newTesters: Array<Tester>): void {
if (!Array.isArray(newTesters)) {
throw new TypeError(
`expect.customEqualityTesters: Must be set to an array of Testers. Was given "${getType(
newTesters,
)}"`,
)
}

(globalThis as any)[JEST_MATCHERS_OBJECT].customEqualityTesters.push(
...newTesters,
)
}

export function getCustomEqualityTesters(): Array<Tester> {
return (globalThis as any)[JEST_MATCHERS_OBJECT].customEqualityTesters
}
52 changes: 32 additions & 20 deletions packages/expect/src/jest-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { isObject } from '@vitest/utils'
import type { Tester } from './types'
import type { Tester, TesterContext } from './types'

// Extracted out of jasmine 2.5.2
export function equals(
Expand Down Expand Up @@ -87,8 +87,9 @@ function eq(
if (asymmetricResult !== undefined)
return asymmetricResult

const testerContext: TesterContext = { equals }
for (let i = 0; i < customTesters.length; i++) {
const customTesterResult = customTesters[i](a, b)
const customTesterResult = customTesters[i].call(testerContext, a, b, customTesters)
if (customTesterResult !== undefined)
return customTesterResult
}
Expand Down Expand Up @@ -298,7 +299,7 @@ function hasIterator(object: any) {
return !!(object != null && object[IteratorSymbol])
}

export function iterableEquality(a: any, b: any, aStack: Array<any> = [], bStack: Array<any> = []): boolean | undefined {
export function iterableEquality(a: any, b: any, customTesters: Array<Tester> = [], aStack: Array<any> = [], bStack: Array<any> = []): boolean | undefined {
if (
typeof a !== 'object'
|| typeof b !== 'object'
Expand All @@ -324,7 +325,20 @@ export function iterableEquality(a: any, b: any, aStack: Array<any> = [], bStack
aStack.push(a)
bStack.push(b)

const iterableEqualityWithStack = (a: any, b: any) => iterableEquality(a, b, [...aStack], [...bStack])
const filteredCustomTesters: Array<Tester> = [
...customTesters.filter(t => t !== iterableEquality),
iterableEqualityWithStack,
]

function iterableEqualityWithStack(a: any, b: any) {
return iterableEquality(
a,
b,
[...filteredCustomTesters],
[...aStack],
[...bStack],
)
}

if (a.size !== undefined) {
if (a.size !== b.size) {
Expand All @@ -336,7 +350,7 @@ export function iterableEquality(a: any, b: any, aStack: Array<any> = [], bStack
if (!b.has(aValue)) {
let has = false
for (const bValue of b) {
const isEqual = equals(aValue, bValue, [iterableEqualityWithStack])
const isEqual = equals(aValue, bValue, filteredCustomTesters)
if (isEqual === true)
has = true
}
Expand All @@ -357,20 +371,16 @@ export function iterableEquality(a: any, b: any, aStack: Array<any> = [], bStack
for (const aEntry of a) {
if (
!b.has(aEntry[0])
|| !equals(aEntry[1], b.get(aEntry[0]), [iterableEqualityWithStack])
|| !equals(aEntry[1], b.get(aEntry[0]), filteredCustomTesters)
) {
let has = false
for (const bEntry of b) {
const matchedKey = equals(aEntry[0], bEntry[0], [
iterableEqualityWithStack,
])
const matchedKey = equals(aEntry[0], bEntry[0], filteredCustomTesters)

let matchedValue = false
if (matchedKey === true) {
matchedValue = equals(aEntry[1], bEntry[1], [
iterableEqualityWithStack,
])
}
if (matchedKey === true)
matchedValue = equals(aEntry[1], bEntry[1], filteredCustomTesters)

if (matchedValue === true)
has = true
}
Expand All @@ -394,7 +404,7 @@ export function iterableEquality(a: any, b: any, aStack: Array<any> = [], bStack
const nextB = bIterator.next()
if (
nextB.done
|| !equals(aValue, nextB.value, [iterableEqualityWithStack])
|| !equals(aValue, nextB.value, filteredCustomTesters)
)
return false
}
Expand Down Expand Up @@ -430,7 +440,8 @@ function isObjectWithKeys(a: any) {
&& !(a instanceof Date)
}

export function subsetEquality(object: unknown, subset: unknown): boolean | undefined {
export function subsetEquality(object: unknown, subset: unknown, customTesters: Array<Tester> = []): boolean | undefined {
const filteredCustomTesters = customTesters.filter(t => t !== subsetEquality)
// subsetEquality needs to keep track of the references
// it has already visited to avoid infinite loops in case
// there are circular references in the subset passed to it.
Expand All @@ -443,15 +454,15 @@ export function subsetEquality(object: unknown, subset: unknown): boolean | unde
return Object.keys(subset).every((key) => {
if (isObjectWithKeys(subset[key])) {
if (seenReferences.has(subset[key]))
return equals(object[key], subset[key], [iterableEquality])
return equals(object[key], subset[key], filteredCustomTesters)

seenReferences.set(subset[key], true)
}
const result
= object != null
&& hasPropertyInObject(object, key)
&& equals(object[key], subset[key], [
iterableEquality,
...filteredCustomTesters,
subsetEqualityWithContext(seenReferences),
])
// The main goal of using seenReference is to avoid circular node on tree.
Expand Down Expand Up @@ -504,15 +515,16 @@ export function arrayBufferEquality(a: unknown, b: unknown): boolean | undefined
return true
}

export function sparseArrayEquality(a: unknown, b: unknown): boolean | undefined {
export function sparseArrayEquality(a: unknown, b: unknown, customTesters: Array<Tester> = []): boolean | undefined {
if (!Array.isArray(a) || !Array.isArray(b))
return undefined

// A sparse array [, , 1] will have keys ["2"] whereas [undefined, undefined, 1] will have keys ["0", "1", "2"]
const aKeys = Object.keys(a)
const bKeys = Object.keys(b)
const filteredCustomTesters = customTesters.filter(t => t !== sparseArrayEquality)
return (
equals(a, b, [iterableEquality, typeEquality], true) && equals(aKeys, bKeys)
equals(a, b, filteredCustomTesters, true) && equals(aKeys, bKeys)
)
}

Expand Down
4 changes: 3 additions & 1 deletion packages/expect/src/state.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { ExpectStatic, MatcherState } from './types'
import type { ExpectStatic, MatcherState, Tester } from './types'
import { ASYMMETRIC_MATCHERS_OBJECT, GLOBAL_EXPECT, JEST_MATCHERS_OBJECT, MATCHERS_OBJECT } from './constants'

if (!Object.prototype.hasOwnProperty.call(globalThis, MATCHERS_OBJECT)) {
const globalState = new WeakMap<ExpectStatic, MatcherState>()
const matchers = Object.create(null)
const customEqualityTesters: Array<Tester> = []
const assymetricMatchers = Object.create(null)
Object.defineProperty(globalThis, MATCHERS_OBJECT, {
get: () => globalState,
Expand All @@ -13,6 +14,7 @@ if (!Object.prototype.hasOwnProperty.call(globalThis, MATCHERS_OBJECT)) {
get: () => ({
state: globalState.get((globalThis as any)[GLOBAL_EXPECT]),
matchers,
customEqualityTesters,
}),
})
Object.defineProperty(globalThis, ASYMMETRIC_MATCHERS_OBJECT, {
Expand Down
18 changes: 16 additions & 2 deletions packages/expect/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,21 @@ import type { diff, getMatcherUtils, stringify } from './jest-matcher-utils'

export type ChaiPlugin = Chai.ChaiPlugin

export type Tester = (a: any, b: any) => boolean | undefined

export type Tester = (
this: TesterContext,
a: any,
b: any,
customTesters: Array<Tester>,
) => boolean | undefined

export interface TesterContext {
equals: (
a: unknown,
b: unknown,
customTesters?: Array<Tester>,
strictCheck?: boolean,
) => boolean
}
export type { DiffOptions } from '@vitest/utils/diff'

export interface MatcherHintOptions {
Expand Down Expand Up @@ -81,6 +94,7 @@ export interface ExpectStatic extends Chai.ExpectStatic, AsymmetricMatchersConta
unreachable(message?: string): never
soft<T>(actual: T, message?: string): Assertion<T>
extend(expects: MatchersObject): void
addEqualityTesters(testers: Array<Tester>): void
assertions(expected: number): void
hasAssertions(): void
anything(): any
Expand Down
4 changes: 3 additions & 1 deletion packages/vitest/src/integrations/chai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as chai from 'chai'
import './setup'
import type { TaskPopulated, Test } from '@vitest/runner'
import { getCurrentTest } from '@vitest/runner'
import { ASYMMETRIC_MATCHERS_OBJECT, GLOBAL_EXPECT, getState, setState } from '@vitest/expect'
import { ASYMMETRIC_MATCHERS_OBJECT, GLOBAL_EXPECT, addCustomEqualityTesters, getState, setState } from '@vitest/expect'
import type { Assertion, ExpectStatic } from '@vitest/expect'
import type { MatcherState } from '../../types/chai'
import { getFullName } from '../../utils/tasks'
Expand Down Expand Up @@ -46,6 +46,8 @@ export function createExpect(test?: TaskPopulated) {

// @ts-expect-error untyped
expect.extend = matchers => chai.expect.extend(expect, matchers)
expect.addEqualityTesters = customTesters =>
addCustomEqualityTesters(customTesters)

expect.soft = (...args) => {
const assert = expect(...args)
Expand Down
Loading

0 comments on commit a69fae9

Please sign in to comment.