/
jest-extend.ts
133 lines (109 loc) · 4.07 KB
/
jest-extend.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { util } from 'chai'
import type {
ChaiPlugin,
MatcherState,
MatchersObject,
SyncExpectationResult,
} from '../../types/chai'
import { getSnapshotClient } from '../snapshot/chai'
import { AsymmetricMatcher } from './jest-asymmetric-matchers'
import { getState } from './jest-expect'
import * as matcherUtils from './jest-matcher-utils'
import {
equals,
iterableEquality,
subsetEquality,
} from './jest-utils'
const isAsyncFunction = (fn: unknown) =>
typeof fn === 'function' && (fn as any)[Symbol.toStringTag] === 'AsyncFunction'
const getMatcherState = (assertion: Chai.AssertionStatic & Chai.Assertion, expect: Vi.ExpectStatic) => {
const obj = assertion._obj
const isNot = util.flag(assertion, 'negate') as boolean
const promise = util.flag(assertion, 'promise') || ''
const jestUtils = {
...matcherUtils,
iterableEquality,
subsetEquality,
}
const matcherState: MatcherState = {
...getState(expect),
isNot,
utils: jestUtils,
promise,
equals,
// needed for built-in jest-snapshots, but we don't use it
suppressedErrors: [],
snapshotState: getSnapshotClient().snapshotState!,
}
return {
state: matcherState,
isNot,
obj,
}
}
class JestExtendError extends Error {
constructor(message: string, public actual?: any, public expected?: any) {
super(message)
}
}
function JestExtendPlugin(expect: Vi.ExpectStatic, matchers: MatchersObject): ChaiPlugin {
return (c, utils) => {
Object.entries(matchers).forEach(([expectAssertionName, expectAssertion]) => {
function expectSyncWrapper(this: Chai.AssertionStatic & Chai.Assertion, ...args: any[]) {
const { state, isNot, obj } = getMatcherState(this, expect)
// @ts-expect-error args wanting tuple
const { pass, message, actual, expected } = expectAssertion.call(state, obj, ...args) as SyncExpectationResult
if ((pass && isNot) || (!pass && !isNot))
throw new JestExtendError(message(), actual, expected)
}
async function expectAsyncWrapper(this: Chai.AssertionStatic & Chai.Assertion, ...args: any[]) {
const { state, isNot, obj } = getMatcherState(this, expect)
// @ts-expect-error args wanting tuple
const { pass, message, actual, expected } = await expectAssertion.call(state, obj, ...args) as SyncExpectationResult
if ((pass && isNot) || (!pass && !isNot))
throw new JestExtendError(message(), actual, expected)
}
const expectAssertionWrapper = isAsyncFunction(expectAssertion) ? expectAsyncWrapper : expectSyncWrapper
utils.addMethod(c.Assertion.prototype, expectAssertionName, expectAssertionWrapper)
class CustomMatcher extends AsymmetricMatcher<[unknown, ...unknown[]]> {
constructor(inverse = false, ...sample: [unknown, ...unknown[]]) {
super(sample, inverse)
}
asymmetricMatch(other: unknown) {
const { pass } = expectAssertion.call(
this.getMatcherContext(expect),
other,
...this.sample,
) as SyncExpectationResult
return this.inverse ? !pass : pass
}
toString() {
return `${this.inverse ? 'not.' : ''}${expectAssertionName}`
}
getExpectedType() {
return 'any'
}
toAsymmetricMatcher() {
return `${this.toString()}<${this.sample.map(String).join(', ')}>`
}
}
Object.defineProperty(expect, expectAssertionName, {
configurable: true,
enumerable: true,
value: (...sample: [unknown, ...unknown[]]) => new CustomMatcher(false, ...sample),
writable: true,
})
Object.defineProperty(expect.not, expectAssertionName, {
configurable: true,
enumerable: true,
value: (...sample: [unknown, ...unknown[]]) => new CustomMatcher(true, ...sample),
writable: true,
})
})
}
}
export const JestExtend: ChaiPlugin = (chai, utils) => {
utils.addMethod(chai.expect, 'extend', (expect: Vi.ExpectStatic, expects: MatchersObject) => {
chai.use(JestExtendPlugin(expect, expects))
})
}