Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): add callback support for invalidate #1900

Merged
merged 4 commits into from May 31, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/Server.js
Expand Up @@ -1003,9 +1003,9 @@ class Server {
this.contentBaseWatchers.push(watcher);
}

invalidate() {
invalidate(callback) {
if (this.middleware) {
this.middleware.invalidate();
this.middleware.invalidate(callback);
}
}
}
Expand Down
46 changes: 46 additions & 0 deletions test/Server.test.js
Expand Up @@ -2,6 +2,7 @@

const { relative, sep } = require('path');
const webpack = require('webpack');
const { noop } = require('webpack-dev-middleware/lib/util');
const request = require('supertest');
// Mock opn before loading Server
jest.mock('opn');
Expand Down Expand Up @@ -74,6 +75,51 @@ describe('Server', () => {
});
});

describe('Testing callback functions on calling invalidate without callback', () => {
it('should be `noop` (the default callback function)', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use done because Server.test.js has already used done and can reduce code length.
After all, resolve is called in server.close in the callback, so you don't have to make a Promise.

return new Promise((res) => {
// eslint-disable-next-line
const Server = require('../lib/Server');
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(() => {
res();
});
});

compiler.run(() => {});
});
});
});

describe('Testing callback functions on calling invalidate with callback', () => {
it('should be `callback` function', () => {
return new Promise((res) => {
// eslint-disable-next-line
const Server = require('../lib/Server');
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(() => {
res();
});
});

compiler.run(() => {});
});
});
});

// issue: https://github.com/webpack/webpack-dev-server/issues/1724
describe('express.static.mine.types', () => {
beforeEach(() => {
Expand Down