Skip to content

Commit

Permalink
test(Util, Validation): close server each time test ends (#1680)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroppy authored and evilebottnawi committed Feb 22, 2019
1 parent b8d5c1e commit 66129a8
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 68 deletions.
11 changes: 8 additions & 3 deletions test/Util.test.js
Expand Up @@ -8,11 +8,18 @@ const config = require('./fixtures/simple-config/webpack.config');

describe('check utility functions', () => {
let compiler;
let server;

beforeAll(() => {
compiler = webpack(config);
});

afterEach((done) => {
server.close(() => {
done();
});
});

const tests = [
{
name: 'default',
Expand Down Expand Up @@ -82,7 +89,7 @@ describe('check utility functions', () => {
it(`test createDomain '${test.name}'`, (done) => {
const { options, expected } = test;

const server = new Server(compiler, options);
server = new Server(compiler, options);

server.listen(options.port, options.host, (err) => {
if (err) {
Expand All @@ -96,8 +103,6 @@ describe('check utility functions', () => {
} else {
done();
}

server.close();
});
});
});
Expand Down
150 changes: 85 additions & 65 deletions test/Validation.test.js
Expand Up @@ -16,73 +16,86 @@ describe('Validation', () => {
compiler = webpack(config);
});

afterAll((done) => {
server.close(() => {
done();
describe('validation', () => {
afterEach((done) => {
// `server` is undefined if a test is good
if (server) {
server.close(() => {
done();
});
} else {
done();
}
});
});

const tests = [
{
name: 'invalid `hot` configuration',
config: { hot: 'false' },
message:
'options.hot should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserver-hot)\n',
},
{
name: 'invalid `logLevel` configuration',
config: { logLevel: 1 },
message:
'options.logLevel should be {String} and equal to one of the allowed values',
},
{
name: 'invalid `writeToDisk` configuration',
config: { writeToDisk: 1 },
message:
'options.writeToDisk should be {Boolean|Function} (https://github.com/webpack/webpack-dev-middleware#writetodisk)\n',
},
{
name: 'invalid `overlay` configuration',
config: { overlay: { errors: 1 } },
message:
'options.overlay should be {Object|Boolean} (https://webpack.js.org/configuration/dev-server/#devserver-overlay)\n',
},
{
name: 'invalid `contentBase` configuration',
config: { contentBase: [0] },
message:
'options.contentBase should be {Array} (https://webpack.js.org/configuration/dev-server/#devserver-contentbase)\n',
},
{
name: 'no additional properties',
config: { additional: true },
message: 'options should NOT have additional properties\n',
},
];

tests.forEach((test) => {
it(`should fail validation for ${test.name}`, () => {
try {
// eslint-disable-next-line no-new
server = new Server(compiler, test.config);
} catch (err) {
if (err.name !== 'ValidationError') {
throw err;
}
const tests = [
{
name: 'invalid `hot` configuration',
config: { hot: 'false' },
message:
'options.hot should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserver-hot)\n',
},
{
name: 'invalid `logLevel` configuration',
config: { logLevel: 1 },
message:
'options.logLevel should be {String} and equal to one of the allowed values',
},
{
name: 'invalid `writeToDisk` configuration',
config: { writeToDisk: 1 },
message:
'options.writeToDisk should be {Boolean|Function} (https://github.com/webpack/webpack-dev-middleware#writetodisk)\n',
},
{
name: 'invalid `overlay` configuration',
config: { overlay: { errors: 1 } },
message:
'options.overlay should be {Object|Boolean} (https://webpack.js.org/configuration/dev-server/#devserver-overlay)\n',
},
{
name: 'invalid `contentBase` configuration',
config: { contentBase: [0] },
message:
'options.contentBase should be {Array} (https://webpack.js.org/configuration/dev-server/#devserver-contentbase)\n',
},
{
name: 'no additional properties',
config: { additional: true },
message: 'options should NOT have additional properties\n',
},
];

tests.forEach((test) => {
it(`should fail validation for ${test.name}`, () => {
try {
// eslint-disable-next-line no-new
server = new Server(compiler, test.config);
} catch (err) {
if (err.name !== 'ValidationError') {
throw err;
}

const [title, message] = err.message.split('\n\n');
const [title, message] = err.message.split('\n\n');

expect(title).toEqual('webpack Dev Server Invalid Options');
expect(message).toEqual(test.message);
expect(title).toEqual('webpack Dev Server Invalid Options');
expect(message).toEqual(test.message);

return;
}
return;
}

throw new Error("Validation didn't fail");
throw new Error("Validation didn't fail");
});
});
});

describe('filename', () => {
afterEach((done) => {
server.close(() => {
done();
});
});

it('should allow filename to be a function', () => {
try {
// eslint-disable-next-line no-new
Expand All @@ -98,6 +111,12 @@ describe('Validation', () => {
});

describe('checkHost', () => {
afterEach((done) => {
server.close(() => {
done();
});
});

it('should always allow any host if options.disableHostCheck is set', () => {
const options = {
public: 'test.host:80',
Expand All @@ -108,7 +127,7 @@ describe('Validation', () => {
host: 'bad.host',
};

const server = new Server(compiler, options);
server = new Server(compiler, options);

if (!server.checkHost(headers)) {
throw new Error("Validation didn't fail");
Expand All @@ -122,7 +141,7 @@ describe('Validation', () => {
const headers = {
host: 'localhost',
};
const server = new Server(compiler, options);
server = new Server(compiler, options);
if (!server.checkHost(headers)) {
throw new Error("Validation didn't fail");
}
Expand All @@ -137,7 +156,7 @@ describe('Validation', () => {
host: '127.0.0.1',
};

const server = new Server(compiler, options);
server = new Server(compiler, options);

if (!server.checkHost(headers)) {
throw new Error("Validation didn't fail");
Expand All @@ -156,7 +175,7 @@ describe('Validation', () => {
'[ad42::1de2:54c2:c2fa:1234]:8080',
];

const server = new Server(compiler, options);
server = new Server(compiler, options);

tests.forEach((test) => {
const headers = { host: test };
Expand All @@ -176,7 +195,7 @@ describe('Validation', () => {
host: 'test.hostname:80',
};

const server = new Server(compiler, options);
server = new Server(compiler, options);

if (server.checkHost(headers)) {
throw new Error("Validation didn't fail");
Expand All @@ -190,7 +209,7 @@ describe('Validation', () => {
const headers = {
origin: 'https://test.host',
};
const server = new Server(compiler, options);
server = new Server(compiler, options);
if (!server.checkOrigin(headers)) {
throw new Error("Validation didn't fail");
}
Expand All @@ -200,17 +219,18 @@ describe('Validation', () => {
it('should allow hosts in allowedHosts', () => {
const tests = ['test.host', 'test2.host', 'test3.host'];
const options = { allowedHosts: tests };
const server = new Server(compiler, options);
server = new Server(compiler, options);
tests.forEach((test) => {
const headers = { host: test };
if (!server.checkHost(headers)) {
throw new Error("Validation didn't fail");
}
});
});

it('should allow hosts that pass a wildcard in allowedHosts', () => {
const options = { allowedHosts: ['.example.com'] };
const server = new Server(compiler, options);
server = new Server(compiler, options);
const tests = [
'www.example.com',
'subdomain.example.com',
Expand Down

0 comments on commit 66129a8

Please sign in to comment.