Skip to content

Commit eec5a8f

Browse files
author
Ian Walter
committed
Adding server-destroy
1 parent 8488806 commit eec5a8f

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

index.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const http = require('http')
22
const Koa = require('koa')
33
const json = require('koa-json')
44
const log = require('@ianwalter/log')
5+
const enableDestroy = require('server-destroy')
56

67
const defaultOptions = { cors: false }
78

@@ -36,18 +37,14 @@ module.exports = function createTestServer (options = defaultOptions) {
3637
// it receives.
3738
const server = http.createServer(app.callback())
3839

40+
// Add a destroy method to the server instance.
41+
// https://github.com/nodejs/node/issues/2642
42+
enableDestroy(server)
43+
3944
// Add a close method to the Koa app to allow the caller / receiver of the Koa
4045
// app to close the server when done with it.
4146
app.close = function close () {
42-
return new Promise((resolve, reject) => {
43-
server.close(err => {
44-
if (err) {
45-
reject(err)
46-
} else {
47-
resolve(app)
48-
}
49-
})
50-
})
47+
return new Promise(resolve => server.destroy(resolve))
5148
}
5249

5350
// Return the Koa app instance when the server has started listening.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"dependencies": {
3333
"@ianwalter/log": "^2.0.0",
3434
"koa": "^2.7.0",
35-
"koa-json": "^2.0.2"
35+
"koa-json": "^2.0.2",
36+
"server-destroy": "^1.0.1"
3637
},
3738
"devDependencies": {
3839
"@ianwalter/puppeteer-helper": "^3.0.0",

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4819,6 +4819,11 @@ serialize-javascript@^1.4.0:
48194819
resolved "https://registry.yarnpkg.com/serialize-javascript/-/serialize-javascript-1.6.1.tgz#4d1f697ec49429a847ca6f442a2a755126c4d879"
48204820
integrity sha512-A5MOagrPFga4YaKQSWHryl7AXvbQkEqpw4NNYMTNYUNV51bA8ABHgYFpqKx+YFFrw59xMV1qGH1R4AgoNIVgCw==
48214821

4822+
server-destroy@^1.0.1:
4823+
version "1.0.1"
4824+
resolved "https://registry.yarnpkg.com/server-destroy/-/server-destroy-1.0.1.tgz#f13bf928e42b9c3e79383e61cc3998b5d14e6cdd"
4825+
integrity sha1-8Tv5KOQrnD55OD5hzDmYtdFObN0=
4826+
48224827
set-blocking@~2.0.0:
48234828
version "2.0.0"
48244829
resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7"

0 commit comments

Comments
 (0)