/
types.ts
184 lines (162 loc) · 5.9 KB
/
types.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import type { use as chaiUse } from 'chai'
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import type { Formatter } from 'picocolors/types'
import type { Constructable } from '@vitest/utils'
import type { diff, getMatcherUtils, stringify } from './jest-matcher-utils'
export type FirstFunctionArgument<T> = T extends (arg: infer A) => unknown ? A : never
export type ChaiPlugin = FirstFunctionArgument<typeof chaiUse>
export type Tester = (a: any, b: any) => boolean | undefined
export type { DiffOptions } from '@vitest/utils/diff'
export interface MatcherHintOptions {
comment?: string
expectedColor?: Formatter
isDirectExpectCall?: boolean
isNot?: boolean
promise?: string
receivedColor?: Formatter
secondArgument?: string
secondArgumentColor?: Formatter
}
export interface MatcherState {
assertionCalls: number
currentTestName?: string
dontThrow?: () => void
error?: Error
equals: (
a: unknown,
b: unknown,
customTesters?: Array<Tester>,
strictCheck?: boolean,
) => boolean
expand?: boolean
expectedAssertionsNumber?: number | null
expectedAssertionsNumberErrorGen?: (() => Error) | null
isExpectingAssertions?: boolean
isExpectingAssertionsError?: Error | null
isNot: boolean
// environment: VitestEnvironment
promise: string
// snapshotState: SnapshotState
suppressedErrors: Array<Error>
testPath?: string
utils: ReturnType<typeof getMatcherUtils> & {
diff: typeof diff
stringify: typeof stringify
iterableEquality: Tester
subsetEquality: Tester
}
soft?: boolean
}
export interface SyncExpectationResult {
pass: boolean
message: () => string
actual?: any
expected?: any
}
export type AsyncExpectationResult = Promise<SyncExpectationResult>
export type ExpectationResult = SyncExpectationResult | AsyncExpectationResult
export interface RawMatcherFn<T extends MatcherState = MatcherState> {
(this: T, received: any, expected: any, options?: any): ExpectationResult
}
export type MatchersObject<T extends MatcherState = MatcherState> = Record<string, RawMatcherFn<T>>
export interface ExpectStatic extends Chai.ExpectStatic, AsymmetricMatchersContaining {
<T>(actual: T, message?: string): Assertion<T>
unreachable(message?: string): never
soft<T>(actual: T, message?: string): Assertion<T>
extend(expects: MatchersObject): void
assertions(expected: number): void
hasAssertions(): void
anything(): any
any(constructor: unknown): any
getState(): MatcherState
setState(state: Partial<MatcherState>): void
not: AsymmetricMatchersContaining
}
export interface AsymmetricMatchersContaining {
stringContaining(expected: string): any
objectContaining<T = any>(expected: T): any
arrayContaining<T = unknown>(expected: Array<T>): any
stringMatching(expected: string | RegExp): any
}
export interface JestAssertion<T = any> extends jest.Matchers<void, T> {
// 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
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
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
}
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)
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]
}
export interface Assertion<T = any> extends VitestAssertion<Chai.Assertion, T>, JestAssertion<T> {
toBeTypeOf(expected: 'bigint' | 'boolean' | 'function' | 'number' | 'object' | 'string' | 'symbol' | 'undefined'): void
toHaveBeenCalledOnce(): void
toSatisfy<E>(matcher: (value: E) => boolean, message?: string): void
resolves: Promisify<Assertion<T>>
rejects: Promisify<Assertion<T>>
}
declare global {
// support augmenting jest.Matchers by other libraries
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace jest {
// eslint-disable-next-line unused-imports/no-unused-vars
interface Matchers<R, T = {}> {}
}
}
export {}