diff --git a/lib/Server.js b/lib/Server.js index bac0e72d0a..1f94a2a19c 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -97,10 +97,8 @@ class Server { updateCompiler(compiler, options); - this.stats = - options.stats && Object.keys(options.stats).length - ? options.stats - : Server.DEFAULT_STATS; + this.originalStats = + options.stats && Object.keys(options.stats).length ? options.stats : {}; this.hot = options.hot || options.hotOnly; this.headers = options.headers; @@ -724,7 +722,13 @@ class Server { } getStats(statsObj) { - return statsObj.toJson(this.stats); + const stats = Server.DEFAULT_STATS; + + if (this.originalStats.warningsFilter) { + stats.warningsFilter = this.originalStats.warningsFilter; + } + + return statsObj.toJson(stats); } use() { diff --git a/test/Server.test.js b/test/Server.test.js index 1c4182a39f..b2aa5fe21d 100644 --- a/test/Server.test.js +++ b/test/Server.test.js @@ -6,17 +6,6 @@ const Server = require('../lib/Server'); const config = require('./fixtures/simple-config/webpack.config'); const helper = require('./helper'); -const allStats = [ - {}, - // eslint-disable-next-line no-undefined - undefined, - false, - 'errors-only', - { - assets: false, - }, -]; - describe('Server', () => { // issue: https://github.com/webpack/webpack-dev-server/issues/1724 describe('express.static.mine.types', () => { @@ -67,81 +56,79 @@ describe('Server', () => { }); }); - it('should cascade warningsFilter', () => { - const stats = { warningsFilter: 'test' }; - return new Promise((res) => { - const compiler = webpack(config); - const server = new Server(compiler, { stats }); + describe('stats', () => { + it(`should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors')`, () => { + const allStats = [ + {}, + // eslint-disable-next-line no-undefined + undefined, + false, + 'errors-only', + { + assets: false, + }, + ]; + + return new Promise((resolve, reject) => { + (function iterate(stats, i) { + if (i === allStats.length) { + return resolve(); + } + + // Iterate to cover each case. + Promise.resolve() + .then( + () => + new Promise((res) => { + const compiler = webpack(config); + const server = new Server(compiler, { stats }); + + compiler.hooks.done.tap('webpack-dev-server', (s) => { + expect(Object.keys(server.getStats(s))).toMatchSnapshot(); + + server.close(() => { + res(); + }); + }); + + compiler.run(() => {}); + server.listen(8080, 'localhost'); + }) + ) + .then(() => { + i += 1; + iterate(allStats[i], i); + }) + .catch((e) => { + reject(e); + }); + })(allStats[0], 0); + }); + }); - compiler.hooks.done.tap('webpack-dev-server', (s) => { - s.compilation.warnings = ['test', 'another warning']; + it('should respect warningsFilter', () => { + return new Promise((res) => { + const compiler = webpack(config); + const server = new Server(compiler, { + stats: { warningsFilter: 'test' }, + }); - const output = server.getStats(s); - expect(output.warnings.length).toBe(1); - expect(output.warnings[0]).toBe('another warning'); + compiler.hooks.done.tap('webpack-dev-server', (s) => { + s.compilation.warnings = ['test', 'another warning']; - server.close(() => { - res(); - }); - }); + const output = server.getStats(s); - compiler.run(() => {}); - server.listen(8080, 'localhost'); - }); - }); + expect(output.warnings.length).toBe(1); + expect(output.warnings[0]).toBe('another warning'); - it(`should cascade stats options`, () => { - return new Promise((resolve, reject) => { - (function iterate(stats, i) { - if (i === allStats.length) { - return resolve(); - } - - const prom = new Promise((res, rej) => { - const compiler = webpack(config); - const server = new Server(compiler, { stats }); - - compiler.hooks.done.tap('webpack-dev-server', (s) => { - const finalStats = JSON.stringify(server.getStats(s)); - const defaultStats = JSON.stringify( - server._stats.toJson(Server.DEFAULT_STATS) - ); - - // If we're not over-riding stats configuration, - // we get the same result as the DEFAULT_STATS - if (!stats || !Object.keys(stats).length) { - try { - expect(finalStats).toBe(defaultStats); - } catch (e) { - rej(e); - } - } else { - try { - expect(finalStats).not.toBe(defaultStats); - } catch (e) { - rej(e); - } - } - - server.close(() => { - res(); - }); + server.close(() => { + res(); }); - - compiler.run(() => {}); - server.listen(8080, 'localhost'); }); - // Iterate to cover each case. - prom - .then(() => { - i += 1; - iterate(allStats[i], i); - }) - .catch((e) => { - reject(e); - }); - })(allStats[0], 0); + compiler.run(() => {}); + server.listen(8080, 'localhost'); + }); }); }); diff --git a/test/__snapshots__/Server.test.js.snap b/test/__snapshots__/Server.test.js.snap new file mode 100644 index 0000000000..53f30e0c38 --- /dev/null +++ b/test/__snapshots__/Server.test.js.snap @@ -0,0 +1,56 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Server stats should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors') 1`] = ` +Array [ + "errors", + "warnings", + "hash", + "assetsByChunkName", + "assets", + "filteredAssets", +] +`; + +exports[`Server stats should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors') 2`] = ` +Array [ + "errors", + "warnings", + "hash", + "assetsByChunkName", + "assets", + "filteredAssets", +] +`; + +exports[`Server stats should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors') 3`] = ` +Array [ + "errors", + "warnings", + "hash", + "assetsByChunkName", + "assets", + "filteredAssets", +] +`; + +exports[`Server stats should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors') 4`] = ` +Array [ + "errors", + "warnings", + "hash", + "assetsByChunkName", + "assets", + "filteredAssets", +] +`; + +exports[`Server stats should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors') 5`] = ` +Array [ + "errors", + "warnings", + "hash", + "assetsByChunkName", + "assets", + "filteredAssets", +] +`;