Skip to content

Commit 0984d4b

Browse files
feat: add webpack as argument to before and after options (#1760)
1 parent 31dfe22 commit 0984d4b

File tree

3 files changed

+89
-3
lines changed

3 files changed

+89
-3
lines changed

lib/Server.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ class Server {
497497
},
498498
before: () => {
499499
if (typeof options.before === 'function') {
500-
options.before(app, this);
500+
options.before(app, this, compiler);
501501
}
502502
},
503503
middleware: () => {
@@ -507,7 +507,7 @@ class Server {
507507
},
508508
after: () => {
509509
if (typeof options.after === 'function') {
510-
options.after(app, this);
510+
options.after(app, this, compiler);
511511
}
512512
},
513513
headers: () => {

test/BeforeAndAfter.test.js

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'use strict';
2+
3+
const request = require('supertest');
4+
const helper = require('./helper');
5+
const config = require('./fixtures/simple-config/webpack.config');
6+
7+
describe('Before And After options', () => {
8+
let server;
9+
let req;
10+
11+
beforeAll((done) => {
12+
server = helper.start(
13+
config,
14+
{
15+
before: (app, server, compiler) => {
16+
if (!app) {
17+
throw new Error('app is not defined');
18+
}
19+
20+
if (!server) {
21+
throw new Error('server is not defined');
22+
}
23+
24+
if (!compiler) {
25+
throw new Error('compiler is not defined');
26+
}
27+
28+
app.get('/before/some/path', (req, res) => {
29+
res.send('before');
30+
});
31+
},
32+
after: (app, server, compiler) => {
33+
if (!app) {
34+
throw new Error('app is not defined');
35+
}
36+
37+
if (!server) {
38+
throw new Error('server is not defined');
39+
}
40+
41+
if (!compiler) {
42+
throw new Error('compiler is not defined');
43+
}
44+
45+
app.get('/after/some/path', (req, res) => {
46+
res.send('after');
47+
});
48+
},
49+
},
50+
done
51+
);
52+
req = request(server.app);
53+
});
54+
55+
afterAll(helper.close);
56+
57+
it('should handle before route', () => {
58+
return req
59+
.get('/before/some/path')
60+
.expect('Content-Type', 'text/html; charset=utf-8')
61+
.expect(200)
62+
.then((response) => {
63+
expect(response.text).toBe('before');
64+
});
65+
});
66+
67+
it('should handle after route', () => {
68+
return req
69+
.get('/after/some/path')
70+
.expect('Content-Type', 'text/html; charset=utf-8')
71+
.expect(200)
72+
.then((response) => {
73+
expect(response.text).toBe('after');
74+
});
75+
});
76+
});

test/helper.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = {
1515
if (options.quiet === undefined) {
1616
options.quiet = true;
1717
}
18+
1819
// originally, inline was not working by default for tests with the API
1920
// if you need to test inline, it should be set explicitly,
2021
// rather than expecting it to be defaulted to
@@ -26,19 +27,26 @@ module.exports = {
2627
) {
2728
options.inline = false;
2829
}
30+
2931
// defaulting to this will hopefully help with problems on OSX in tests
3032
if (options.watchOptions === undefined) {
3133
options.watchOptions = {
3234
poll: true,
3335
};
3436
}
37+
3538
const compiler = webpack(config);
39+
3640
server = new Server(compiler, options);
3741

3842
const port = options.port || 8080;
3943
const host = options.host || 'localhost';
44+
4045
server.listen(port, host, (err) => {
41-
if (err) return done(err);
46+
if (err) {
47+
return done(err);
48+
}
49+
4250
done();
4351
});
4452

@@ -57,9 +65,11 @@ module.exports = {
5765
};
5866

5967
const fullSetup = this.startFullSetup(config, options, ready);
68+
6069
// wait for compilation, since dev server can start before this
6170
// https://github.com/webpack/webpack-dev-server/issues/847
6271
fullSetup.compiler.hooks.done.tap('done', ready);
72+
6373
return fullSetup;
6474
},
6575
startAwaitingCompilation(config, options, done) {

0 commit comments

Comments
 (0)