-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
cli.test.js
159 lines (142 loc) · 4.43 KB
/
cli.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use strict';
/* eslint-disable
array-bracket-spacing,
*/
const path = require('path');
const execa = require('execa');
const runDevServer = require('./helpers/run-webpack-dev-server');
const httpsCertificateDirectory = path.join(
__dirname,
'fixtures/https-certificate'
);
const caPath = path.join(httpsCertificateDirectory, 'ca.pem');
const pfxPath = path.join(httpsCertificateDirectory, 'server.pfx');
const keyPath = path.join(httpsCertificateDirectory, 'server.key');
const certPath = path.join(httpsCertificateDirectory, 'server.crt');
describe('CLI', () => {
it('--progress', (done) => {
runDevServer('--progress')
.then((output) => {
expect(output.code).toEqual(0);
expect(output.stderr.indexOf('0% compiling') >= 0).toBe(true);
done();
})
.catch(done);
});
it('--bonjour', (done) => {
runDevServer('--bonjour')
.then((output) => {
expect(output.code).toEqual(0);
expect(output.stdout.indexOf('Bonjour') >= 0).toBe(true);
done();
})
.catch(done);
});
it('--https', (done) => {
runDevServer('--https')
.then((output) => {
expect(output.code).toEqual(0);
expect(output.stdout.indexOf('Project is running at') >= 0).toBe(true);
done();
})
.catch(done);
});
it('--https --cacert --pfx --key --cert --pfx-passphrase', (done) => {
runDevServer(
`--https --cacert ${caPath} --pfx ${pfxPath} --key ${keyPath} --cert ${certPath} --pfx-passphrase webpack-dev-server`
)
.then((output) => {
expect(output.code).toEqual(0);
expect(output.stdout.indexOf('Project is running at') >= 0).toBe(true);
done();
})
.catch(done);
});
it('should exit the process when SIGINT is detected', (done) => {
const cliPath = path.resolve(__dirname, '../bin/webpack-dev-server.js');
const examplePath = path.resolve(__dirname, '../examples/cli/public');
const cp = execa('node', [cliPath], { cwd: examplePath });
cp.stdout.on('data', (data) => {
const bits = data.toString();
if (/Compiled successfully/.test(bits)) {
expect(cp.pid !== 0).toBe(true);
cp.kill('SIGINT');
}
});
cp.on('exit', () => {
done();
});
});
it('should exit the process when SIGINT is detected, even before the compilation is done', (done) => {
const cliPath = path.resolve(__dirname, '../bin/webpack-dev-server.js');
const examplePath = path.resolve(__dirname, '../examples/cli/public');
const cp = execa('node', [cliPath], { cwd: examplePath });
let killed = false;
cp.stdout.on('data', () => {
if (!killed) {
expect(cp.pid !== 0).toBe(true);
cp.kill('SIGINT');
}
killed = true;
});
cp.on('exit', () => {
done();
});
});
it('should use different random port when multiple instances are started on different processes', (done) => {
const cliPath = path.resolve(__dirname, '../bin/webpack-dev-server.js');
const examplePath = path.resolve(__dirname, '../examples/cli/public');
const cp = execa('node', [cliPath], { cwd: examplePath });
const cp2 = execa('node', [cliPath], { cwd: examplePath });
const runtime = {
cp: {
port: null,
done: false,
},
cp2: {
port: null,
done: false,
},
};
cp.stdout.on('data', (data) => {
const bits = data.toString();
const portMatch = /Project is running at http:\/\/localhost:(\d*)\//.exec(
bits
);
if (portMatch) {
runtime.cp.port = portMatch[1];
}
if (/Compiled successfully/.test(bits)) {
expect(cp.pid !== 0).toBe(true);
cp.kill('SIGINT');
}
});
cp2.stdout.on('data', (data) => {
const bits = data.toString();
const portMatch = /Project is running at http:\/\/localhost:(\d*)\//.exec(
bits
);
if (portMatch) {
runtime.cp2.port = portMatch[1];
}
if (/Compiled successfully/.test(bits)) {
expect(cp.pid !== 0).toBe(true);
cp2.kill('SIGINT');
}
});
cp.on('exit', () => {
runtime.cp.done = true;
if (runtime.cp2.done) {
expect(runtime.cp.port !== runtime.cp2.port).toBe(true);
done();
}
});
cp2.on('exit', () => {
runtime.cp2.done = true;
if (runtime.cp.done) {
expect(runtime.cp.port !== runtime.cp2.port).toBe(true);
done();
}
});
});
});