From d9642ccc59366a59661f093b1abfc2e4c4bc6a0a Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Thu, 4 Jun 2020 08:37:18 +0530 Subject: [PATCH 1/4] feat: add init to webpack-cli --- packages/webpack-cli/package.json | 1 + 1 file changed, 1 insertion(+) 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", From 43d38838223a884b38a22a881d5425249e37e126 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Thu, 4 Jun 2020 22:16:38 +0530 Subject: [PATCH 2/4] tests: init integration with cli --- packages/webpack-cli/__tests__/init.test.js | 49 +++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 packages/webpack-cli/__tests__/init.test.js diff --git a/packages/webpack-cli/__tests__/init.test.js b/packages/webpack-cli/__tests__/init.test.js new file mode 100644 index 00000000000..41a08ebf87c --- /dev/null +++ b/packages/webpack-cli/__tests__/init.test.js @@ -0,0 +1,49 @@ +const { sync: spawnSync } = require('execa'); +const path = require('path'); +const fs = require('fs'); +const genPath = path.join(__dirname, 'test-assets'); +const firstPrompt = 'Will your application have multiple bundles?'; + +describe('init', () => { + 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(); + }); +}); From b0151c3f3f25b04091ef9afe15c402f50aa4e83d Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Thu, 4 Jun 2020 22:28:28 +0530 Subject: [PATCH 3/4] chore: add timeout --- packages/webpack-cli/__tests__/init.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/webpack-cli/__tests__/init.test.js b/packages/webpack-cli/__tests__/init.test.js index 41a08ebf87c..76b56822c8e 100644 --- a/packages/webpack-cli/__tests__/init.test.js +++ b/packages/webpack-cli/__tests__/init.test.js @@ -4,6 +4,8 @@ const fs = require('fs'); const genPath = path.join(__dirname, 'test-assets'); const firstPrompt = 'Will your application have multiple bundles?'; +jest.setTimeout(60000); + describe('init', () => { it('should work with cli', () => { const { stdout, stderr } = spawnSync(path.resolve(__dirname, '../bin/cli.js'), ['init'], { From 678314748c3a1707ce21164cfbefd15167b84cfb Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Thu, 4 Jun 2020 22:48:14 +0530 Subject: [PATCH 4/4] chore: use resolved path --- packages/webpack-cli/__tests__/init.test.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/webpack-cli/__tests__/init.test.js b/packages/webpack-cli/__tests__/init.test.js index 76b56822c8e..a34e5eb6080 100644 --- a/packages/webpack-cli/__tests__/init.test.js +++ b/packages/webpack-cli/__tests__/init.test.js @@ -1,12 +1,24 @@ +/* eslint-disable node/no-extraneous-require */ const { sync: spawnSync } = require('execa'); const path = require('path'); const fs = require('fs'); -const genPath = path.join(__dirname, 'test-assets'); +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,