Skip to content

Commit cbd2450

Browse files
committed
test(progress): added progress and profile option tests
1 parent 1ee23d2 commit cbd2450

File tree

4 files changed

+117
-13
lines changed

4 files changed

+117
-13
lines changed

lib/Server.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -144,27 +144,25 @@ class Server {
144144
}
145145

146146
setupProgressPlugin() {
147+
// for CLI output
147148
new webpack.ProgressPlugin({
148149
profile: !!this.options.profile,
149150
}).apply(this.compiler);
150151

151-
const progressPlugin = new webpack.ProgressPlugin(
152-
(percent, msg, addInfo) => {
153-
percent = Math.floor(percent * 100);
152+
// for browser console output
153+
new webpack.ProgressPlugin((percent, msg, addInfo) => {
154+
percent = Math.floor(percent * 100);
154155

155-
if (percent === 100) {
156-
msg = 'Compilation completed';
157-
}
158-
159-
if (addInfo) {
160-
msg = `${msg} (${addInfo})`;
161-
}
156+
if (percent === 100) {
157+
msg = 'Compilation completed';
158+
}
162159

163-
this.sockWrite(this.sockets, 'progress-update', { percent, msg });
160+
if (addInfo) {
161+
msg = `${msg} (${addInfo})`;
164162
}
165-
);
166163

167-
progressPlugin.apply(this.compiler);
164+
this.sockWrite(this.sockets, 'progress-update', { percent, msg });
165+
}).apply(this.compiler);
168166
}
169167

170168
setupApp() {

test/ports-map.js

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const portsList = {
3838
WebsocketClient: 1,
3939
WebsocketServer: 1,
4040
Progress: 1,
41+
'progress-option': 1,
42+
'profile-option': 1,
4143
};
4244

4345
let startPort = 8079;

test/server/profile-option.test.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
const webpack = require('webpack');
4+
const Server = require('../../lib/Server');
5+
const config = require('../fixtures/simple-config/webpack.config');
6+
const port = require('../ports-map')['progress-option'];
7+
8+
describe('profile', () => {
9+
describe('output', () => {
10+
let mockStderr;
11+
12+
beforeAll(() => {
13+
mockStderr = jest
14+
.spyOn(process.stderr, 'write')
15+
.mockImplementation(() => {});
16+
});
17+
18+
it('should show percentage progress with profile data', (done) => {
19+
const compiler = webpack(config);
20+
const server = new Server(compiler, {
21+
port,
22+
// profile will only have an effect when progress is enabled
23+
progress: true,
24+
profile: true,
25+
});
26+
27+
compiler.hooks.done.tap('webpack-dev-server', () => {
28+
const calls = mockStderr.mock.calls;
29+
mockStderr.mockRestore();
30+
let foundProgress = false;
31+
let foundProfile = false;
32+
calls.forEach((call) => {
33+
if (call[0].includes('0% compiling')) {
34+
foundProgress = true;
35+
}
36+
37+
// this is an indicator that the profile option is enabled,
38+
// so we should expect to find it in stderr since profile is enabled
39+
if (call[0].includes('ms after chunk modules optimization')) {
40+
foundProfile = true;
41+
}
42+
});
43+
expect(foundProgress).toBeTruthy();
44+
expect(foundProfile).toBeTruthy();
45+
46+
server.close(done);
47+
});
48+
49+
compiler.run(() => {});
50+
server.listen(port, 'localhost');
51+
});
52+
});
53+
});

test/server/progress-option.test.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
const webpack = require('webpack');
4+
const Server = require('../../lib/Server');
5+
const config = require('../fixtures/simple-config/webpack.config');
6+
const port = require('../ports-map')['progress-option'];
7+
8+
describe('progress', () => {
9+
describe('output', () => {
10+
let mockStderr;
11+
12+
beforeAll(() => {
13+
mockStderr = jest
14+
.spyOn(process.stderr, 'write')
15+
.mockImplementation(() => {});
16+
});
17+
18+
it('should show percentage progress without profile data', (done) => {
19+
const compiler = webpack(config);
20+
const server = new Server(compiler, {
21+
port,
22+
progress: true,
23+
});
24+
25+
compiler.hooks.done.tap('webpack-dev-server', () => {
26+
const calls = mockStderr.mock.calls;
27+
mockStderr.mockRestore();
28+
let foundProgress = false;
29+
let foundProfile = false;
30+
calls.forEach((call) => {
31+
if (call[0].includes('0% compiling')) {
32+
foundProgress = true;
33+
}
34+
35+
// this is an indicator that the profile option is enabled,
36+
// so we should expect to not find it in stderr since it is not enabled
37+
if (call[0].includes('ms after chunk modules optimization')) {
38+
foundProfile = true;
39+
}
40+
});
41+
expect(foundProgress).toBeTruthy();
42+
expect(foundProfile).toBeFalsy();
43+
44+
server.close(done);
45+
});
46+
47+
compiler.run(() => {});
48+
server.listen(port, 'localhost');
49+
});
50+
});
51+
});

0 commit comments

Comments
 (0)