From cbd2450182069725750da25de0934b19ff77eaf6 Mon Sep 17 00:00:00 2001 From: Kirill Nagaitsev Date: Mon, 24 Jun 2019 11:41:11 -0500 Subject: [PATCH] test(progress): added progress and profile option tests --- lib/Server.js | 24 ++++++------- test/ports-map.js | 2 ++ test/server/profile-option.test.js | 53 +++++++++++++++++++++++++++++ test/server/progress-option.test.js | 51 +++++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 13 deletions(-) create mode 100644 test/server/profile-option.test.js create mode 100644 test/server/progress-option.test.js diff --git a/lib/Server.js b/lib/Server.js index ddf72ff860..085b36f1bb 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -144,27 +144,25 @@ class Server { } setupProgressPlugin() { + // for CLI output new webpack.ProgressPlugin({ profile: !!this.options.profile, }).apply(this.compiler); - const progressPlugin = new webpack.ProgressPlugin( - (percent, msg, addInfo) => { - percent = Math.floor(percent * 100); + // for browser console output + new webpack.ProgressPlugin((percent, msg, addInfo) => { + percent = Math.floor(percent * 100); - if (percent === 100) { - msg = 'Compilation completed'; - } - - if (addInfo) { - msg = `${msg} (${addInfo})`; - } + if (percent === 100) { + msg = 'Compilation completed'; + } - this.sockWrite(this.sockets, 'progress-update', { percent, msg }); + if (addInfo) { + msg = `${msg} (${addInfo})`; } - ); - progressPlugin.apply(this.compiler); + this.sockWrite(this.sockets, 'progress-update', { percent, msg }); + }).apply(this.compiler); } setupApp() { diff --git a/test/ports-map.js b/test/ports-map.js index 6281122672..3f0fd27244 100644 --- a/test/ports-map.js +++ b/test/ports-map.js @@ -38,6 +38,8 @@ const portsList = { WebsocketClient: 1, WebsocketServer: 1, Progress: 1, + 'progress-option': 1, + 'profile-option': 1, }; let startPort = 8079; diff --git a/test/server/profile-option.test.js b/test/server/profile-option.test.js new file mode 100644 index 0000000000..5806a57e3a --- /dev/null +++ b/test/server/profile-option.test.js @@ -0,0 +1,53 @@ +'use strict'; + +const webpack = require('webpack'); +const Server = require('../../lib/Server'); +const config = require('../fixtures/simple-config/webpack.config'); +const port = require('../ports-map')['progress-option']; + +describe('profile', () => { + describe('output', () => { + let mockStderr; + + beforeAll(() => { + mockStderr = jest + .spyOn(process.stderr, 'write') + .mockImplementation(() => {}); + }); + + it('should show percentage progress with profile data', (done) => { + const compiler = webpack(config); + const server = new Server(compiler, { + port, + // profile will only have an effect when progress is enabled + progress: true, + profile: true, + }); + + compiler.hooks.done.tap('webpack-dev-server', () => { + const calls = mockStderr.mock.calls; + mockStderr.mockRestore(); + let foundProgress = false; + let foundProfile = false; + calls.forEach((call) => { + if (call[0].includes('0% compiling')) { + foundProgress = true; + } + + // this is an indicator that the profile option is enabled, + // so we should expect to find it in stderr since profile is enabled + if (call[0].includes('ms after chunk modules optimization')) { + foundProfile = true; + } + }); + expect(foundProgress).toBeTruthy(); + expect(foundProfile).toBeTruthy(); + + server.close(done); + }); + + compiler.run(() => {}); + server.listen(port, 'localhost'); + }); + }); +}); diff --git a/test/server/progress-option.test.js b/test/server/progress-option.test.js new file mode 100644 index 0000000000..ae2d23c07f --- /dev/null +++ b/test/server/progress-option.test.js @@ -0,0 +1,51 @@ +'use strict'; + +const webpack = require('webpack'); +const Server = require('../../lib/Server'); +const config = require('../fixtures/simple-config/webpack.config'); +const port = require('../ports-map')['progress-option']; + +describe('progress', () => { + describe('output', () => { + let mockStderr; + + beforeAll(() => { + mockStderr = jest + .spyOn(process.stderr, 'write') + .mockImplementation(() => {}); + }); + + it('should show percentage progress without profile data', (done) => { + const compiler = webpack(config); + const server = new Server(compiler, { + port, + progress: true, + }); + + compiler.hooks.done.tap('webpack-dev-server', () => { + const calls = mockStderr.mock.calls; + mockStderr.mockRestore(); + let foundProgress = false; + let foundProfile = false; + calls.forEach((call) => { + if (call[0].includes('0% compiling')) { + foundProgress = true; + } + + // this is an indicator that the profile option is enabled, + // so we should expect to not find it in stderr since it is not enabled + if (call[0].includes('ms after chunk modules optimization')) { + foundProfile = true; + } + }); + expect(foundProgress).toBeTruthy(); + expect(foundProfile).toBeFalsy(); + + server.close(done); + }); + + compiler.run(() => {}); + server.listen(port, 'localhost'); + }); + }); +});