-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.test.js
180 lines (144 loc) · 4.06 KB
/
app.test.js
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
const { getQueriesForElement } = require("@testing-library/dom");
const { JSDOM, ResourceLoader } = require("jsdom");
const { build } = require("esbuild");
const path = require("path");
async function bundle() {
const out = await build({
logLevel: "error",
entryPoints: [path.resolve(__dirname, "../src/app.jsx")],
bundle: true,
write: false,
target: "es2019",
define: { "process.env.NODE_ENV": '"development"', global: "window" },
outdir: "./",
});
return out;
}
const buildResultPromise = bundle();
/**
* JSDOM ResourceLoader for resolving requested resources against our bundle
*/
class BundledResourceLoader extends ResourceLoader {
async fetch(url, options) {
const { outputFiles } = await buildResultPromise;
const byUrl = outputFiles.reduce((map, file) => {
if (file.path.match(/\.js\.map/)) {
return map;
}
map.set(`https://test.app${file.path}`, file.contents);
return map;
}, new Map());
const match = byUrl.get(url);
if (match) {
return Promise.resolve(Buffer.from(match));
}
return super.fetch(url, options);
}
}
/**
* Create a JSDOM instance of a basic HTML page with our bundled app
* loaded on the page.
*/
async function constructDOM(url) {
const { outputFiles } = await buildResultPromise;
const dom = new JSDOM(
`<!DOCTYPE html><body><div id="root"></div></body></html>`,
{
resources: new BundledResourceLoader(),
runScripts: "dangerously",
url,
}
);
const { window } = dom;
/**
* Inject JS files from the bundle into the DOM.
*/
for (let out of outputFiles.filter((f) => f.path.match(/\.js$/))) {
const scriptEl = window.document.createElement("script");
scriptEl.src = out.path;
window.document.body.appendChild(scriptEl);
}
return window;
}
const toMB = (bytes) => Math.floor(bytes / 1024 / 1024);
/**
* Create a JSDOM instance for a given URL
*/
async function createBrowserRuntime(config) {
const { url } = config;
const window = await constructDOM(url);
return window;
}
/**
* Rudimentary test API. Creates a JSDOM instance and provides it to
* the given implementation callback. Should dispose of the JSDOM instance after
* the test is complete (or fails).
*/
function test(title, implementation) {
let window;
return new Promise(async (resolve, reject) => {
try {
await implementation(async (config) => {
window = await createBrowserRuntime(config);
return window;
});
if (window) {
window.close();
window = null;
}
resolve();
} catch (e) {
if (window) {
window.close();
window = null;
}
reject(e);
console.log(`Failed: ${e}`);
} finally {
global.gc();
console.log(
`Finished ${title}: ${toMB(process.memoryUsage().heapUsed)} MB`
);
}
});
}
async function run() {
await test("1", async (browser) => {
const window = await browser({
url: "https://test.app",
});
const screen = getQueriesForElement(window.document.body);
await screen.findByText(/Paragraph 1 Heading/);
});
await test("2", async (browser) => {
const window = await browser({
url: "https://test.app",
});
const screen = getQueriesForElement(window.document.body);
await screen.findByText(/Paragraph 1 Heading/);
});
await test("3", async (browser) => {
const window = await browser({
url: "https://test.app",
});
const screen = getQueriesForElement(window.document.body);
await screen.findByText(/Paragraph 1 Heading/);
});
await test("4", async (browser) => {
const window = await browser({
url: "https://test.app",
});
const screen = getQueriesForElement(window.document.body);
await screen.findByText(/Paragraph 1 Heading/);
});
await test("5", async (browser) => {
const window = await browser({
url: "https://test.app",
});
const screen = getQueriesForElement(window.document.body);
await screen.findByText(/Paragraph 1 Heading/);
});
}
(() => {
run();
})();