Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import type { ExpectStatic, MatcherState, Tester } from './types'import { ASYMMETRIC_MATCHERS_OBJECT, GLOBAL_EXPECT, JEST_MATCHERS_OBJECT, MATCHERS_OBJECT,} from './constants'
if (!Object.hasOwn(globalThis, MATCHERS_OBJECT)) { const globalState = new WeakMap<ExpectStatic, MatcherState>() const matchers = Object.create(null) const customEqualityTesters: Array<Tester> = [] const asymmetricMatchers = Object.create(null) // `configurable` so that vm pools can strip the accessors from a disposed // context: the getters capture the expect state, which would otherwise keep // the whole test-file world reachable from the leaked context shell Object.defineProperty(globalThis, MATCHERS_OBJECT, { configurable: true, get: () => globalState, }) Object.defineProperty(globalThis, JEST_MATCHERS_OBJECT, { configurable: true, get: () => ({ state: globalState.get((globalThis as any)[GLOBAL_EXPECT]), matchers, customEqualityTesters, }), }) Object.defineProperty(globalThis, ASYMMETRIC_MATCHERS_OBJECT, { configurable: true, get: () => asymmetricMatchers, })}
export function getState<State extends MatcherState = MatcherState>(expect: ExpectStatic): State { return (globalThis as any)[MATCHERS_OBJECT].get(expect)}
export function setState<State extends MatcherState = MatcherState>( state: Partial<State>, expect: ExpectStatic,): void { const map = (globalThis as any)[MATCHERS_OBJECT] const current = map.get(expect) // so it keeps getters from `testPath` const next = Object.defineProperties(current || {}, Object.getOwnPropertyDescriptors(state)) if (!current) { map.set(expect, next) }}