Skip to content

Commit e8964d1

Browse files
ryanwholeyshellscape
authored andcommitted
add --progress cli test (#1182)
1 parent a9327e5 commit e8964d1

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

test/cli.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
const assert = require('assert');
44
const path = require('path');
55
const execa = require('execa');
6+
const runDevServer = require('./helpers/run-webpack-dev-server');
7+
68

79
describe('SIGINT', () => {
810
it('should exit the process when SIGINT is detected', (done) => {
@@ -26,3 +28,15 @@ describe('SIGINT', () => {
2628
});
2729
}).timeout(4000);
2830
});
31+
32+
describe('CLI', () => {
33+
it('--progress', (done) => {
34+
runDevServer('--progress')
35+
.then((output) => {
36+
assert(output.code === 0);
37+
assert(output.stderr.indexOf('0% compiling') >= 0);
38+
done();
39+
})
40+
.catch(done);
41+
}).timeout(4000);
42+
});

test/fixtures/cli/foo.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
console.log('i am foo!');

test/fixtures/cli/webpack.config.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
module.exports = {
4+
context: __dirname,
5+
entry: './foo.js',
6+
output: {
7+
filename: 'bundle.js'
8+
},
9+
plugins: [{
10+
apply(compiler) {
11+
compiler.plugin('done', (stats) => {
12+
let exitCode = 0;
13+
if (stats.hasErrors()) {
14+
exitCode = 1;
15+
}
16+
setTimeout(() => process.exit(exitCode));
17+
});
18+
}
19+
}]
20+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const spawn = require('child_process').spawn;
5+
6+
const webpackDevServerPath = path.resolve(__dirname, '../../bin/webpack-dev-server.js');
7+
const basicConfigPath = path.resolve(__dirname, '../fixtures/cli/webpack.config.js');
8+
9+
10+
function runWebackDevServer(testArgs, configPath) {
11+
const cwd = process.cwd();
12+
const env = process.env.NODE_ENV;
13+
let stdout = '';
14+
let stderr = '';
15+
16+
if (!configPath) {
17+
configPath = basicConfigPath;
18+
}
19+
20+
if (!testArgs) {
21+
testArgs = [];
22+
} else if (typeof testArgs === 'string') {
23+
testArgs = testArgs.split(' ');
24+
}
25+
26+
const args = [webpackDevServerPath, '--config', configPath].concat(testArgs);
27+
28+
return new Promise((resolve, reject) => {
29+
const child = spawn('node', args, { cwd, env });
30+
31+
child.on('error', error => reject(error));
32+
33+
child.stdout.on('data', (data) => {
34+
stdout += data.toString();
35+
});
36+
37+
child.stderr.on('data', (data) => {
38+
stderr += data.toString();
39+
});
40+
41+
child.on('close', (code) => {
42+
if (code !== 0) {
43+
return reject(stderr);
44+
}
45+
resolve({ stdout, stderr, code });
46+
});
47+
});
48+
}
49+
50+
module.exports = runWebackDevServer;

0 commit comments

Comments
 (0)