Skip to content

Commit 4741e21

Browse files
committed
test(progress): added progress and profile option tests
1 parent 8faed38 commit 4741e21

File tree

4 files changed

+117
-13
lines changed

4 files changed

+117
-13
lines changed

lib/Server.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -153,27 +153,25 @@ class Server {
153153
}
154154

155155
setupProgressPlugin() {
156+
// for CLI output
156157
new webpack.ProgressPlugin({
157158
profile: !!this.options.profile,
158159
}).apply(this.compiler);
159160

160-
const progressPlugin = new webpack.ProgressPlugin(
161-
(percent, msg, addInfo) => {
162-
percent = Math.floor(percent * 100);
161+
// for browser console output
162+
new webpack.ProgressPlugin((percent, msg, addInfo) => {
163+
percent = Math.floor(percent * 100);
163164

164-
if (percent === 100) {
165-
msg = 'Compilation completed';
166-
}
167-
168-
if (addInfo) {
169-
msg = `${msg} (${addInfo})`;
170-
}
165+
if (percent === 100) {
166+
msg = 'Compilation completed';
167+
}
171168

172-
this.sockWrite(this.sockets, 'progress-update', { percent, msg });
169+
if (addInfo) {
170+
msg = `${msg} (${addInfo})`;
173171
}
174-
);
175172

176-
progressPlugin.apply(this.compiler);
173+
this.sockWrite(this.sockets, 'progress-update', { percent, msg });
174+
}).apply(this.compiler);
177175
}
178176

179177
setupApp() {

test/ports-map.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ const portsList = {
4141
ClientMode: 1,
4242
'clientMode-option': 1,
4343
Progress: 1,
44+
'progress-option': 1,
45+
'profile-option': 1,
4446
};
4547

4648
let startPort = 8079;

test/server/profile-option.test.js

Lines changed: 53 additions & 0 deletions
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

Lines changed: 51 additions & 0 deletions
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)