Skip to content

Commit

Permalink
Remove --forceExit antipattern by closing all servers in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nickmccurdy committed Sep 9, 2017
1 parent 6b3f8ea commit 31b6f42
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion lib/__tests__/basic-auth.js
Expand Up @@ -8,7 +8,7 @@ describe('Basic Auth', () => {
this.body = this.request.basicAuth;
});

return request(app.listen())
return request(app.callback())
.get('/')
.auth('username', 'password')
.expect(200)
Expand Down
22 changes: 11 additions & 11 deletions lib/__tests__/body-parsing.js
Expand Up @@ -8,7 +8,7 @@ describe('Body Parsing', () => {
app.use(function * () {
this.body = yield * this.request.json();
});
return request(app.listen())
return request(app.callback())
.post('/')
.send({
message: 'lol'
Expand All @@ -23,7 +23,7 @@ describe('Body Parsing', () => {
app.use(function * () {
this.body = yield * this.request.json();
});
return request(app.listen())
return request(app.callback())
.post('/')
.type('json')
.send('"lol"')
Expand All @@ -36,7 +36,7 @@ describe('Body Parsing', () => {
app.use(function * () {
this.body = yield * this.request.json();
});
return request(app.listen())
return request(app.callback())
.post('/')
.type('json')
.send('"lol"')
Expand All @@ -51,7 +51,7 @@ describe('Body Parsing', () => {
app.use(function * () {
this.body = yield * this.request.urlencoded();
});
return request(app.listen())
return request(app.callback())
.post('/')
.send('message=lol')
.expect(200)
Expand All @@ -64,7 +64,7 @@ describe('Body Parsing', () => {
app.use(function * () {
this.body = yield * this.request.urlencoded();
});
return request(app.listen())
return request(app.callback())
.post('/')
.type('form')
.send({
Expand All @@ -83,7 +83,7 @@ describe('Body Parsing', () => {
app.use(function * () {
this.body = yield * this.request.urlencoded();
});
return request(app.listen())
return request(app.callback())
.post('/')
.type('form')
.send({
Expand All @@ -107,7 +107,7 @@ describe('Body Parsing', () => {
this.body = yield * this.request.text();
expect(typeof this.body).toBe('string');
});
return request(app.listen())
return request(app.callback())
.post('/')
.send('message=lol')
.expect(200)
Expand All @@ -120,7 +120,7 @@ describe('Body Parsing', () => {
yield * this.request.text('1kb');
this.body = 204;
});
return request(app.listen())
return request(app.callback())
.post('/')
.send(Buffer.alloc(2048))
.expect(413);
Expand All @@ -134,7 +134,7 @@ describe('Body Parsing', () => {
this.body = yield * this.request.buffer();
expect(Buffer.isBuffer(this.body)).toBeTruthy();
});
return request(app.listen())
return request(app.callback())
.post('/')
.send('message=lol')
.expect(200)
Expand All @@ -147,7 +147,7 @@ describe('Body Parsing', () => {
yield * this.request.buffer('1kb');
this.body = 204;
});
return request(app.listen())
return request(app.callback())
.post('/')
.send(Buffer.alloc(2048))
.expect(413);
Expand All @@ -160,7 +160,7 @@ describe('Body Parsing', () => {
app.use(function * () {
this.body = yield * this.request.json();
});
return request(app.listen())
return request(app.callback())
.get('/')
.set('expect', '100-continue')
.set('content-type', 'application/json')
Expand Down
16 changes: 8 additions & 8 deletions lib/__tests__/cache-control.js
Expand Up @@ -10,7 +10,7 @@ describe('Cache-Control', () => {
this.status = 204;
});

return request(app.listen())
return request(app.callback())
.get('/')
.expect(204)
.expect('Cache-Control', 'public, max-age=1');
Expand All @@ -23,7 +23,7 @@ describe('Cache-Control', () => {
this.status = 204;
});

return request(app.listen())
return request(app.callback())
.get('/')
.expect(204)
.expect('Cache-Control', 'public, max-age=1');
Expand All @@ -36,7 +36,7 @@ describe('Cache-Control', () => {
this.status = 204;
});

return request(app.listen())
return request(app.callback())
.get('/')
.expect(204)
.expect('Cache-Control', 'public, max-age=1');
Expand All @@ -51,7 +51,7 @@ describe('Cache-Control', () => {
this.status = 204;
});

return request(app.listen())
return request(app.callback())
.get('/')
.expect(204)
.expect('Cache-Control', 'public, max-age=1000');
Expand All @@ -66,7 +66,7 @@ describe('Cache-Control', () => {
this.status = 204;
});

return request(app.listen())
return request(app.callback())
.get('/')
.expect(204)
.expect('Cache-Control', 'public, max-age=3600');
Expand All @@ -81,7 +81,7 @@ describe('Cache-Control', () => {
this.status = 204;
});

return request(app.listen())
return request(app.callback())
.get('/')
.expect(204)
.expect('Cache-Control', 'private, no-cache');
Expand All @@ -96,7 +96,7 @@ describe('Cache-Control', () => {
this.status = 204;
});

return request(app.listen())
return request(app.callback())
.get('/')
.expect(204)
.expect('Cache-Control', 'foo');
Expand All @@ -111,7 +111,7 @@ describe('Cache-Control', () => {
this.status = 204;
});

return request(app.listen())
return request(app.callback())
.get('/')
.expect(500);
});
Expand Down
2 changes: 1 addition & 1 deletion lib/__tests__/conditional-get.js
Expand Up @@ -10,7 +10,7 @@ describe('Conditional-Get', () => {
this.body = 'hello';
});

const server = app.listen();
const server = app.listen().close();

test('should set an etag', () => {
return request(server)
Expand Down
20 changes: 10 additions & 10 deletions lib/__tests__/headers.js
Expand Up @@ -6,7 +6,7 @@ describe('Set headers', () => {
test('should get X-Response-Time correctly by default', () => {
const app = koala();

return request(app.listen())
return request(app.callback())
.get('/')
.expect(404)
.expect('X-Response-Time', /s$/);
Expand All @@ -18,7 +18,7 @@ describe('Set headers', () => {
responseTime: false
});

return request(app.listen())
return request(app.callback())
.get('/')
.expect(404)
.expect(res => {
Expand All @@ -32,7 +32,7 @@ describe('Set headers', () => {
test('should not set Strict-Transport-Security by default', () => {
const app = koala();

return request(app.listen())
return request(app.callback())
.get('/')
.expect(404)
.expect(res => {
Expand All @@ -46,7 +46,7 @@ describe('Set headers', () => {
}
});

return request(app.listen())
return request(app.callback())
.get('/')
.expect(404)
.expect('Strict-Transport-Security', 'max-age=1');
Expand All @@ -60,7 +60,7 @@ describe('Set headers', () => {
}
});

return request(app.listen())
return request(app.callback())
.get('/')
.expect(404)
.expect('Strict-Transport-Security', 'max-age=1');
Expand All @@ -73,7 +73,7 @@ describe('Set headers', () => {
}
});

return request(app.listen())
return request(app.callback())
.get('/')
.expect(404)
.expect('Strict-Transport-Security', 'max-age=1; includeSubDomains');
Expand All @@ -84,7 +84,7 @@ describe('Set headers', () => {
test('should get X-Frame-Options DENY by default', () => {
const app = koala();

return request(app.listen())
return request(app.callback())
.get('/')
.expect(404)
.expect('X-Frame-Options', 'DENY');
Expand All @@ -96,7 +96,7 @@ describe('Set headers', () => {
}
});

return request(app.listen())
return request(app.callback())
.get('/')
.expect(404)
.expect(res => {
Expand All @@ -110,7 +110,7 @@ describe('Set headers', () => {
}
});

return request(app.listen())
return request(app.callback())
.get('/')
.expect(404)
.expect('X-Frame-Options', 'DENY');
Expand All @@ -122,7 +122,7 @@ describe('Set headers', () => {
}
});

return request(app.listen())
return request(app.callback())
.get('/')
.expect(404)
.expect('X-Frame-Options', 'SAMEORIGIN');
Expand Down
4 changes: 2 additions & 2 deletions lib/__tests__/jsonp.js
Expand Up @@ -12,7 +12,7 @@ describe('jsonp', () => {
this.jsonp = {foo: 'bar'};
});

return request(app.listen())
return request(app.callback())
.get('/user.json?_callback=fn')
.expect(200)
.expect('/**/ typeof fn === \'function\' && fn({"foo":"bar"});');
Expand All @@ -28,7 +28,7 @@ describe('jsonp', () => {
this.jsonp = {foo: 'bar'};
});

return request(app.listen())
return request(app.callback())
.get('/user.json')
.expect(200)
.expect({'foo': 'bar'});
Expand Down
2 changes: 1 addition & 1 deletion lib/__tests__/object-streams.js
Expand Up @@ -21,7 +21,7 @@ describe('Object Streams', () => {
body.end();
});

return request(app.listen())
return request(app.callback())
.get('/')
.expect(200)
.expect([{
Expand Down
4 changes: 2 additions & 2 deletions lib/__tests__/query-string.js
Expand Up @@ -9,7 +9,7 @@ describe('Nested Query Strings', () => {
this.response.body = this.request.query;
});

return request(app.listen())
return request(app.callback())
.get('/')
.query({
something: {
Expand All @@ -32,7 +32,7 @@ describe('Nested Query Strings', () => {
this.response.body = this.request.query;
});

return request(app.listen())
return request(app.callback())
.get('/')
.query({
something: {
Expand Down
4 changes: 2 additions & 2 deletions lib/middleware/__tests__/middleware.js
Expand Up @@ -10,7 +10,7 @@ describe('Middlewares', () => {
this.body = this.session;
});

return request(app.listen())
return request(app.callback())
.get('/')
.expect('{}');
});
Expand All @@ -23,7 +23,7 @@ describe('Middlewares', () => {
this.body = this.session === undefined;
});

return request(app.listen())
return request(app.callback())
.get('/')
.expect('true');
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -57,7 +57,7 @@
"template"
],
"scripts": {
"tests": "jest --forceExit",
"tests": "jest",
"test": "npm-run-all lint tests",
"snyk-protect": "snyk protect",
"prepare": "npm run snyk-protect",
Expand Down

0 comments on commit 31b6f42

Please sign in to comment.