-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
311 lines (247 loc) · 7.92 KB
/
index.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// https://github.com/facebook/jest/issues/7832#issuecomment-462343138
import { DeepPartial } from 'ts-essentials';
export * from './jestExtensions';
export * from './utils';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type GenericFunction = (...args: any[]) => any;
type PickByTypeKeyFilter<T, C> = { [K in keyof T]: T[K] extends C ? K : never };
type KeysByType<T, C> = PickByTypeKeyFilter<T, C>[keyof T];
type ValuesByType<T, C> = { [K in keyof T]: T[K] extends C ? T[K] : never };
type PickByType<T, C> = Pick<ValuesByType<T, C>, KeysByType<T, C>>;
export type MethodsOf<T> = KeysByType<Required<T>, GenericFunction>;
type InterfaceOf<T> = PickByType<T, GenericFunction>;
type PartiallyMockedInterfaceOf<T> = { [K in MethodsOf<T>]?: jest.Mock<InterfaceOf<T>[K]> };
/**
* A smart mock for jest that automatically mocks all methods
*/
export function mock<T>(): jest.Mocked<T> {
return autoMockProxy<T>();
}
/**
* Automatically create a jest mock for all methods. If a default is provided, resolve the value for every method of T unless otherwise specified
* @param defaultResponse
*/
export function autoMockProxy<T>(defaultResponse?: unknown): jest.Mocked<T> {
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const obj = {} as any;
// @ts-ignore
return new Proxy<T>(obj, {
get(target, propKey: keyof T) {
if (obj[propKey]) {
return obj[propKey];
}
// seems that somehow a .then is being applied to async proxies so ignore that otherwise we create a
// a proxy of a promise we didnt mean to make
if (propKey === 'then') {
return;
}
if (defaultResponse) {
// @ts-ignore
obj[propKey] = jest.fn().mockReset().mockResolvedValue(defaultResponse);
} else {
// @ts-ignore
obj[propKey] = jest.fn().mockReset();
}
return obj[propKey];
},
});
}
export function mockMethod<T>(root: T, mockedMethod: MethodsOf<T>, resetIfExists = true): jest.Mocked<T> {
const partiallyMocked: PartiallyMockedInterfaceOf<T> = root as PartiallyMockedInterfaceOf<T>;
// set the mock method if its not already set as a mock
if ((partiallyMocked[mockedMethod] as unknown as jest.Mock)?.mock === undefined) {
// eslint-disable-next-line no-return-assign
partiallyMocked[mockedMethod] = jest.fn().mockReset();
} else if (resetIfExists) {
partiallyMocked[mockedMethod]?.mockReset();
}
return partiallyMocked as jest.Mocked<T>;
}
export function asMocked<T>(item: T): jest.Mocked<T> {
return item as jest.Mocked<T>;
}
/**
* Maps a type to its call arguments if the type is a function or a jest function spy, otherwise never
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type FunctionParams<T> = T extends (...args: any) => any
? Parameters<T> // eslint-disable-next-line @typescript-eslint/no-explicit-any
: T extends jest.SpyInstance<any, infer Y>
? Y
: never;
type ArrayElement<T> = T extends Array<infer Y> ? Y : never;
/**
* Maps a type to its unpacked form if it's an array, otherwise maps the type to itself
*/
type SafeUnpackedPotentialArray<T> = T extends Array<infer Y> ? Y : T;
/**
* Wrap the source in a generic safe form for jest expect. This ensures that matchers
* all properly type check against the input type
* @param item
*/
export function safeExpect<T>(item: T) {
return new SafeExpector(item);
}
/**
* A method that can help type object containing matchers when using this against
* hasCalled function parameters
* @param contains
*/
export function safeObjectContaining<T, Y extends T>(contains: Partial<Y>): T {
return expect.objectContaining(contains);
}
/**
* Safely cast a deep partial item as the actual item. This is useful in tests
* when you can get away without constructing the full object but want to avoid an explicit
* "as" cast which will invalidate any compile time checking.
* @param p
*/
export function safePartial<T>(p: DeepPartial<T>): T {
return p as T;
}
/**
* Allows you to do a deep partial match for expectations
* @param contains
*/
export function deepSafeObjectContaining<T, Y extends T>(contains: DeepPartial<Y>): T {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const process = (data: any) => {
// @ts-ignore
const t: T = {};
Object.keys(data).forEach(k => {
// @ts-ignore
const value = data[k];
// @ts-ignore
t[k] = Array.isArray(value) || typeof value === 'object' ? safeObjectContaining(process(value)) : value;
});
return t;
};
return safeObjectContaining(process(contains));
}
class SafeExpector<T> {
private negate: boolean;
constructor(private source: T) {
this.negate = false;
}
get not(): SafeExpector<T> {
this.negate = !this.negate;
return this;
}
get rejects() {
return expect(this.source).rejects;
}
/**
* We don't want to encourage `.resolves` behavior because it doesn't buy us anything.
* We might as well just `await thing` rather than `await safeExpect(thing).resolves` because the test
* will fail in either case and `await thing` not throwing means a pass anyway in this case.
*/
get resolves() {
return expect(this.source).resolves;
}
/**
* It can be useful to want to conditionally negate an expectation. This allows us to chain
* a programmatic expectation negation:
*
* i.e.
*
* expect(foo).not.toBeTruthy() === expect(foo).negated(true).toBeTruthy()
*
* and
*
* expect(foo).toBeTruthy() === expect(foo).negated(false).toBeTruthy()
*
* @param negate Whether or not to negate the previous part of the expectation chain
*/
negated(negate: boolean) {
if (negate) {
return this.not;
}
return this;
}
toBeDefined() {
this.expect().toBeDefined();
}
toBeFalsy() {
this.expect().toBeFalsy();
}
toBeGreaterThan(t: number) {
this.expect().toBeGreaterThan(t);
}
toBeGreaterThanOrEqual(t: number) {
this.expect().toBeGreaterThanOrEqual(t);
}
toBeLessThan(t: number) {
this.expect().toBeLessThan(t);
}
toBeLessThanOrEqual(t: number) {
this.expect().toBeLessThanOrEqual(t);
}
toBeTruthy() {
this.expect().toBeTruthy();
}
toBeUndefined() {
this.expect().toBeUndefined();
}
toBeNull() {
this.expect().toBeNull();
}
toBeWithinRange(floor: number, ceil: number) {
this.expect().toBeWithinRange(floor, ceil);
}
toContain(t: SafeUnpackedPotentialArray<T>) {
this.expect().toContain(t);
}
toContainEqual(elem: ArrayElement<T>) {
this.expect().toContainEqual(elem);
}
toEqual(t: T) {
this.expect().toEqual(t);
}
toHaveBeenCalled() {
this.expect().toHaveBeenCalled();
}
toHaveBeenCalledTimes(times: number) {
this.expect().toHaveBeenCalledTimes(times);
}
toHaveBeenCalledWith(...args: FunctionParams<T>) {
this.expect().toHaveBeenCalledWith(...args);
}
toHaveBeenNthCalledWith(n: number, ...args: FunctionParams<T>) {
this.expect().toHaveBeenNthCalledWith(n, ...args);
}
toHaveLength(n: number) {
this.expect().toHaveLength(n);
}
toMatchObject(t: Partial<T>) {
this.expect().toMatchObject(t);
}
/**
* Extension to jest comparisons that asserts the shape matches but excludes the typed fields
* @param t
* @param fields
*/
toMatchObjectExcluding(t: Partial<T>, fields: Array<keyof NonNullable<T>>) {
// create copies so we can delete the fields
const toCompare = { ...t };
const original = { ...this.source };
fields.forEach(f => {
delete toCompare[f as keyof T];
delete original[f as keyof T];
});
this.expect(original).toMatchObject(toCompare);
}
toThrow(error?: string | ErrorConstructor | RegExp | Error) {
this.expect().toThrow(error);
}
toMatchSnapshot() {
this.expect().toMatchSnapshot();
}
private expect(s?: T) {
const original = s ?? this.source;
if (this.negate) {
return expect(original).not;
}
return expect(original);
}
}