-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.ts
More file actions
91 lines (81 loc) · 2.06 KB
/
jest.setup.ts
File metadata and controls
91 lines (81 loc) · 2.06 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import "@testing-library/jest-dom";
import type { ReactNode } from "react";
import enMessages from "./messages/en.json";
type TranslationValues = Record<string, string | number>;
const getMessage = (path: string) => {
const result = path
.split(".")
.reduce<unknown>(
(accumulator, segment) =>
accumulator && typeof accumulator === "object" && segment in accumulator
? (accumulator as Record<string, unknown>)[segment]
: undefined,
enMessages,
);
return typeof result === "string" ? result : path;
};
const formatMessage = (message: string, values?: TranslationValues) => {
if (!values) {
return message;
}
return Object.entries(values).reduce(
(result, [key, value]) => result.replace(`{${key}}`, String(value)),
message,
);
};
jest.mock("next-intl", () => ({
NextIntlClientProvider: ({ children }: { children: ReactNode }) => children,
useLocale: () => "en",
useMessages: () => enMessages,
useTranslations:
(namespace?: string) => (key: string, values?: TranslationValues) =>
formatMessage(
getMessage(namespace ? `${namespace}.${key}` : key),
values,
),
}));
if (typeof globalThis.TransformStream === "undefined") {
Object.defineProperty(globalThis, "TransformStream", {
value: class TransformStream {
readable: unknown;
writable: unknown;
constructor() {
this.readable = {};
this.writable = {};
}
},
writable: true,
});
}
if (typeof globalThis.ReadableStream === "undefined") {
Object.defineProperty(globalThis, "ReadableStream", {
value: class ReadableStream {
getReader() {
return this;
}
},
writable: true,
});
}
if (typeof globalThis.WritableStream === "undefined") {
Object.defineProperty(globalThis, "WritableStream", {
value: class WritableStream {
getWriter() {
return this;
}
},
writable: true,
});
}
if (typeof globalThis.fetch === "undefined") {
Object.defineProperty(globalThis, "fetch", {
value: jest.fn(async () => ({
ok: true,
status: 200,
headers: new Headers(),
json: async () => ({}),
text: async () => "",
})),
writable: true,
});
}