generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 39
/
TestUtils.ts
90 lines (82 loc) · 2.98 KB
/
TestUtils.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
import { AccessToken } from "@okta/okta-auth-js";
export const mockToken = (mock?: Partial<AccessToken>): AccessToken => {
return {
authorizeUrl: mock?.authorizeUrl || "",
expiresAt: mock?.expiresAt || 0,
scopes: mock?.scopes || [],
userinfoUrl: mock?.userinfoUrl || "",
accessToken: mock?.accessToken || "",
claims: mock?.claims || { sub: "" },
tokenType: mock?.tokenType || "",
};
};
export const mockEvent = (mock?: Partial<any>) => {
return {
response: mock?.response || null,
};
};
export function conditionallySuppressConsole(...matchers: string[]) {
const origConsole = jest.requireActual("console");
const jestError = jest
.spyOn(console, "error")
.mockImplementation((message: any) => {
if (
!matchers.find((matcher) =>
message.toString().includes(matcher)
)
) {
origConsole.error(message);
}
});
const jestWarn = jest
.spyOn(console, "warn")
.mockImplementation((message: any) => {
if (
!matchers.find((matcher) =>
message.toString().includes(matcher)
)
) {
origConsole.warn(message);
}
});
return () => {
jestError.mockRestore();
jestWarn.mockRestore();
};
}
/**
* Future updates to browser Intl will eventually include a change to strings where
* some spaces will be unicode no-break spaces (u+202F / charCode 8239) instead of
* regular spaces (charCode 32). This apparently already happened in the browser
* space but was quickly reverted due to breaking WebCompat. Node 18.14.2
* implements this change which will break tests doing string comparisons of Intl
* output with developer-crafted comparisons. A future update to Node may revert
* this change as well if this was reverted in V8 itself. In either case, this
* helper will be important for the eventual transition period if they persue this
* again. Use this helper function preferably within a mock implementation or
* proxy object handler so the strings are passed on naturally.
*/
export function replaceUnicodeNarrowNoBreakSpaces(str: string) {
return str.replaceAll(String.fromCharCode(8239), String.fromCharCode(32));
}
/**
* Create a proxy of a Node DateTimeFormat object so we can replace unicode no-break
* strings before returning.
*/
export function createProxyNodeDateTimeFormatter(
formatter: Intl.DateTimeFormat
) {
return new Proxy(formatter, {
get(target, p) {
const targets: (string | symbol)[] = [
"format",
"formatRange",
] as (keyof Intl.DateTimeFormat)[];
if (targets.includes(p)) {
return (...args: any[]) =>
replaceUnicodeNarrowNoBreakSpaces(target.format(...args));
}
return (target as any)[p];
},
});
}