Skip to content

Commit aaf7c41

Browse files
committed
Fix tests for #48
1 parent 27cfbbf commit aaf7c41

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"@hapi/hapi": "^18.4.1",
3131
"@hapi/hoek": "^9.1.0",
3232
"@hapi/lab": "^24.1.0",
33-
"bluebird": "3.x.x"
33+
"bluebird": "3.x.x",
34+
"sinon": "^11.1.1"
3435
},
3536
"peerDependencies": {
3637
"@hapi/hapi": ">= 18.0.0"

test/connection.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const Hapi = require('@hapi/hapi');
44
const Hoek = require('@hapi/hoek');
55
const Lab = require('@hapi/lab');
66
const Mongodb = require('mongodb');
7+
const Sinon = require('sinon');
78

89
const { describe, it, beforeEach, expect } = exports.lab = Lab.script();
910

@@ -314,17 +315,48 @@ describe('Hapi server', () => {
314315
it('should disconnect if the server stops', async () => {
315316

316317
await server.register({
317-
plugin: require('../'),
318-
options: {
319-
settings: {
320-
promiseLibrary: 'bluebird'
321-
}
322-
}
318+
plugin: require('../')
323319
});
324320

325321
await server.initialize();
322+
323+
expect(server.plugins['hapi-mongodb'].client.isConnected()).to.be.true();
324+
326325
await server.stop();
327326
await Hoek.wait(100); // Let the connections end.
327+
328+
expect(server.plugins['hapi-mongodb'].client.isConnected()).to.be.false();
329+
});
330+
331+
it('should logs errors on disconnect', async () => {
332+
333+
const logEntries = [];
334+
server.events.on('log', (entry) => {
335+
336+
logEntries.push(entry);
337+
});
338+
339+
await server.register({
340+
plugin: require('../')
341+
});
342+
343+
await server.initialize();
344+
345+
expect(server.plugins['hapi-mongodb'].client.isConnected()).to.be.true();
346+
const closeStub = Sinon.stub(server.plugins['hapi-mongodb'].client, 'close').callsFake((cb) => {
347+
348+
setTimeout(cb, 0, new Error('Oops'));
349+
});
350+
351+
await server.stop();
352+
await Hoek.wait(100); // Let the connections end.
353+
354+
closeStub.restore();
355+
await server.plugins['hapi-mongodb'].client.close();
356+
357+
expect(logEntries).to.have.length(2);
358+
expect(logEntries[1].tags).to.equal(['hapi-mongodb', 'error']);
359+
expect(logEntries[1].error).to.be.an.error('Oops');
328360
});
329361

330362
it('should be able to find the plugin exposed objects', async () => {

0 commit comments

Comments
 (0)