From 55afe7087222fa94742feb33f656f6a2b8730847 Mon Sep 17 00:00:00 2001 From: Ryan Wholey Date: Wed, 8 Nov 2017 00:55:05 -0800 Subject: [PATCH] add --progress cli test --- test/cli.test.js | 14 ++++++++ test/fixtures/cli/foo.js | 3 ++ test/fixtures/cli/webpack.config.js | 20 +++++++++++ test/helpers/run-webpack-dev-server.js | 50 ++++++++++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 test/fixtures/cli/foo.js create mode 100644 test/fixtures/cli/webpack.config.js create mode 100644 test/helpers/run-webpack-dev-server.js diff --git a/test/cli.test.js b/test/cli.test.js index 159b330a68..d9ed5293a0 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -3,6 +3,8 @@ const assert = require('assert'); const path = require('path'); const execa = require('execa'); +const runDevServer = require('./helpers/run-webpack-dev-server'); + describe('SIGINT', () => { it('should exit the process when SIGINT is detected', (done) => { @@ -26,3 +28,15 @@ describe('SIGINT', () => { }); }).timeout(4000); }); + +describe('CLI', () => { + it('--progress', (done) => { + runDevServer('--progress') + .then((output) => { + assert(output.code === 0); + assert(output.stderr.indexOf('0% compiling') >= 0); + done(); + }) + .catch(done); + }).timeout(4000); +}); diff --git a/test/fixtures/cli/foo.js b/test/fixtures/cli/foo.js new file mode 100644 index 0000000000..f88d8b5040 --- /dev/null +++ b/test/fixtures/cli/foo.js @@ -0,0 +1,3 @@ +'use strict'; + +console.log('i am foo!'); diff --git a/test/fixtures/cli/webpack.config.js b/test/fixtures/cli/webpack.config.js new file mode 100644 index 0000000000..16db7d96b7 --- /dev/null +++ b/test/fixtures/cli/webpack.config.js @@ -0,0 +1,20 @@ +'use strict'; + +module.exports = { + context: __dirname, + entry: './foo.js', + output: { + filename: 'bundle.js' + }, + plugins: [{ + apply(compiler) { + compiler.plugin('done', (stats) => { + let exitCode = 0; + if (stats.hasErrors()) { + exitCode = 1; + } + setTimeout(() => process.exit(exitCode)); + }); + } + }] +}; diff --git a/test/helpers/run-webpack-dev-server.js b/test/helpers/run-webpack-dev-server.js new file mode 100644 index 0000000000..214e1c7e81 --- /dev/null +++ b/test/helpers/run-webpack-dev-server.js @@ -0,0 +1,50 @@ +'use strict'; + +const path = require('path'); +const spawn = require('child_process').spawn; + +const webpackDevServerPath = path.resolve(__dirname, '../../bin/webpack-dev-server.js'); +const basicConfigPath = path.resolve(__dirname, '../fixtures/cli/webpack.config.js'); + + +function runWebackDevServer(testArgs, configPath) { + const cwd = process.cwd(); + const env = process.env.NODE_ENV; + let stdout = ''; + let stderr = ''; + + if (!configPath) { + configPath = basicConfigPath; + } + + if (!testArgs) { + testArgs = []; + } else if (typeof testArgs === 'string') { + testArgs = testArgs.split(' '); + } + + const args = [webpackDevServerPath, '--config', configPath].concat(testArgs); + + return new Promise((resolve, reject) => { + const child = spawn('node', args, { cwd, env }); + + child.on('error', error => reject(error)); + + child.stdout.on('data', (data) => { + stdout += data.toString(); + }); + + child.stderr.on('data', (data) => { + stderr += data.toString(); + }); + + child.on('close', (code) => { + if (code !== 0) { + return reject(stderr); + } + resolve({ stdout, stderr, code }); + }); + }); +} + +module.exports = runWebackDevServer;