1 const ServerTestUtil = {
2 server_: null,
3 app_: null,
4
5 setApp: function (app) {
6 this.app_ = app
7 },
8
9 start: async function () {
10 this.server_ = await new Promise((resolve, reject) => {
11 const s = this.app_.listen(PORT, (err) => {
12 if (err) {
13 reject(err)
14 }
15 })
16 resolve(s)
17 })
18 },
19
20 kill: function () {
21 return new Promise((resolve, _) => {
22 if (this.server_) {
23 this.server_.close(() => resolve())
24 }
25 resolve()
26 })
27 },
28 }
29
30 const instance = ServerTestUtil
31
32 module.exports = {
33 setApp: function (app) {
34 instance.setApp(app)
35 return instance
36 },
37
38 useServer: function () {
39 return instance
40 },
41 }