Skip to content

Commit 0130a1a

Browse files
committed
test: clean up any event listeners
Latest mocha no longer kills the process at the end. So we have to clean up properly.
1 parent 9e2ae8b commit 0130a1a

12 files changed

+55
-20
lines changed

test/accountSpec.js

+4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ describe('Accounts', function () {
6565
])
6666
})
6767

68+
afterEach(function () {
69+
appHelper.close(this)
70+
})
71+
6872
describe('GET /accounts', function () {
6973
it('should return 200', async function () {
7074
const account1 = this.adminAccount

test/fulfillmentSpec.js

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ describe('GET /fulfillment', function () {
4949
afterEach(async function () {
5050
nock.cleanAll()
5151
this.clock.restore()
52+
appHelper.close(this)
5253
})
5354

5455
it('should return 401 if the request is not authenticated', async function () {
@@ -240,6 +241,7 @@ describe('GET /fulfillment2', function () {
240241
afterEach(async function () {
241242
nock.cleanAll()
242243
this.clock.restore()
244+
appHelper.close(this)
243245
})
244246

245247
it('should return 401 if the request is not authenticated', async function () {
@@ -429,6 +431,7 @@ describe('PUT /fulfillment', function () {
429431
afterEach(async function () {
430432
nock.cleanAll()
431433
this.clock.restore()
434+
appHelper.close(this)
432435
})
433436

434437
it('should return 401 if the request is not authenticated', async function () {
@@ -682,6 +685,7 @@ describe('PUT /fulfillment2', function () {
682685
afterEach(async function () {
683686
nock.cleanAll()
684687
this.clock.restore()
688+
appHelper.close(this)
685689
})
686690

687691
it('should return 401 if the request is not authenticated', async function () {

test/getAuthTokenSpec.js

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ describe('GET /auth_token', function () {
2525
await dbHelper.addAccounts(_.values(_.omit(accounts, 'noBalance')))
2626
})
2727

28+
afterEach(function () {
29+
appHelper.close(this)
30+
})
31+
2832
it('returns 200 and a token on success', async function () {
2933
await this.request()
3034
.get('/auth_token')

test/getTransferSpec.js

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ describe('GET /transfers/:uuid', function () {
5050
afterEach(async function () {
5151
nock.cleanAll()
5252
this.clock.restore()
53+
appHelper.close(this)
5354
})
5455

5556
it('should return 200 for an existing transfer', async function () {

test/healthSpec.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,27 @@ const logger = require('../src/services/log')
1010
const logHelper = require('./helpers/log')
1111
const expect = require('chai').expect
1212

13-
function request () {
14-
return superagent(app.koa.listen())
15-
}
16-
1713
describe('Health', function () {
1814
logHelper(logger)
1915

20-
before(function * () {
21-
yield dbHelper.init()
22-
yield dbHelper.clean()
23-
yield dbHelper.addAccounts([
16+
before(async function () {
17+
await dbHelper.init()
18+
await dbHelper.clean()
19+
await dbHelper.addAccounts([
2420
accounts.admin
2521
])
22+
23+
this.server = app.koa.listen()
24+
this.request = () => superagent(this.server)
25+
})
26+
27+
after(function () {
28+
this.server.close()
2629
})
2730

2831
describe('GET /health', function () {
2932
it('should return 200 for an authenticated request', async function () {
30-
await request()
33+
await this.request()
3134
.get('/health')
3235
.auth('admin', 'admin')
3336
.expect(200)
@@ -38,11 +41,10 @@ describe('Health', function () {
3841
})
3942
})
4043

41-
it('should return 401 for an unauthenticated request', function * () {
42-
yield request()
44+
it('should return 401 for an unauthenticated request', async function () {
45+
await this.request()
4346
.get('/health')
4447
.expect(401)
45-
.end()
4648
})
4749
})
4850
})

test/helpers/app.js

+4
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,7 @@ exports.create = function (context, app) {
8787
return new WebSocket(processedUri, protocols, options)
8888
}
8989
}
90+
91+
exports.close = function (context) {
92+
context.server.close()
93+
}

test/metadataSpec.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,24 @@ const app = require('../src/services/app')
99
const logger = require('../src/services/log')
1010
const logHelper = require('./helpers/log')
1111

12-
function request () {
13-
return superagent(app.koa.listen())
14-
}
15-
1612
describe('Metadata', function () {
1713
logHelper(logger)
1814

15+
before(function () {
16+
this.server = app.koa.listen()
17+
this.request = () => superagent(this.server)
18+
})
19+
20+
after(function () {
21+
this.server.close()
22+
})
23+
1924
delete process.env.LEDGER_AMOUNT_PRECISION
2025
delete process.env.UNIT_TEST_OVERRIDE
2126

2227
describe('GET /', function () {
2328
it('should return metadata', async function () {
24-
await request()
29+
await this.request()
2530
.get('/')
2631
.expect(200)
2732
.expect(function (res) {
@@ -66,7 +71,8 @@ describe('Metadata', function () {
6671
timerWorker: require('../src/services/timerWorker'),
6772
notificationBroadcaster: require('../src/services/notificationBroadcaster')
6873
})
69-
const agent = superagent(newApp.koa.listen())
74+
const server = newApp.koa.listen()
75+
const agent = superagent(server)
7076

7177
await agent
7278
.get('/')
@@ -105,6 +111,8 @@ describe('Metadata', function () {
105111
delete process.env.LEDGER_CURRENCY_CODE
106112
delete process.env.LEDGER_CURRENCY_SYMBOL
107113
delete process.env.LEDGER_ILP_PREFIX
114+
115+
server.close()
108116
})
109117
})
110118
})

test/notificationSpec.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ describe('Notifications', function () {
7171

7272
afterEach(function () {
7373
this.clock.restore()
74+
appHelper.close(this)
7475
})
7576

7677
describe('GET /websocket as alice (regular user)', function () {
@@ -1019,11 +1020,14 @@ describe('Notifications', function () {
10191020
resolve()
10201021
})
10211022
})
1023+
this.socket.terminate()
10221024
})
10231025

1024-
it('closes the websocket if an invalid token is passed', function (done) {
1026+
it('closes the websocket if an invalid token is passed', async function () {
10251027
this.socket = this.ws('http://localhost/websocket?token=foo', {})
1026-
this.socket.on('close', () => done())
1028+
await new Promise(resolve => {
1029+
this.socket.on('close', resolve)
1030+
})
10271031
})
10281032
})
10291033
})

test/postMessageSpec.js

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ describe('POST /messages', function () {
5757

5858
afterEach(async function () {
5959
this.socket.terminate()
60+
appHelper.close(this)
6061
})
6162

6263
it('returns 201 if the message is valid', async function () {

test/putTransferSpec.js

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ describe('PUT /transfers/:id', function () {
7373
afterEach(async function () {
7474
nock.cleanAll()
7575
this.clock.restore()
76+
appHelper.close(this)
7677
})
7778

7879
/* Invalid transfer objects */

test/rejectionSpec.js

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ describe('PUT /rejection', function () {
5757
afterEach(async function () {
5858
nock.cleanAll()
5959
this.clock.restore()
60+
appHelper.close(this)
6061
})
6162

6263
it('should return 401 if the request is not authenticated', async function () {

test/transferStateSpec.js

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ describe('Transfer State', function () {
5050

5151
afterEach(async function () {
5252
this.clock.restore()
53+
appHelper.close(this)
5354
})
5455

5556
describe('GET /transfers/:uuid/state', function () {

0 commit comments

Comments
 (0)