Skip to content

Commit 66129a8

Browse files
hiroppyevilebottnawi
authored andcommitted
test(Util, Validation): close server each time test ends (#1680)
1 parent b8d5c1e commit 66129a8

File tree

2 files changed

+93
-68
lines changed

2 files changed

+93
-68
lines changed

test/Util.test.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@ const config = require('./fixtures/simple-config/webpack.config');
88

99
describe('check utility functions', () => {
1010
let compiler;
11+
let server;
1112

1213
beforeAll(() => {
1314
compiler = webpack(config);
1415
});
1516

17+
afterEach((done) => {
18+
server.close(() => {
19+
done();
20+
});
21+
});
22+
1623
const tests = [
1724
{
1825
name: 'default',
@@ -82,7 +89,7 @@ describe('check utility functions', () => {
8289
it(`test createDomain '${test.name}'`, (done) => {
8390
const { options, expected } = test;
8491

85-
const server = new Server(compiler, options);
92+
server = new Server(compiler, options);
8693

8794
server.listen(options.port, options.host, (err) => {
8895
if (err) {
@@ -96,8 +103,6 @@ describe('check utility functions', () => {
96103
} else {
97104
done();
98105
}
99-
100-
server.close();
101106
});
102107
});
103108
});

test/Validation.test.js

Lines changed: 85 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -16,73 +16,86 @@ describe('Validation', () => {
1616
compiler = webpack(config);
1717
});
1818

19-
afterAll((done) => {
20-
server.close(() => {
21-
done();
19+
describe('validation', () => {
20+
afterEach((done) => {
21+
// `server` is undefined if a test is good
22+
if (server) {
23+
server.close(() => {
24+
done();
25+
});
26+
} else {
27+
done();
28+
}
2229
});
23-
});
2430

25-
const tests = [
26-
{
27-
name: 'invalid `hot` configuration',
28-
config: { hot: 'false' },
29-
message:
30-
'options.hot should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserver-hot)\n',
31-
},
32-
{
33-
name: 'invalid `logLevel` configuration',
34-
config: { logLevel: 1 },
35-
message:
36-
'options.logLevel should be {String} and equal to one of the allowed values',
37-
},
38-
{
39-
name: 'invalid `writeToDisk` configuration',
40-
config: { writeToDisk: 1 },
41-
message:
42-
'options.writeToDisk should be {Boolean|Function} (https://github.com/webpack/webpack-dev-middleware#writetodisk)\n',
43-
},
44-
{
45-
name: 'invalid `overlay` configuration',
46-
config: { overlay: { errors: 1 } },
47-
message:
48-
'options.overlay should be {Object|Boolean} (https://webpack.js.org/configuration/dev-server/#devserver-overlay)\n',
49-
},
50-
{
51-
name: 'invalid `contentBase` configuration',
52-
config: { contentBase: [0] },
53-
message:
54-
'options.contentBase should be {Array} (https://webpack.js.org/configuration/dev-server/#devserver-contentbase)\n',
55-
},
56-
{
57-
name: 'no additional properties',
58-
config: { additional: true },
59-
message: 'options should NOT have additional properties\n',
60-
},
61-
];
62-
63-
tests.forEach((test) => {
64-
it(`should fail validation for ${test.name}`, () => {
65-
try {
66-
// eslint-disable-next-line no-new
67-
server = new Server(compiler, test.config);
68-
} catch (err) {
69-
if (err.name !== 'ValidationError') {
70-
throw err;
71-
}
31+
const tests = [
32+
{
33+
name: 'invalid `hot` configuration',
34+
config: { hot: 'false' },
35+
message:
36+
'options.hot should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserver-hot)\n',
37+
},
38+
{
39+
name: 'invalid `logLevel` configuration',
40+
config: { logLevel: 1 },
41+
message:
42+
'options.logLevel should be {String} and equal to one of the allowed values',
43+
},
44+
{
45+
name: 'invalid `writeToDisk` configuration',
46+
config: { writeToDisk: 1 },
47+
message:
48+
'options.writeToDisk should be {Boolean|Function} (https://github.com/webpack/webpack-dev-middleware#writetodisk)\n',
49+
},
50+
{
51+
name: 'invalid `overlay` configuration',
52+
config: { overlay: { errors: 1 } },
53+
message:
54+
'options.overlay should be {Object|Boolean} (https://webpack.js.org/configuration/dev-server/#devserver-overlay)\n',
55+
},
56+
{
57+
name: 'invalid `contentBase` configuration',
58+
config: { contentBase: [0] },
59+
message:
60+
'options.contentBase should be {Array} (https://webpack.js.org/configuration/dev-server/#devserver-contentbase)\n',
61+
},
62+
{
63+
name: 'no additional properties',
64+
config: { additional: true },
65+
message: 'options should NOT have additional properties\n',
66+
},
67+
];
68+
69+
tests.forEach((test) => {
70+
it(`should fail validation for ${test.name}`, () => {
71+
try {
72+
// eslint-disable-next-line no-new
73+
server = new Server(compiler, test.config);
74+
} catch (err) {
75+
if (err.name !== 'ValidationError') {
76+
throw err;
77+
}
7278

73-
const [title, message] = err.message.split('\n\n');
79+
const [title, message] = err.message.split('\n\n');
7480

75-
expect(title).toEqual('webpack Dev Server Invalid Options');
76-
expect(message).toEqual(test.message);
81+
expect(title).toEqual('webpack Dev Server Invalid Options');
82+
expect(message).toEqual(test.message);
7783

78-
return;
79-
}
84+
return;
85+
}
8086

81-
throw new Error("Validation didn't fail");
87+
throw new Error("Validation didn't fail");
88+
});
8289
});
8390
});
8491

8592
describe('filename', () => {
93+
afterEach((done) => {
94+
server.close(() => {
95+
done();
96+
});
97+
});
98+
8699
it('should allow filename to be a function', () => {
87100
try {
88101
// eslint-disable-next-line no-new
@@ -98,6 +111,12 @@ describe('Validation', () => {
98111
});
99112

100113
describe('checkHost', () => {
114+
afterEach((done) => {
115+
server.close(() => {
116+
done();
117+
});
118+
});
119+
101120
it('should always allow any host if options.disableHostCheck is set', () => {
102121
const options = {
103122
public: 'test.host:80',
@@ -108,7 +127,7 @@ describe('Validation', () => {
108127
host: 'bad.host',
109128
};
110129

111-
const server = new Server(compiler, options);
130+
server = new Server(compiler, options);
112131

113132
if (!server.checkHost(headers)) {
114133
throw new Error("Validation didn't fail");
@@ -122,7 +141,7 @@ describe('Validation', () => {
122141
const headers = {
123142
host: 'localhost',
124143
};
125-
const server = new Server(compiler, options);
144+
server = new Server(compiler, options);
126145
if (!server.checkHost(headers)) {
127146
throw new Error("Validation didn't fail");
128147
}
@@ -137,7 +156,7 @@ describe('Validation', () => {
137156
host: '127.0.0.1',
138157
};
139158

140-
const server = new Server(compiler, options);
159+
server = new Server(compiler, options);
141160

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

159-
const server = new Server(compiler, options);
178+
server = new Server(compiler, options);
160179

161180
tests.forEach((test) => {
162181
const headers = { host: test };
@@ -176,7 +195,7 @@ describe('Validation', () => {
176195
host: 'test.hostname:80',
177196
};
178197

179-
const server = new Server(compiler, options);
198+
server = new Server(compiler, options);
180199

181200
if (server.checkHost(headers)) {
182201
throw new Error("Validation didn't fail");
@@ -190,7 +209,7 @@ describe('Validation', () => {
190209
const headers = {
191210
origin: 'https://test.host',
192211
};
193-
const server = new Server(compiler, options);
212+
server = new Server(compiler, options);
194213
if (!server.checkOrigin(headers)) {
195214
throw new Error("Validation didn't fail");
196215
}
@@ -200,17 +219,18 @@ describe('Validation', () => {
200219
it('should allow hosts in allowedHosts', () => {
201220
const tests = ['test.host', 'test2.host', 'test3.host'];
202221
const options = { allowedHosts: tests };
203-
const server = new Server(compiler, options);
222+
server = new Server(compiler, options);
204223
tests.forEach((test) => {
205224
const headers = { host: test };
206225
if (!server.checkHost(headers)) {
207226
throw new Error("Validation didn't fail");
208227
}
209228
});
210229
});
230+
211231
it('should allow hosts that pass a wildcard in allowedHosts', () => {
212232
const options = { allowedHosts: ['.example.com'] };
213-
const server = new Server(compiler, options);
233+
server = new Server(compiler, options);
214234
const tests = [
215235
'www.example.com',
216236
'subdomain.example.com',

0 commit comments

Comments
 (0)