Repository navigation
Expand file tree
/
Copy pathsetup-jest.ts
More file actions
72 lines (62 loc) · 1.88 KB
/
Copy pathsetup-jest.ts
File metadata and controls
72 lines (62 loc) · 1.88 KB
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
import { isAxiosError } from "axios";
const wrapFlat =
(original: FlatClbk): FlatClbk =>
(...args) => {
const clbk = args[1];
args[1] = (...args) => {
const res = clbk(args);
if (res instanceof Promise) {
return res.catch((err) => {
// axios errors contains node (request + response)
// each of them is reqursive as hell
// so we need to strip them
if (isAxiosError(err)) {
err.request = null;
err.response = undefined;
throw err;
} else throw err;
});
}
return res;
};
return original(...args);
};
const recursiveKeys = ["only", "failing", "skip", "concurrent"] as const;
const recusiveKeyError = () => {
throw new Error("TODO: support recursive keys");
};
const defineRecursiveError = (value: TestFn) => {
for (const key of recursiveKeys) {
Object.defineProperty(value, key, {
get: recusiveKeyError,
});
}
};
type FlatClbk = {
(
name: unknown,
clbk: (...args: unknown[]) => unknown,
...args: unknown[]
): unknown;
};
type TestFn = FlatClbk & {
skip: TestFn;
only: TestFn;
failing: TestFn;
concurrent: TestFn;
};
const wrap = (original: TestFn): TestFn => {
const modified = wrapFlat(original) as TestFn;
Object.assign(modified, original);
for (const key of recursiveKeys) {
if (key in modified) {
const next = wrapFlat(modified[key]) as TestFn;
Object.assign(next, modified);
defineRecursiveError(next);
modified[key] = next;
}
}
return modified;
};
(globalThis.test as TestFn) = wrap(test as TestFn);
(globalThis.it as TestFn) = wrap(it as TestFn);