Skip to content

Commit 6bec272

Browse files
test: update
1 parent d7b4192 commit 6bec272

File tree

3 files changed

+151
-22
lines changed

3 files changed

+151
-22
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`CLI --host :: (IPv6): stderr 1`] = `
4+
"<i> [webpack-dev-server] Project is running at:
5+
<i> [webpack-dev-server] Local: http://localhost:<port>/
6+
<i> [webpack-dev-server] On Your Network: http://<network-ip-v4>:<port>/ (IPv4)
7+
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
8+
`;
9+
10+
exports[`CLI --host ::1 (IPv6): stderr 1`] = `
11+
"<i> [webpack-dev-server] Project is running at http://[::1]:<port>/ (IPv6)
12+
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
13+
`;
14+
15+
exports[`CLI --host 0.0.0.0 (IPv4): stderr 1`] = `
16+
"<i> [webpack-dev-server] Project is running at:
17+
<i> [webpack-dev-server] Local: http://localhost:<port>/
18+
<i> [webpack-dev-server] On Your Network: http://<network-ip-v4>:<port>/ (IPv4)
19+
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
20+
`;
21+
22+
exports[`CLI --host 0:0:0:0:0:FFFF:7F00:0001 (IPv6): stderr 1`] = `
23+
"<i> [webpack-dev-server] Project is running at http://127.0.0.1:<port>/ (IPv4), http://[::ffff:127.0.0.1]:<port>/ (IPv6)
24+
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
25+
`;
26+
27+
exports[`CLI --host 127.0.0.1 (IPv4): stderr 1`] = `
28+
"<i> [webpack-dev-server] Project is running at http://127.0.0.1:<port>/ (IPv4)
29+
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
30+
`;
31+
32+
exports[`CLI --host 192.168.0.5 (IPv4): stderr 1`] = `
33+
"<i> [webpack-dev-server] Project is running at http://<network-ip-v4>:<port>/ (IPv4)
34+
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
35+
`;
36+
37+
exports[`CLI --host and --port are unspecified: stderr 1`] = `
38+
"<i> [webpack-dev-server] Project is running at:
39+
<i> [webpack-dev-server] Local: http://localhost:<port>/
40+
<i> [webpack-dev-server] On Your Network: http://<network-ip-v4>:<port>/ (IPv4)
41+
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
42+
`;
43+
44+
exports[`CLI --host localhost --port 9999: stderr 1`] = `
45+
"<i> [webpack-dev-server] Project is running at http://127.0.0.1:<port>/ (IPv4)
46+
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
47+
`;
48+
49+
exports[`CLI --host localhost: stderr 1`] = `
50+
"<i> [webpack-dev-server] Project is running at http://127.0.0.1:<port>/ (IPv4)
51+
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
52+
`;

test/cli/cli.test.js

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
const path = require('path');
44
const execa = require('execa');
55
const internalIp = require('internal-ip');
6-
const testBin = require('../helpers/test-bin');
6+
const { testBin, normalizeStderr } = require('../helpers/test-bin');
7+
8+
const localIPv4 = internalIp.v4.sync();
9+
const localIPv6 = internalIp.v6.sync();
710

