diff --git a/packages/webpack-cli/__tests__/init.test.js b/packages/webpack-cli/__tests__/init.test.js new file mode 100644 index 00000000000..a34e5eb6080 --- /dev/null +++ b/packages/webpack-cli/__tests__/init.test.js @@ -0,0 +1,63 @@ +/* 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?'; + +jest.setTimeout(60000); + +describe('init', () => { + beforeAll(() => { + rimraf.sync(genPath); + fs.mkdirSync(genPath); + }); + + afterAll(() => { + rimraf.sync(genPath); + }); + + it('should work with cli', () => { + const { stdout, stderr } = spawnSync(path.resolve(__dirname, '../bin/cli.js'), ['init'], { + cwd: genPath, + reject: false, + }); + expect(stdout).toBeTruthy(); + expect(stderr).toBeFalsy(); + 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).toBeTruthy(); + 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']; + + // eslint-disable-next-line prettier/prettier + 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(); + }); +}); diff --git a/packages/webpack-cli/package.json b/packages/webpack-cli/package.json index 397fd074556..0bce7486474 100644 --- a/packages/webpack-cli/package.json +++ b/packages/webpack-cli/package.json @@ -25,6 +25,7 @@ "dependencies": { "@webpack-cli/package-utils": "^1.0.1-alpha.4", "@webpack-cli/info": "^1.0.1-alpha.4", + "@webpack-cli/init": "^1.0.1-alpha.5", "ansi-escapes": "^4.3.1", "chalk": "^3.0.0", "command-line-usage": "^6.1.0",