Skip to content

Commit

Permalink
feat: add webpack as argument to before and after options (#1760)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Apr 4, 2019
1 parent 31dfe22 commit 0984d4b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ class Server {
},
before: () => {
if (typeof options.before === 'function') {
options.before(app, this);
options.before(app, this, compiler);
}
},
middleware: () => {
Expand All @@ -507,7 +507,7 @@ class Server {
},
after: () => {
if (typeof options.after === 'function') {
options.after(app, this);
options.after(app, this, compiler);
}
},
headers: () => {
Expand Down
76 changes: 76 additions & 0 deletions test/BeforeAndAfter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict';

const request = require('supertest');
const helper = require('./helper');
const config = require('./fixtures/simple-config/webpack.config');

describe('Before And After options', () => {
let server;
let req;

beforeAll((done) => {
server = helper.start(
config,
{
before: (app, server, compiler) => {
if (!app) {
throw new Error('app is not defined');
}

if (!server) {
throw new Error('server is not defined');
}

if (!compiler) {
throw new Error('compiler is not defined');
}

app.get('/before/some/path', (req, res) => {
res.send('before');
});
},
after: (app, server, compiler) => {
if (!app) {
throw new Error('app is not defined');
}

if (!server) {
throw new Error('server is not defined');
}

if (!compiler) {
throw new Error('compiler is not defined');
}

app.get('/after/some/path', (req, res) => {
res.send('after');
});
},
},
done
);
req = request(server.app);
});

afterAll(helper.close);

it('should handle before route', () => {
return req
.get('/before/some/path')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(200)
.then((response) => {
expect(response.text).toBe('before');
});
});

it('should handle after route', () => {
return req
.get('/after/some/path')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(200)
.then((response) => {
expect(response.text).toBe('after');
});
});
});
12 changes: 11 additions & 1 deletion test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
if (options.quiet === undefined) {
options.quiet = true;
}

// originally, inline was not working by default for tests with the API
// if you need to test inline, it should be set explicitly,
// rather than expecting it to be defaulted to
Expand All @@ -26,19 +27,26 @@ module.exports = {
) {
options.inline = false;
}

// defaulting to this will hopefully help with problems on OSX in tests
if (options.watchOptions === undefined) {
options.watchOptions = {
poll: true,
};
}

const compiler = webpack(config);

server = new Server(compiler, options);

const port = options.port || 8080;
const host = options.host || 'localhost';

server.listen(port, host, (err) => {
if (err) return done(err);
if (err) {
return done(err);
}

done();
});

Expand All @@ -57,9 +65,11 @@ module.exports = {
};

const fullSetup = this.startFullSetup(config, options, ready);

// wait for compilation, since dev server can start before this
// https://github.com/webpack/webpack-dev-server/issues/847
fullSetup.compiler.hooks.done.tap('done', ready);

return fullSetup;
},
startAwaitingCompilation(config, options, done) {
Expand Down

0 comments on commit 0984d4b

Please sign in to comment.