UNPKG

2.09 kBJavaScriptView Raw
1const makeDir = require('./utils/makeTempDir');
2const startServer = require('./utils/startServer');
3const runGenerator = require('./utils/runGenerator');
4const fs = require('fs');
5const fetch = require('node-fetch');
6const execa = require('execa');
7const path = require('path');
8
9describe('Create Frontend with 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=react']);
22
23 const files = fs.readdirSync(tempDir.path);
24
25 expect(files).toEqual(
26 expect.arrayContaining([
27 'client', // Frontend directory was created
28 'package.json', // NPM boilerplate was added
29 'node_modules', // NPM modules were installed
30 ])
31 );
32 });
33
34 it('should start the dev server without errors', async () => {
35 const { output, cleanup } = await startServer(tempDir.path, 'dev', {
36 devServerMessage: /webpack dev server started at/i,
37 buildDoneMessage: /build for web done/i,
38 });
39
40 const url = output.devServerMessage.match(/https?:\/\/.*:(?:\d)+/)[0];
41
42 const result = await fetch(url);
43 expect(result.status).toBe(200);
44 const text = await result.text();
45 expect(text).toMatch(/^<!DOCTYPE html>/i);
46
47 await cleanup();
48 });
49
50 it('should create a production build with a manifest', async () => {
51 await execa('npm', ['run', 'build'], { cwd: tempDir.path });
52
53 const files = fs.readdirSync(path.resolve(tempDir.path, 'public/build'));
54
55 expect(files).toEqual(
56 expect.arrayContaining([
57 expect.stringMatching(/^app-.*\.css/),
58 expect.stringMatching(/^app-.*\.js/),
59 'asset-manifest.json',
60 ])
61 );
62
63 const manifest = JSON.parse(fs.readFileSync(path.resolve(tempDir.path, 'public/build/asset-manifest.json')));
64
65 expect(manifest).toMatchObject({
66 'app.css': expect.stringMatching(/^\/build\/.*\.css/),
67 'app.js': expect.stringMatching(/^\/build\/.*\.js/),
68 });
69 });
70});