forked from preactjs/preact-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.test.js
514 lines (420 loc) · 15.3 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
const { join } = require('path');
const { access, mkdir, readdir, readFile, rename, unlink, writeFile } =
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 shell = require('shelljs');
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]);
}
}
/**
* Get build output file as utf-8 string
* @param {string} dir
* @param {RegExp | string} file
* @returns {Promise<string>}
*/
async function getOutputFile(dir, file) {
if (typeof file !== 'string') {
// @ts-ignore
file = (await readdir(join(dir, 'build'))).find(f => file.test(f));
}
return await readFile(join(dir, 'build', file), 'utf8');
}
describe('preact build', () => {
it('builds the `default` template', async () => {
let dir = await create('default');
await build(dir);
let output = await snapshot(join(dir, 'build'));
testMatch(output, images.default);
});
it('builds the `default` template with esm', async () => {
let dir = await create('default');
await build(dir, { esm: true });
let output = await snapshot(join(dir, 'build'));
testMatch(output, images['default-esm']);
});
it('builds the `typescript` template', async () => {
let dir = await create('typescript');
// The tsconfig.json in the template covers the test directory,
// so TS will error out if it can't find even test-only module definitions
shell.cd(dir);
//shell.exec('npm i @types/enzyme@3.10.11 enzyme-adapter-preact-pure');
// Remove when https://github.com/preactjs/enzyme-adapter-preact-pure/issues/161 is resolved
shell.exec('rm tsconfig.json');
await expect(build(dir)).resolves.not.toThrow();
});
it('should patch global location object', async () => {
let dir = await subject('location-patch');
await expect(build(dir)).resolves.not.toThrow();
});
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();
});
describe('Push manifest plugin', () => {
it('should produce correct default `push-manifest.json`', async () => {
let dir = await create('default');
await build(dir);
const manifest = await readFile(
`${dir}/build/push-manifest.json`,
'utf8'
);
expect(manifest).toEqual(
expect.stringMatching(getRegExpFromMarkup(images.pushManifest))
);
});
it('should produce correct default `push-manifest.json` with esm', async () => {
let dir = await create('default');
await build(dir, { esm: true });
const manifest = await readFile(
`${dir}/build/push-manifest.json`,
'utf8'
);
expect(manifest).toEqual(
expect.stringMatching(getRegExpFromMarkup(images.pushManifestEsm))
);
});
it('should produce correct `push-manifest.json` when expected values are missing', async () => {
// In this subject, there is no source CSS which means no CSS asset is output.
// In the past, this would result in `"undefined": { type: "style" ... }` being added to the manifest.
let dir = await subject('custom-webpack');
await build(dir);
const manifest = await readFile(
`${dir}/build/push-manifest.json`,
'utf8'
);
expect(manifest).not.toMatch(/"undefined"/);
});
// Issue #1675
it('should produce correct `push-manifest.json` when user configures output filenames', async () => {
let dir = await subject('custom-webpack');
const config = await readFile(`${dir}/preact.config.js`, 'utf8');
await writeFile(
`${dir}/preact.config.js`,
config.replace(
"config.output.filename = '[name].js'",
"config.output.filename = 'scripts/[name].js'"
)
);
await build(dir, { prerender: false });
const manifest = await readFile(
`${dir}/build/push-manifest.json`,
'utf8'
);
expect(manifest).toEqual(
expect.stringMatching(
getRegExpFromMarkup(images.pushManifestAlteredFilenames)
)
);
});
});
it('should use a custom `.env` with prefixed environment variables', async () => {
let dir = await subject('custom-dotenv');
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'
);
// "Hello World!" should replace 'process.env.PREACT_APP_MY_VARIABLE'
expect(transpiledChunk.includes('Hello World!')).toBe(true);
expect(transpiledChunk.includes('PREACT_APP_MY_VARIABLE')).toBe(false);
});
it('should respect `publicPath` value', async () => {
let dir = await subject('public-path');
await build(dir);
const html = await getOutputFile(dir, 'index.html');
expect(html).toEqual(
expect.stringMatching(getRegExpFromMarkup(images.publicPath))
);
});
describe('CLI Options', () => {
it('--src', async () => {
let dir = await subject('minimal');
await mkdir(join(dir, 'renamed-src'));
await rename(join(dir, 'index.js'), join(dir, 'renamed-src/index.js'));
await rename(join(dir, 'style.css'), join(dir, 'renamed-src/style.css'));
await expect(build(dir, { src: 'renamed-src' })).resolves.toBeUndefined();
});
it('--dest', async () => {
let dir = await subject('minimal');
await build(dir, { dest: 'renamed-dest' });
expect(await access(join(dir, 'renamed-dest'))).toBeUndefined();
});
it('--sw', async () => {
let dir = await subject('minimal');
const logSpy = jest.spyOn(process.stdout, 'write');
await build(dir, { sw: true });
expect(await access(join(dir, 'build', 'sw.js'))).toBeUndefined();
expect(logSpy).toHaveBeenCalledWith(
expect.stringContaining('Could not find sw.js')
);
await build(dir, { sw: false });
await expect(access(join(dir, 'build', 'sw.js'))).rejects.toThrow(
'no such file or directory'
);
});
it('--babelConfig', async () => {
let dir = await subject('custom-babelrc');
await build(dir);
let transpiledChunk = await getOutputFile(dir, /bundle\.\w{5}\.js$/);
expect(/=>\s?setTimeout/.test(transpiledChunk)).toBe(true);
await rename(join(dir, '.babelrc'), join(dir, 'babel.config.json'));
await build(dir, {
babelConfig: 'babel.config.json',
});
transpiledChunk = await getOutputFile(dir, /bundle\.\w{5}\.js$/);
expect(/=>\s?setTimeout/.test(transpiledChunk)).toBe(true);
});
it('--json', async () => {
let dir = await subject('minimal');
await build(dir, { json: true });
expect(await access(join(dir, 'stats.json'))).toBeUndefined();
// Need to clean up manually as it is placed in project root
await unlink(join(dir, 'stats.json'));
await build(dir, { json: false });
await expect(access(join(dir, 'stats.json'))).rejects.toThrow(
'no such file or directory'
);
});
it('--template', async () => {
let dir = await subject('custom-template');
await rename(
join(dir, 'template.html'),
join(dir, 'renamed-template.html')
);
await build(dir, { template: 'renamed-template.html' });
const html = await getOutputFile(dir, 'index.html');
expect(html).toEqual(
expect.stringMatching(getRegExpFromMarkup(images.template))
);
});
it('--preload', async () => {
let dir = await subject('preload-chunks');
await build(dir, { preload: true });
let head = await getHead(dir);
expect(head).toEqual(
expect.stringMatching(getRegExpFromMarkup(images.preload.true))
);
await build(dir, { preload: false });
head = await getHead(dir);
expect(head).toEqual(
expect.stringMatching(getRegExpFromMarkup(images.preload.false))
);
});
it('--prerender', async () => {
let dir = await subject('minimal');
await build(dir, { prerender: true });
let html = await getOutputFile(dir, 'index.html');
expect(html).toMatch('<h1>Minimal App</h1>');
await build(dir, { prerender: false });
html = await getOutputFile(dir, 'index.html');
expect(html).not.toMatch('<h1>Minimal App</h1>');
});
it('--prerenderUrls', async () => {
let dir = await subject('multiple-prerendering');
await build(dir, { prerenderUrls: 'prerender-urls.json' });
expect(await access(join(dir, 'build/index.html'))).toBeUndefined();
expect(
await access(join(dir, 'build/route66/index.html'))
).toBeUndefined();
expect(
await access(join(dir, 'build/custom/index.html'))
).toBeUndefined();
await rename(
join(dir, 'prerender-urls.json'),
join(dir, 'renamed-urls.json')
);
await build(dir, { prerenderUrls: 'renamed-urls.json' });
expect(await access(join(dir, 'build/index.html'))).toBeUndefined();
expect(
await access(join(dir, 'build/route66/index.html'))
).toBeUndefined();
expect(
await access(join(dir, 'build/custom/index.html'))
).toBeUndefined();
});
it('--inline-css', async () => {
let dir = await subject('minimal');
await build(dir, { 'inline-css': true });
let head = await getHead(dir);
expect(head).toMatch('<style>h1{color:red}</style>');
await build(dir, { 'inline-css': false });
head = await getOutputFile(dir, 'index.html');
expect(head).not.toMatch(/<style>[^<]*<\/style>/);
});
it('--config', async () => {
let dir = await subject('custom-webpack');
await build(dir, { config: 'preact.config.js' });
expect(await access(join(dir, 'build/bundle.js'))).toBeUndefined();
await rename(
join(dir, 'preact.config.js'),
join(dir, 'renamed-config.js')
);
await build(dir, { config: 'renamed-config.js' });
expect(await access(join(dir, 'build/bundle.js'))).toBeUndefined();
});
it('--invalid-arg', async () => {
let dir = await subject('minimal');
// @ts-ignore
const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {});
await expect(build(dir, { 'invalid-arg': false })).rejects.toEqual(
new Error('Invalid argument found.')
);
expect(mockExit).toHaveBeenCalledWith(1);
mockExit.mockRestore();
});
});
describe('CSS', () => {
it('should resolve CSS imports', async () => {
let dir = await subject('css-imports');
await mkdir(`${dir}/node_modules/fake-module`, { recursive: true });
await writeFile(
`${dir}/node_modules/fake-module/style.css`,
'h2{color:green}'
);
await build(dir);
const builtStylesheet = await getOutputFile(dir, /bundle\.\w{5}\.css$/);
expect(builtStylesheet).toMatch('h1{color:red}');
expect(builtStylesheet).toMatch('h1{background:#ffdab9}');
expect(builtStylesheet).toMatch(/body{background:url\(\/.*\.jpg\)}/);
expect(builtStylesheet).toMatch('h2{color:green}');
});
it('should use CSS Modules in `routes` and `components` directories', async () => {
let dir = await subject('css-auto-modules');
await build(dir);
const builtStylesheet = await getOutputFile(dir, /bundle\.\w{5}\.css$/);
const builtSplitStylesheet = await getOutputFile(
dir,
/route-index\.chunk\.\w{5}\.css$/
);
expect(builtStylesheet).toMatch('h1{color:red}');
expect(builtStylesheet).toMatch(/\.text__\w{5}{color:tan}/);
expect(builtSplitStylesheet).toMatch(/\.text__\w{5}{color:red}/);
});
it('should inline critical CSS only', async () => {
let dir = await subject('css-inline');
await build(dir);
const builtStylesheet = await getOutputFile(dir, /bundle\.\w{5}\.css$/);
const html = await getOutputFile(dir, 'index.html');
expect(builtStylesheet).toMatch('h1{color:red}div{background:tan}');
expect(html).toMatch('<style>h1{color:red}</style>');
});
// Issue #1411
it('should preserve side-effectful CSS imports even if package.json claims no side effects', async () => {
let dir = await subject('css-side-effect');
await build(dir);
const builtStylesheet = await getOutputFile(dir, /bundle\.\w{5}\.css$/);
expect(builtStylesheet).toMatch('h1{background:#673ab8}');
});
it('should use SASS styles', async () => {
let dir = await subject('css-sass');
await build(dir);
let body = await getBody(dir);
looksLike(body, images.sass);
});
});
describe('prerender', () => {
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)
)
);
});
});
});
});