/
init.test.js
59 lines (49 loc) · 1.89 KB
/
init.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
/* eslint-disable node/no-extraneous-require */
const { sync: spawnSync } = require('execa');
const path = require('path');
const fs = require('fs');
const rimraf = require('rimraf');
const genPath = path.resolve(__dirname, './test-assets');
const firstPrompt = 'Will your application have multiple bundles?';
describe('init', () => {
beforeAll(() => {
rimraf.sync(genPath);
fs.mkdirSync(genPath);
});
afterAll(() => {
rimraf.sync(genPath);
});
it('should work with cli', () => {
const { stdout } = spawnSync(path.resolve(__dirname, '../bin/cli.js'), ['init'], {
cwd: genPath,
reject: false,
});
expect(stdout).toContain(firstPrompt);
});
it('should run with cli when auto is supplied', () => {
const { stdout } = spawnSync(path.resolve(__dirname, '../bin/cli.js'), ['init', '--auto'], {
cwd: genPath,
reject: false,
});
// Test no prompts are present
expect(stdout).not.toContain(firstPrompt);
// Skip test in case installation fails
if (!fs.existsSync(path.resolve(genPath, './yarn.lock'))) {
return;
}
// Test regressively files are scaffolded
const files = ['./sw.js', './package.json', './src/index.js'];
files.forEach((file) => {
expect(fs.existsSync(path.resolve(genPath, file))).toBeTruthy();
});
// Check package json is correctly configured
const pkgJsonTests = () => {
const pkgJson = require(path.join(genPath, './package.json'));
expect(pkgJson).toBeTruthy();
expect(pkgJson['devDependencies']).toBeTruthy();
expect(pkgJson['devDependencies']['webpack']).toBeTruthy();
expect(pkgJson['scripts']['build'] == 'webpack').toBeTruthy();
};
expect(pkgJsonTests).not.toThrow();
});
});