From cd218ef963e5f9b898fcb698da0174d5f6128f79 Mon Sep 17 00:00:00 2001 From: Eslam El-Hakmey Date: Fri, 31 May 2019 16:38:17 +0200 Subject: [PATCH] feat(server): add callback support for invalidate (#1900) --- lib/Server.js | 4 ++-- test/InvalidateCallback.test.js | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 test/InvalidateCallback.test.js diff --git a/lib/Server.js b/lib/Server.js index 01173d119c..8eebe35578 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -980,9 +980,9 @@ class Server { this.contentBaseWatchers.push(watcher); } - invalidate() { + invalidate(callback) { if (this.middleware) { - this.middleware.invalidate(); + this.middleware.invalidate(callback); } } } diff --git a/test/InvalidateCallback.test.js b/test/InvalidateCallback.test.js new file mode 100644 index 0000000000..c95781f24e --- /dev/null +++ b/test/InvalidateCallback.test.js @@ -0,0 +1,41 @@ +'use strict'; + +const webpack = require('webpack'); +const { noop } = require('webpack-dev-middleware/lib/util'); +const Server = require('../lib/Server'); +const config = require('./fixtures/simple-config/webpack.config'); + +describe('Invalidate Callback', () => { + describe('Testing callback functions on calling invalidate without callback', () => { + it('should be `noop` (the default callback function)', (done) => { + const compiler = webpack(config); + const server = new Server(compiler); + + server.invalidate(); + expect(server.middleware.context.callbacks[0]).toBe(noop); + + compiler.hooks.done.tap('webpack-dev-server', () => { + server.close(done); + }); + + compiler.run(() => {}); + }); + }); + + describe('Testing callback functions on calling invalidate with callback', () => { + it('should be `callback` function', (done) => { + const compiler = webpack(config); + const callback = jest.fn(); + const server = new Server(compiler); + server.invalidate(callback); + + expect(server.middleware.context.callbacks[0]).toBe(callback); + + compiler.hooks.done.tap('webpack-dev-server', () => { + server.close(done); + }); + + compiler.run(() => {}); + }); + }); +});