1 | const makeDir = require('./utils/makeTempDir');
|
2 | const startServer = require('./utils/startServer');
|
3 | const runGenerator = require('./utils/runGenerator');
|
4 | const fs = require('fs');
|
5 | const fetch = require('node-fetch');
|
6 | const path = require('path');
|
7 | const execa = require('execa');
|
8 |
|
9 | describe('Create Frontend with Universal React template', () => {
|
10 | let tempDir;
|
11 |
|
12 | beforeAll(async () => {
|
13 | tempDir = await makeDir();
|
14 | });
|
15 |
|
16 | afterAll(() => {
|
17 | tempDir.cleanup();
|
18 | });
|
19 |
|
20 | it('should generate a folder structure', async () => {
|
21 | await runGenerator(tempDir.path, ['--template=universal-react']);
|
22 |
|
23 | const files = fs.readdirSync(tempDir.path);
|
24 |
|
25 | expect(files).toEqual(
|
26 | expect.arrayContaining([
|
27 | 'client',
|
28 | 'server',
|
29 | 'app',
|
30 | 'package.json',
|
31 | 'node_modules',
|
32 | ])
|
33 | );
|
34 | });
|
35 |
|
36 | it('should start the dev server without errors', async () => {
|
37 | const { output, cleanup } = await startServer(tempDir.path, 'dev', {
|
38 | devServerMessage: /server started at/i,
|
39 | webBuildDoneMessage: /build for web done/i,
|
40 | nodeBuildDoneMessage: /build for node done/i,
|
41 | });
|
42 |
|
43 | const url = output.devServerMessage.match(/https?:\/\/.*:(?:\d)+/)[0];
|
44 | const result = await fetch(url);
|
45 | expect(result.status).toBe(200);
|
46 |
|
47 | const text = await result.text();
|
48 | expect(text).toMatch(/^<!DOCTYPE html>/i);
|
49 |
|
50 | await cleanup();
|
51 | });
|
52 |
|
53 | it('should create a production build with a manifest', async () => {
|
54 | await execa('npm', ['run', 'build'], { cwd: tempDir.path });
|
55 |
|
56 | |
57 |
|
58 |
|
59 | const clientFiles = fs.readdirSync(path.resolve(tempDir.path, 'build/client'));
|
60 |
|
61 | expect(clientFiles).toEqual(
|
62 | expect.arrayContaining([
|
63 | expect.stringMatching(/^app-.*\.css/),
|
64 | expect.stringMatching(/^app-.*\.js/),
|
65 | 'asset-manifest.json',
|
66 | ])
|
67 | );
|
68 |
|
69 | const manifest = JSON.parse(fs.readFileSync(path.resolve(tempDir.path, 'build/client/asset-manifest.json')));
|
70 |
|
71 | expect(manifest).toMatchObject({
|
72 | 'app.css': expect.stringMatching(/^\/client\/.*\.css/),
|
73 | 'app.js': expect.stringMatching(/^\/client\/.*\.js/),
|
74 | });
|
75 |
|
76 | |
77 |
|
78 |
|
79 | const serverFiles = fs.readdirSync(path.resolve(tempDir.path, 'build/server'));
|
80 |
|
81 | expect(serverFiles).toEqual(expect.arrayContaining(['build-server.js']));
|
82 | });
|
83 | });
|