/
global.ts
144 lines (130 loc) · 5.4 KB
/
global.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
134
135
136
137
138
139
140
141
142
143
144
import type { Plugin as PrettyFormatPlugin } from 'pretty-format'
import type { MatchersObject } from '@vitest/expect'
import type SnapshotState from '../integrations/snapshot/port/state'
import type { MatcherState } from './chai'
import type { Constructable, UserConsoleLog } from './general'
import type { VitestEnvironment } from './config'
import type { BenchmarkResult } from './benchmark'
type Promisify<O> = {
[K in keyof O]: O[K] extends (...args: infer A) => infer R
? O extends R
? Promisify<O[K]>
: (...args: A) => Promise<R>
: O[K]
}
declare module '@vitest/expect' {
interface MatcherState {
environment: VitestEnvironment
snapshotState: SnapshotState
}
}
declare module '@vitest/runner' {
interface TestContext {
expect: Vi.ExpectStatic
}
interface TaskBase {
logs?: UserConsoleLog[]
}
interface TaskResult {
benchmark?: BenchmarkResult
}
}
declare global {
// support augmenting jest.Matchers by other libraries
namespace jest {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<R, T = {}> {}
}
namespace Vi {
interface ExpectStatic extends Chai.ExpectStatic, AsymmetricMatchersContaining {
<T>(actual: T, message?: string): Vi.Assertion<T>
extend(expects: MatchersObject): void
assertions(expected: number): void
hasAssertions(): void
anything(): any
any(constructor: unknown): any
addSnapshotSerializer(plugin: PrettyFormatPlugin): void
getState(): MatcherState
setState(state: Partial<MatcherState>): void
not: AsymmetricMatchersContaining
}
interface AsymmetricMatchersContaining {
stringContaining(expected: string): any
objectContaining<T = any>(expected: T): any
arrayContaining<T = unknown>(expected: Array<T>): any
stringMatching(expected: string | RegExp): any
}
interface JestAssertion<T = any> extends jest.Matchers<void, T> {
// Snapshot
toMatchSnapshot<U extends { [P in keyof T]: any }>(snapshot: Partial<U>, message?: string): void
toMatchSnapshot(message?: string): void
matchSnapshot<U extends { [P in keyof T]: any }>(snapshot: Partial<U>, message?: string): void
matchSnapshot(message?: string): void
toMatchInlineSnapshot<U extends { [P in keyof T]: any }>(properties: Partial<U>, snapshot?: string, message?: string): void
toMatchInlineSnapshot(snapshot?: string, message?: string): void
toThrowErrorMatchingSnapshot(message?: string): void
toThrowErrorMatchingInlineSnapshot(snapshot?: string, message?: string): void
// Jest compact
toEqual<E>(expected: E): void
toStrictEqual<E>(expected: E): void
toBe<E>(expected: E): void
toMatch(expected: string | RegExp): void
toMatchObject<E extends {} | any[]>(expected: E): void
toContain<E>(item: E): void
toContainEqual<E>(item: E): void
toBeTruthy(): void
toBeFalsy(): void
toBeGreaterThan(num: number | bigint): void
toBeGreaterThanOrEqual(num: number | bigint): void
toBeLessThan(num: number | bigint): void
toBeLessThanOrEqual(num: number | bigint): void
toBeNaN(): void
toBeUndefined(): void
toBeNull(): void
toBeDefined(): void
toBeTypeOf(expected: 'bigint' | 'boolean' | 'function' | 'number' | 'object' | 'string' | 'symbol' | 'undefined'): void
toBeInstanceOf<E>(expected: E): void
toBeCalledTimes(times: number): void
toHaveLength(length: number): void
toHaveProperty<E>(property: string | (string | number)[], value?: E): void
toBeCloseTo(number: number, numDigits?: number): void
toHaveBeenCalledTimes(times: number): void
toHaveBeenCalledOnce(): void
toHaveBeenCalled(): void
toBeCalled(): void
toHaveBeenCalledWith<E extends any[]>(...args: E): void
toBeCalledWith<E extends any[]>(...args: E): void
toHaveBeenNthCalledWith<E extends any[]>(n: number, ...args: E): void
nthCalledWith<E extends any[]>(nthCall: number, ...args: E): void
toHaveBeenLastCalledWith<E extends any[]>(...args: E): void
lastCalledWith<E extends any[]>(...args: E): void
toThrow(expected?: string | Constructable | RegExp | Error): void
toThrowError(expected?: string | Constructable | RegExp | Error): void
toReturn(): void
toHaveReturned(): void
toReturnTimes(times: number): void
toHaveReturnedTimes(times: number): void
toReturnWith<E>(value: E): void
toHaveReturnedWith<E>(value: E): void
toHaveLastReturnedWith<E>(value: E): void
lastReturnedWith<E>(value: E): void
toHaveNthReturnedWith<E>(nthCall: number, value: E): void
nthReturnedWith<E>(nthCall: number, value: E): void
toSatisfy<E>(matcher: (value: E) => boolean, message?: string): void
}
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
// @ts-ignore build namespace conflict
type VitestAssertion<A, T> = {
[K in keyof A]: A[K] extends Chai.Assertion
? Assertion<T>
: A[K] extends (...args: any[]) => any
? A[K] // not converting function since they may contain overload
: VitestAssertion<A[K], T>
} & ((type: string, message?: string) => Assertion)
interface Assertion<T = any> extends VitestAssertion<Chai.Assertion, T>, JestAssertion<T> {
resolves: Promisify<Assertion<T>>
rejects: Promisify<Assertion<T>>
}
}
}
export {}