-
-
Notifications
You must be signed in to change notification settings - Fork 376
/
build.test.js
236 lines (190 loc) · 6.9 KB
/
build.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
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
const { join } = require('path');
const { access, readdir } = require('fs').promises;
const { readFile } = require('fs').promises;
const looksLike = require('html-looks-like');
const { create, build } = require('./lib/cli');
const { snapshot } = require('./lib/utils');
const { subject } = require('./lib/output');
const images = require('./images/build');
const minimatch = require('minimatch');
const prerenderUrlFiles = [
'prerender-urls.json',
'prerender-urls.js',
'prerender-urls.promise.js',
];
async function getBody(dir, file = 'index.html') {
file = join(dir, `build/${file}`);
let html = await readFile(file, 'utf-8');
return html.match(/<body>.*<\/body>/)[0];
}
async function getHead(dir, file = 'index.html') {
file = join(dir, `build/${file}`);
let html = await readFile(file, 'utf-8');
return html.match(/<head>.*<\/head>/)[0];
}
function getRegExpFromMarkup(markup) {
const minifiedMarkup = markup
.replace(/\n/g, '')
.replace(/\t/g, '')
.replace(/\s{2}/g, '');
return new RegExp(minifiedMarkup);
}
function testMatch(received, expected) {
let receivedKeys = Object.keys(received);
let expectedKeys = Object.keys(expected);
expect(receivedKeys).toHaveLength(expectedKeys.length);
for (let key in expected) {
const receivedKey = receivedKeys.find(k => minimatch(k, key));
expect(key).toFindMatchingKey(receivedKey);
expect(receivedKey).toBeCloseInSize(received[receivedKey], expected[key]);
}
}
describe('preact build', () => {
it(`builds the 'default' template`, async () => {
let dir = await create('default');
await build(dir);
dir = join(dir, 'build');
let output = await snapshot(dir);
testMatch(output, images.default);
});
it(`builds the 'default' template with esm`, async () => {
let dir = await create('default');
await build(dir, { esm: true });
dir = join(dir, 'build');
let output = await snapshot(dir);
testMatch(output, images['default-esm']);
});
it('should use SASS styles', async () => {
let dir = await subject('sass');
await build(dir);
let body = await getBody(dir);
looksLike(body, images.sass);
});
it('should use custom `.babelrc`', async () => {
// app with custom .babelrc enabling async functions
let dir = await subject('custom-babelrc');
await build(dir);
const bundleFile = (await readdir(`${dir}/build`)).find(file =>
/bundle\.\w{5}\.js$/.test(file)
);
const transpiledChunk = await readFile(
`${dir}/build/${bundleFile}`,
'utf8'
);
// when tragetting only last 1 chrome version, babel preserves
// arrow function. So checking for the delay function code from delay function in
// https://github.com/preactjs/preact-cli/blob/master/packages/cli/tests/subjects/custom-babelrc/index.js
expect(transpiledChunk.includes('=>setTimeout')).toBe(true);
});
prerenderUrlFiles.forEach(prerenderUrls => {
it(`should prerender the routes provided with '${prerenderUrls}'`, async () => {
let dir = await subject('multiple-prerendering');
await build(dir, { prerenderUrls });
const body1 = await getBody(dir);
looksLike(body1, images.prerender.home);
const body2 = await getBody(dir, 'route66/index.html');
looksLike(body2, images.prerender.route);
const body3 = await getBody(dir, 'custom/index.html');
looksLike(body3, images.prerender.custom);
const head1 = await getHead(dir);
expect(head1).toEqual(
expect.stringMatching(getRegExpFromMarkup(images.prerender.heads.home))
);
const head2 = await getHead(dir, 'route66/index.html');
expect(head2).toEqual(
expect.stringMatching(
getRegExpFromMarkup(images.prerender.heads.route66)
)
);
const head3 = await getHead(dir, 'custom/index.html');
expect(head3).toEqual(
expect.stringMatching(
getRegExpFromMarkup(images.prerender.heads.custom)
)
);
});
});
prerenderUrlFiles.forEach(prerenderUrls => {
it(`should prerender the routes with data provided with '${prerenderUrls}' via provider`, async () => {
let dir = await subject('multiple-prerendering-with-provider');
await build(dir, { prerenderUrls });
const body1 = await getBody(dir);
looksLike(body1, images.prerender.home);
const body2 = await getBody(dir, 'route66/index.html');
looksLike(body2, images.prerender.route);
const body3 = await getBody(dir, 'custom/index.html');
looksLike(body3, images.prerender.custom);
const body4 = await getBody(dir, 'customhook/index.html');
looksLike(body4, images.prerender.customhook);
const body5 = await getBody(dir, 'htmlsafe/index.html');
looksLike(body5, images.prerender.htmlSafe);
const head1 = await getHead(dir);
expect(head1).toEqual(
expect.stringMatching(getRegExpFromMarkup(images.prerender.heads.home))
);
const head2 = await getHead(dir, 'route66/index.html');
expect(head2).toEqual(
expect.stringMatching(
getRegExpFromMarkup(images.prerender.heads.route66)
)
);
const head3 = await getHead(dir, 'custom/index.html');
expect(head3).toEqual(
expect.stringMatching(
getRegExpFromMarkup(images.prerender.heads.custom)
)
);
});
});
it('should preload correct files', async () => {
let dir = await subject('preload-chunks');
await build(dir, { preload: true });
const head1 = await getHead(dir);
expect(head1).toEqual(
expect.stringMatching(getRegExpFromMarkup(images.preload.head))
);
});
it('should use custom `preact.config.js`', async () => {
// app with stable output name via preact.config.js
let dir = await subject('custom-webpack');
await build(dir);
let stableOutput = join(dir, 'build/bundle.js');
expect(await access(stableOutput)).toBeUndefined();
});
it('should use custom `template.html`', async () => {
let dir = await subject('custom-template');
await build(dir);
let file = join(dir, 'build/index.html');
let html = await readFile(file, 'utf-8');
expect(html).toEqual(
expect.stringMatching(getRegExpFromMarkup(images.template))
);
});
it('should patch global location object', async () => {
let dir = await subject('location-patch');
expect(() => build(dir)).not.toThrow();
});
it('should import non-modules CSS even when side effects are false', async () => {
let dir = await subject('side-effect-css');
await build(dir);
let head = await getHead(dir);
expect(head).toEqual(
expect.stringMatching(getRegExpFromMarkup(images.sideEffectCss))
);
});
it('should copy resources from static to build directory', async () => {
let dir = await subject('static-root');
await build(dir);
let file = join(dir, 'build', '.htaccess');
expect(await access(file)).toBeUndefined();
});
it('should error out for invalid CLI argument', async () => {
let dir = await subject('custom-template');
const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {});
await expect(build(dir, { 'service-worker': false })).rejects.toEqual(
new Error('Invalid argument found.')
);
expect(mockExit).toHaveBeenCalledWith(1);
mockExit.mockRestore();
});
});