811
describe('CLI', () => {
912
it('--hot', (done) => {
@@ -90,10 +93,11 @@ describe('CLI', () => {
9093
.catch(done);
9194
});
9295

93-
it('unspecified host and port', (done) => {
96+
it('--host and --port are unspecified', (done) => {
9497
testBin('')
9598
.then((output) => {
96-
expect(/http:\/\/localhost:[0-9]+/.test(output.stderr)).toEqual(true);
99+
expect(normalizeStderr(output.stderr)).toMatchSnapshot('stderr');
100+
97101
done();
98102
})
99103
.catch(done);
@@ -102,27 +106,28 @@ describe('CLI', () => {
102106
it('--host 0.0.0.0 (IPv4)', (done) => {
103107
testBin('--host 0.0.0.0')
104108
.then((output) => {
105-
const localIP = internalIp.v4.sync();
109+
expect(normalizeStderr(output.stderr)).toMatchSnapshot('stderr');
106110

107-
expect(/http:\/\/localhost:[0-9]+/.test(output.stderr)).toEqual(true);
108-
expect(
109-
new RegExp(`http://${localIP}:[0-9]+/`).test(output.stderr)
110-
).toEqual(true);
111111
done();
112112
})
113113
.catch(done);
114114
});
115115

116-
// TODO search way how to tests it on github actions
117-
it.skip('--host :: (IPv6)', (done) => {
116+
it('--host :: (IPv6)', (done) => {
118117
testBin('--host ::')
119118
.then((output) => {
120-
const localIP = internalIp.v4.sync();
119+
expect(normalizeStderr(output.stderr)).toMatchSnapshot('stderr');
120+
121+
done();
122+
})
123+
.catch(done);
124+
});
125+
126+
it('--host ::1 (IPv6)', (done) => {
127+
testBin('--host ::1')
128+
.then((output) => {
129+
expect(normalizeStderr(output.stderr)).toMatchSnapshot('stderr');
121130

122-
expect(/http:\/\/localhost:[0-9]+/.test(output.stderr)).toEqual(true);
123-
expect(
124-
new RegExp(`http://${localIP}:[0-9]+/`).test(output.stderr)
125-
).toEqual(true);
126131
done();
127132
})
128133
.catch(done);
@@ -131,16 +136,58 @@ describe('CLI', () => {
131136
it('--host localhost', (done) => {
132137
testBin('--host localhost')
133138
.then((output) => {
134-
expect(/http:\/\/localhost:[0-9]+/.test(output.stderr)).toEqual(true);
139+
expect(normalizeStderr(output.stderr)).toMatchSnapshot('stderr');
140+
135141
done();
136142
})
137143
.catch(done);
138144
});
139145

140-
it('--port and --host', (done) => {
141-
testBin('--port 9999 --host localhost')
146+
it('--host 127.0.0.1 (IPv4)', (done) => {
147+
testBin('--host 127.0.0.1')
142148
.then((output) => {
143-
expect(/http:\/\/localhost:9999/.test(output.stderr)).toEqual(true);
149+
expect(normalizeStderr(output.stderr)).toMatchSnapshot('stderr');
150+
151+
done();
152+
})
153+
.catch(done);
154+
});
155+
156+
it('--host 0:0:0:0:0:FFFF:7F00:0001 (IPv6)', (done) => {
157+
testBin('--host 0:0:0:0:0:FFFF:7F00:0001')
158+
.then((output) => {
159+
expect(normalizeStderr(output.stderr)).toMatchSnapshot('stderr');
160+
161+
done();
162+
})
163+
.catch(done);
164+
});
165+
166+
it(`--host ${localIPv4} (IPv4)`, (done) => {
167+
testBin(`--host ${localIPv4}`)
168+
.then((output) => {
169+
expect(normalizeStderr(output.stderr)).toMatchSnapshot('stderr');
170+
171+
done();
172+
})
173+
.catch(done);
174+
});
175+
176+
it.skip(`--host ${localIPv6} (IPv6)`, (done) => {
177+
testBin(`--host ${localIPv6}`)
178+
.then((output) => {
179+
expect(normalizeStderr(output.stderr)).toMatchSnapshot('stderr');
180+
181+
done();
182+
})
183+
.catch(done);
184+
});
185+
186+
it('--host localhost --port 9999', (done) => {
187+
testBin('--host localhost --port 9999')
188+
.then((output) => {
189+
expect(normalizeStderr(output.stderr)).toMatchSnapshot('stderr');
190+
144191
done();
145192
})
146193
.catch(done);
@@ -292,8 +339,6 @@ describe('CLI', () => {
292339
});
293340
});
294341

295-
// TODO: do not skip after @webpack-cli/serve passes null port by default
296-
// https://github.com/webpack/webpack-cli/pull/2126
297342
it.skip('should use different random port when multiple instances are started on different processes', (done) => {
298343
const cliPath = path.resolve(__dirname, '../../bin/webpack-dev-server.js');
299344
const cwd = path.resolve(__dirname, '../fixtures/cli');

test/helpers/test-bin.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const path = require('path');
44
const execa = require('execa');
5+
const internalIp = require('internal-ip');
56

67
const webpackDevServerPath = path.resolve(
78
__dirname,
@@ -31,4 +32,35 @@ function testBin(testArgs, configPath) {
3132
return execa('node', args, { cwd, env, timeout: 10000 });
3233
}
3334

34-
module.exports = testBin;
35+
function normalizeStderr(stderr) {
36+
let normalizedStderr = stderr;
37+
38+
normalizedStderr = normalizedStderr.replace(
39+
new RegExp(process.cwd(), 'g'),
40+
'<cwd>'
41+
);
42+
43+
const networkIPv4 = internalIp.v4.sync();
44+
45+
if (networkIPv4) {
46+
normalizedStderr = normalizedStderr.replace(
47+
new RegExp(networkIPv4, 'g'),
48+
'<network-ip-v4>'
49+
);
50+
}
51+
52+
const networkIPv6 = internalIp.v6.sync();
53+
54+
if (networkIPv6) {
55+
normalizedStderr = normalizedStderr.replace(
56+
new RegExp(networkIPv6, 'g'),
57+
'<network-ip-v6>'
58+
);
59+
}
60+
61+
normalizedStderr = normalizedStderr.replace(/:[0-9]+\//g, ':<port>/');
62+
63+
return normalizedStderr;
64+
}
65+
66+
module.exports = { normalizeStderr, testBin };

0 commit comments

Comments
 (0)