/
state.ts
30 lines (27 loc) · 1.01 KB
/
state.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
import type { MatcherState } from './types'
import { GLOBAL_EXPECT, JEST_MATCHERS_OBJECT, MATCHERS_OBJECT } from './constants'
if (!Object.prototype.hasOwnProperty.call(globalThis, MATCHERS_OBJECT)) {
const globalState = new WeakMap<Vi.ExpectStatic, MatcherState>()
const matchers = Object.create(null)
Object.defineProperty(globalThis, MATCHERS_OBJECT, {
get: () => globalState,
})
Object.defineProperty(globalThis, JEST_MATCHERS_OBJECT, {
configurable: true,
get: () => ({
state: globalState.get((globalThis as any)[GLOBAL_EXPECT]),
matchers,
}),
})
}
export const getState = <State extends MatcherState = MatcherState>(expect: Vi.ExpectStatic): State =>
(globalThis as any)[MATCHERS_OBJECT].get(expect)
export const setState = <State extends MatcherState = MatcherState>(
state: Partial<State>,
expect: Vi.ExpectStatic,
): void => {
const map = (globalThis as any)[MATCHERS_OBJECT]
const current = map.get(expect) || {}
Object.assign(current, state)
map.set(expect, current)
}