diff --git a/test/collection.capped.test.js b/test/collection.capped.test.js index 5441a5ce968..ddd39600537 100644 --- a/test/collection.capped.test.js +++ b/test/collection.capped.test.js @@ -19,12 +19,15 @@ const Schema = mongoose.Schema; describe('collections: capped:', function() { let db; + const connectionsToClose = []; + before(function() { db = start(); }); after(async function() { await db.close(); + await Promise.all(connectionsToClose.map((v) => v.close())); }); it('schemas should have option size', function() { @@ -52,6 +55,7 @@ describe('collections: capped:', function() { it('skips when setting autoCreate to false (gh-8566)', async function() { const db = start(); + connectionsToClose.push(db); this.timeout(30000); await db.dropDatabase(); diff --git a/test/collection.test.js b/test/collection.test.js index 887ad7f8042..30a98c75289 100644 --- a/test/collection.test.js +++ b/test/collection.test.js @@ -8,8 +8,15 @@ const assert = require('assert'); const mongoose = start.mongoose; describe('collections:', function() { + const connectionsToClose = []; + + after(async function() { + await Promise.all(connectionsToClose.map((v) => v.close())); + }); + it('should buffer commands until connection is established', function(done) { const db = mongoose.createConnection(); + connectionsToClose.push(db); const collection = db.collection('test-buffering-collection'); let connected = false; let insertedId = undefined; @@ -43,6 +50,7 @@ describe('collections:', function() { it('returns a promise if buffering and no callback (gh-7676)', function(done) { const db = mongoose.createConnection(); + connectionsToClose.push(db); const collection = db.collection('gh7676'); const promise = collection.insertOne({ foo: 'bar' }, {}) @@ -145,6 +153,7 @@ describe('collections:', function() { it('buffers for sync methods (gh-10610)', function(done) { const db = mongoose.createConnection(); + connectionsToClose.push(db); const collection = db.collection('gh10610'); collection.find({}, {}, function(err, res) { diff --git a/test/connection.test.js b/test/connection.test.js index 395f3205ffa..fcb78b7fc95 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -56,6 +56,7 @@ describe('connections:', function() { }).asPromise(); assert.strictEqual(conn.config.autoIndex, false); + await conn.close(); }); it('with autoCreate (gh-6489)', async function() { @@ -80,6 +81,7 @@ describe('connections:', function() { const res = await conn.collection('gh6489_Conn'). find({}).sort({ name: 1 }).toArray(); assert.deepEqual(res.map(v => v.name), ['alpha', 'Zeta']); + await conn.close(); }); it('with autoCreate = false (gh-8814)', async function() { @@ -95,6 +97,7 @@ describe('connections:', function() { const res = await conn.db.listCollections().toArray(); assert.ok(!res.map(c => c.name).includes('gh8814_Conn')); + await conn.close(); }); it('autoCreate when collection already exists does not fail (gh-7122)', async function() { @@ -108,6 +111,7 @@ describe('connections:', function() { }, { autoCreate: true }); await conn.model('Actor', schema).init(); + await conn.close(); }); it('throws helpful error with legacy syntax (gh-6756)', function() { @@ -133,9 +137,10 @@ describe('connections:', function() { const _conn = await bootMongo.promise; assert.equal(_conn, conn); + await conn.close(); }); - it('connection plugins (gh-7378)', function() { + it('connection plugins (gh-7378)', async function() { const conn1 = mongoose.createConnection(start.uri); const conn2 = mongoose.createConnection(start.uri); @@ -149,6 +154,8 @@ describe('connections:', function() { conn1.model('Test', schema); assert.equal(called.length, 1); assert.equal(called[0], schema); + await conn1.close(); + await conn2.close(); }); }); @@ -511,7 +518,7 @@ describe('connections:', function() { }). then(() => { assert.ok(session.serverSession.lastUse > lastUse); - conn.close(); + return conn.close(); }); }); @@ -593,6 +600,7 @@ describe('connections:', function() { const nothing2 = await m2.findById(i1.id); assert.strictEqual(null, nothing2); + await db.close(); await db2.close(); }); @@ -752,6 +760,7 @@ describe('connections:', function() { await db.openUri(start.uri); assert.strictEqual(db.client, db2.client); + await db.close(); }); it('closes correctly for all dbs, closing secondary db', function(done) { @@ -764,29 +773,29 @@ describe('connections:', function() { db2.close(); }); - it('cache connections to the same db', function() { + it('cache connections to the same db', function(done) { const db = start(); const db2 = db.useDb(start.databases[1], { useCache: true }); const db3 = db.useDb(start.databases[1], { useCache: true }); assert.strictEqual(db2, db3); - db.close(); + db.close(done); }); }); describe('shouldAuthenticate()', function() { describe('when using standard authentication', function() { describe('when username and password are undefined', function() { - it('should return false', function() { + it('should return false', function(done) { const db = mongoose.createConnection(start.uri, {}); assert.equal(db.shouldAuthenticate(), false); - db.close(); + db.close(done); }); }); describe('when username and password are empty strings', function() { - it('should return false', function() { + it('should return false', function(done) { const db = mongoose.createConnection(start.uri, { user: '', pass: '' @@ -795,7 +804,7 @@ describe('connections:', function() { assert.equal(db.shouldAuthenticate(), false); - db.close(); + db.close(done); }); }); describe('when both username and password are defined', function() { @@ -808,20 +817,20 @@ describe('connections:', function() { assert.equal(db.shouldAuthenticate(), true); - db.close(); + db.close(); // does not actually do anything }); }); }); describe('when using MONGODB-X509 authentication', function() { describe('when username and password are undefined', function() { - it('should return false', function() { + it('should return false', function(done) { const db = mongoose.createConnection(start.uri, {}); db.on('error', function() { }); assert.equal(db.shouldAuthenticate(), false); - db.close(); + db.close(done); }); }); describe('when only username is defined', function() { @@ -833,7 +842,7 @@ describe('connections:', function() { db.asPromise().catch(() => {}); assert.equal(db.shouldAuthenticate(), true); - db.close(); + db.close(); // does not actually do anything }); }); describe('when both username and password are defined', function() { @@ -847,23 +856,24 @@ describe('connections:', function() { assert.equal(db.shouldAuthenticate(), true); - db.close(); + db.close(); // does not actually do anything }); }); }); }); describe('passing a function into createConnection', function() { - it('should store the name of the function (gh-6517)', function() { + it('should store the name of the function (gh-6517)', function(done) { const conn = mongoose.createConnection(start.uri); const schema = new Schema({ name: String }); class Person extends mongoose.Model {} conn.model(Person, schema); assert.strictEqual(conn.modelNames()[0], 'Person'); + conn.close(done); }); }); - it('deleteModel()', function() { + it('deleteModel()', async function() { const conn = mongoose.createConnection(start.uri); let Model = conn.model('gh6813', new Schema({ name: String })); @@ -883,7 +893,8 @@ describe('connections:', function() { Model = conn.model('gh6813', new Schema({ name: String })); assert.ok(Model); - return Model.create({ name: 'test' }); + await Model.create({ name: 'test' }); + await conn.close(); }); it('throws a MongooseServerSelectionError on server selection timeout (gh-8451)', function() { @@ -926,6 +937,7 @@ describe('connections:', function() { await nextChange; assert.equal(changes.length, 1); assert.equal(changes[0].operationType, 'insert'); + await conn.close(); }); it('useDB inherits config from default connection (gh-8267)', async function() { @@ -947,6 +959,7 @@ describe('connections:', function() { await conn.createCollection('test'); const res = await conn.dropCollection('test'); assert.ok(res); + await conn.close(); }); it('connection.asPromise() resolves to a connection instance (gh-9496)', async function() { @@ -1065,7 +1078,7 @@ describe('connections:', function() { }); describe('mongoose.createConnection', function() { - it('forces autoIndex & autoCreate to be false if read preference is secondary or secondaryPreferred (gh-9374)', function() { + it('forces autoIndex & autoCreate to be false if read preference is secondary or secondaryPreferred (gh-9374)', async function() { const conn = new mongoose.createConnection(start.uri, { readPreference: 'secondary' }); assert.equal(conn.get('autoIndex'), false); @@ -1075,13 +1088,16 @@ describe('connections:', function() { assert.equal(conn2.get('autoIndex'), false); assert.equal(conn2.get('autoCreate'), false); + await conn.close(); + await conn2.close(); }); - it('keeps autoIndex & autoCreate as true by default if read preference is primaryPreferred (gh-9374)', function() { + it('keeps autoIndex & autoCreate as true by default if read preference is primaryPreferred (gh-9374)', async function() { const conn = new mongoose.createConnection(start.uri, { readPreference: 'primaryPreferred' }); assert.equal(conn.get('autoIndex'), undefined); assert.equal(conn.get('autoCreate'), undefined); + await conn.close(); }); it('throws if options try to set autoIndex to true', function() { @@ -1164,6 +1180,7 @@ describe('connections:', function() { indexes = await Test.collection.listIndexes().toArray(); assert.equal(indexes.length, 2); assert.equal(indexes[1].name, 'name_1'); + await conn.close(); }); it('re-runs init() if running setClient() after disconnecting (gh-12047)', async function() { @@ -1206,13 +1223,20 @@ describe('connections:', function() { describe('Connection#syncIndexes() (gh-10893) (gh-11039)', () => { let connection; - this.beforeEach(async() => { - const mongooseInstance = new mongoose.Mongoose(); + let mongooseInstance; + + before(async() => { + mongooseInstance = new mongoose.Mongoose(); connection = mongooseInstance.createConnection(start.uri); }); - this.afterEach(async() => { + beforeEach(() => connection.deleteModel(/.*/)); + afterEach(async() => { await connection.dropDatabase(); }); + after(async() => { + await connection.close(); + }); + it('Allows a syncIndexes option with connection mongoose.connection.syncIndexes (gh-10893)', async function() { const coll = 'tests2'; @@ -1235,6 +1259,7 @@ describe('connections:', function() { const indexesAfterDropping = await connection.syncIndexes(); assert.deepEqual(indexesAfterDropping, { Test: ['name_1'] }); }); + it('does not sync indexes automatically when `autoIndex: true` (gh-11039)', async function() { // Arrange const buildingSchema = new Schema({ name: String }, { autoIndex: false }); @@ -1272,6 +1297,7 @@ describe('connections:', function() { assert.deepEqual(floorIndexes.map(index => index.key), [{ _id: 1 }]); assert.deepEqual(officeIndexes.map(index => index.key), [{ _id: 1 }]); }); + it('stops as soon as one model fails with `continueOnError: false` (gh-11039)', async function() { // Arrange const buildingSchema = new Schema({ name: String }, { autoIndex: false }); @@ -1356,6 +1382,7 @@ describe('connections:', function() { assert.equal(err.errors['Book'].code, 11000); assert.equal(err.errors['Book'].code, 11000); }); + it('when `continueOnError: true` it will continue to sync indexes even if one model fails', async() => { // Arrange const buildingSchema = new Schema({ name: String }, { autoIndex: false }); @@ -1451,6 +1478,8 @@ describe('connections:', function() { assert.ok(Array.isArray(result['Building'])); assert.ok(result['Floor'].name, 'MongoServerError'); assert.ok(Array.isArray(result['Office'])); + + await m.disconnect(); }); }); diff --git a/test/docs/cast.test.js b/test/docs/cast.test.js index ce89219e5c8..1461c0849a8 100644 --- a/test/docs/cast.test.js +++ b/test/docs/cast.test.js @@ -23,6 +23,10 @@ describe('Cast Tutorial', function() { }); }); + after(async () => { + await mongoose.disconnect(); + }) + it('get and set', async function() { const query = Character.find({ name: 'Jean-Luc Picard' }); query.getFilter(); // `{ name: 'Jean-Luc Picard' }` @@ -150,4 +154,4 @@ describe('Cast Tutorial', function() { }); // acquit:ignore:end }); -}); \ No newline at end of file +}); diff --git a/test/docs/date.test.js b/test/docs/date.test.js index 0cf8811c41f..62a147d26be 100644 --- a/test/docs/date.test.js +++ b/test/docs/date.test.js @@ -5,7 +5,6 @@ const start = require('../common'); describe('Date Tutorial', function() { let User; - // let db; const mongoose = new start.mongoose.Mongoose(); @@ -20,6 +19,10 @@ describe('Date Tutorial', function() { return mongoose.connect(start.uri); }); + after(async () => { + await mongoose.disconnect(); + }) + it('Example 1.2: casts strings to dates', function() { const user = new User({ name: 'Jean-Luc Picard', @@ -83,7 +86,6 @@ describe('Date Tutorial', function() { describe('Example 1.3.1', function() { let Episode; - let db; before(async function() { const episodeSchema = new mongoose.Schema({ @@ -96,8 +98,7 @@ describe('Date Tutorial', function() { max: '1994-05-23' } }); - db = await start().asPromise(); - Episode = db.model('Episode', episodeSchema); + Episode = mongoose.model('Episode2', episodeSchema); await Episode.create([ { title: 'Encounter at Farpoint', airedAt: '1987-09-28' }, diff --git a/test/docs/debug.test.js b/test/docs/debug.test.js index 39df6e93369..b12e9c65bbf 100644 --- a/test/docs/debug.test.js +++ b/test/docs/debug.test.js @@ -39,6 +39,8 @@ describe('debug: shell', function() { const originalConsole = console.info; const originalDebugOption = mongoose.options.debug; + const connectionsToClose = []; + before(function() { db = start(); testModel = db.model('Test', testSchema); @@ -57,6 +59,7 @@ describe('debug: shell', function() { console.info = originalConsole; mongoose.set('debug', originalDebugOption); await db.close(); + await Promise.all(connectionsToClose.map((v) => v.close())) }); it('no-shell', async function() { @@ -75,12 +78,14 @@ describe('debug: shell', function() { const m = new mongoose.Mongoose(); // `conn1` with active debug const conn1 = m.createConnection(start.uri); + connectionsToClose.push(conn1); conn1.set('debug', true); const testModel1 = conn1.model('Test', testSchema); await testModel1.create({ dob: new Date(), title: 'Connection 1' }); const storedLog = lastLog; // `conn2` without debug const conn2 = m.createConnection(start.uri); + connectionsToClose.push(conn2); const testModel2 = conn2.model('Test', testSchema); await testModel2.create({ dob: new Date(), title: 'Connection 2' }); // Last log should not have been overwritten diff --git a/test/docs/findoneandupdate.test.js b/test/docs/findoneandupdate.test.js index 7d5b3578956..08158b06dc6 100644 --- a/test/docs/findoneandupdate.test.js +++ b/test/docs/findoneandupdate.test.js @@ -26,6 +26,10 @@ describe('Tutorial: findOneAndUpdate()', function() { await Character.create({ name: 'Jean-Luc Picard' }); }); + after(async () => { + await mongoose.disconnect(); + }) + it('basic case', async function() { // acquit:ignore:start await mongoose.model('Character').deleteMany({}); @@ -159,4 +163,4 @@ describe('Tutorial: findOneAndUpdate()', function() { assert.ok(!res.lastErrorObject.updatedExisting); // acquit:ignore:end }); -}); \ No newline at end of file +}); diff --git a/test/docs/getters-setters.test.js b/test/docs/getters-setters.test.js index 1c5c89bb610..78c285ae275 100644 --- a/test/docs/getters-setters.test.js +++ b/test/docs/getters-setters.test.js @@ -17,6 +17,10 @@ describe('getters/setters', function() { mongoose.deleteModel(/.*/); }); + after(async () => { + await mongoose.disconnect(); + }) + describe('getters', function() { it('basic example', async function() { const userSchema = new Schema({ @@ -223,4 +227,4 @@ describe('getters/setters', function() { assert.equal(recipes[0].ingredients[0].name, 'Huevos'); // 'Huevos' }); }) -}); \ No newline at end of file +}); diff --git a/test/docs/lean.test.js b/test/docs/lean.test.js index 576d8bcdcf3..a1b6c270d40 100644 --- a/test/docs/lean.test.js +++ b/test/docs/lean.test.js @@ -21,6 +21,10 @@ describe('Lean Tutorial', function() { mongoose.deleteModel(/Group/); }); + after(async () => { + await mongoose.disconnect(); + }) + it('compare sizes lean vs not lean', async function() { // acquit:ignore:start if (typeof Deno !== 'undefined') { diff --git a/test/docs/virtuals.test.js b/test/docs/virtuals.test.js index eb8f9ac55dd..e2cfc109f49 100644 --- a/test/docs/virtuals.test.js +++ b/test/docs/virtuals.test.js @@ -17,6 +17,10 @@ describe('Virtuals', function() { mongoose.deleteModel(/.*/); }); + after(async () => { + await mongoose.disconnect(); + }) + it('basic', async function() { const userSchema = mongoose.Schema({ email: String @@ -171,4 +175,4 @@ describe('Virtuals', function() { assert.equal(doc.author.email, 'test@gmail.com'); // acquit:ignore:end }); -}); \ No newline at end of file +}); diff --git a/test/helpers/clone.test.js b/test/helpers/clone.test.js index 482a9e4b33e..d8c6a74f103 100644 --- a/test/helpers/clone.test.js +++ b/test/helpers/clone.test.js @@ -1,6 +1,8 @@ 'use strict'; const assert = require('assert'); + +require('../common'); // required for side-effect setup (so that the default driver is set-up) const clone = require('../../lib/helpers/clone'); const symbols = require('../../lib/helpers/symbols'); const ObjectId = require('../../lib/types/objectid'); diff --git a/test/helpers/indexes.getRelatedIndexes.test.js b/test/helpers/indexes.getRelatedIndexes.test.js index e6a20d71cad..3eb597f883d 100644 --- a/test/helpers/indexes.getRelatedIndexes.test.js +++ b/test/helpers/indexes.getRelatedIndexes.test.js @@ -12,7 +12,10 @@ const { describe('getRelatedIndexes', () => { let db; - beforeEach(() => db = start()); + before(() => db = start()); + beforeEach(() => db.deleteModel(/.*/)); + afterEach(() => require('../util').clearTestData(db)); + afterEach(() => require('../util').stopRemainingOps(db)); after(() => db.close()); describe('getRelatedSchemaIndexes', () => { diff --git a/test/helpers/indexes.isIndexEqual.test.js b/test/helpers/indexes.isIndexEqual.test.js index 9194fe79a02..ee4d343b013 100644 --- a/test/helpers/indexes.isIndexEqual.test.js +++ b/test/helpers/indexes.isIndexEqual.test.js @@ -1,6 +1,8 @@ 'use strict'; const assert = require('assert'); + +require('../common'); // required for side-effect setup (so that the default driver is set-up) const isIndexEqual = require('../../lib/helpers/indexes/isIndexEqual'); describe('isIndexEqual', function() { diff --git a/test/helpers/isMongooseObject.test.js b/test/helpers/isMongooseObject.test.js index f309cbdd754..6a20956400a 100644 --- a/test/helpers/isMongooseObject.test.js +++ b/test/helpers/isMongooseObject.test.js @@ -1,6 +1,8 @@ 'use strict'; const assert = require('assert'); + +require('../common'); // required for side-effect setup (so that the default driver is set-up) const isMongooseObject = require('../../lib/helpers/isMongooseObject'); const MongooseArray = require('../../lib/types/array'); diff --git a/test/helpers/isSimpleValidator.test.js b/test/helpers/isSimpleValidator.test.js index 6dfcaec6368..e76d4c4bcb2 100644 --- a/test/helpers/isSimpleValidator.test.js +++ b/test/helpers/isSimpleValidator.test.js @@ -1,6 +1,8 @@ 'use strict'; const assert = require('assert'); + +require('../common'); // required for side-effect setup (so that the default driver is set-up) const isSimpleValidator = require('../../lib/helpers/isSimpleValidator'); const MongooseDocumentArray = require('../../lib/types/DocumentArray'); diff --git a/test/helpers/populate.getSchemaTypes.test.js b/test/helpers/populate.getSchemaTypes.test.js index b34af9d3a36..f46592b098f 100644 --- a/test/helpers/populate.getSchemaTypes.test.js +++ b/test/helpers/populate.getSchemaTypes.test.js @@ -1,9 +1,10 @@ 'use strict'; -const Schema = require('../../lib/schema'); const assert = require('assert'); -const getSchemaTypes = require('../../lib/helpers/populate/getSchemaTypes'); + const mongoose = require('../common').mongoose; +const Schema = require('../../lib/schema'); +const getSchemaTypes = require('../../lib/helpers/populate/getSchemaTypes'); describe('getSchemaTypes', function() { it('handles embedded discriminators (gh-5970)', function() { diff --git a/test/helpers/populate.getVirtual.test.js b/test/helpers/populate.getVirtual.test.js index 0b9be11e379..7919cd225fe 100644 --- a/test/helpers/populate.getVirtual.test.js +++ b/test/helpers/populate.getVirtual.test.js @@ -1,7 +1,9 @@ 'use strict'; -const Schema = require('../../lib/schema'); const assert = require('assert'); + +require('../common'); // required for side-effect setup (so that the default driver is set-up) +const Schema = require('../../lib/schema'); const getVirtual = require('../../lib/helpers/populate/getVirtual'); describe('getVirtual', function() { diff --git a/test/helpers/projection.applyProjection.test.js b/test/helpers/projection.applyProjection.test.js index 93ae5cb8a10..fadfe53fa25 100644 --- a/test/helpers/projection.applyProjection.test.js +++ b/test/helpers/projection.applyProjection.test.js @@ -1,8 +1,10 @@ 'use strict'; -const applyProjection = require('../../lib/helpers/projection/applyProjection'); const assert = require('assert'); +require('../common'); // required for side-effect setup (so that the default driver is set-up) +const applyProjection = require('../../lib/helpers/projection/applyProjection'); + describe('applyProjection', function() { it('handles deep inclusive projections', function() { const obj = { str: 'test', nested: { str2: 'test2', num3: 42 } }; diff --git a/test/index.test.js b/test/index.test.js index 723bc817234..6c6a5797943 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -88,6 +88,7 @@ describe('mongoose module:', function() { await User.findOne(); assert.equal(written.length, 1); assert.ok(written[0].startsWith('users.findOne(')); + await mongoose.disconnect(); }); it('{g,s}etting options', function() { @@ -479,6 +480,7 @@ describe('mongoose module:', function() { assert.equal(doc.createdAt.valueOf(), date.valueOf()); assert.equal(doc.updatedAt.valueOf(), date.valueOf()); + await mongoose.disconnect(); }); it('isolates custom types between mongoose instances (gh-6933) (gh-7158)', function() { @@ -758,6 +760,7 @@ describe('mongoose module:', function() { await Person.create({ name: 'Test1', favoriteMovie: movie._id }); const entry = await Person.findOne().populate({ path: 'favoriteMovie' }); assert(entry); + await mongoose.disconnect(); }); it('global `strictPopulate` works when true (gh-10694)', async function() { const mongoose = new Mongoose(); @@ -774,6 +777,7 @@ describe('mongoose module:', function() { await assert.rejects(async() => { await Person.findOne().populate({ path: 'favoriteGame' }); }, { message: 'Cannot populate path `favoriteGame` because it is not in your schema. Set the `strictPopulate` option to false to override.' }); + await mongoose.disconnect(); }); it('allows global `strictPopulate` to be overriden on specific queries set to true (gh-10694)', async function() { const mongoose = new Mongoose(); @@ -789,6 +793,7 @@ describe('mongoose module:', function() { await assert.rejects(async() => { await Person.findOne().populate({ path: 'favoriteGame', strictPopulate: true }); }, { message: 'Cannot populate path `favoriteGame` because it is not in your schema. Set the `strictPopulate` option to false to override.' }); + await mongoose.disconnect(); }); it('allows global `strictPopulate` to be overriden on specific queries set to false (gh-10694)', async function() { const mongoose = new Mongoose(); @@ -803,6 +808,7 @@ describe('mongoose module:', function() { await Person.create({ name: 'Test1', favoriteMovie: movie._id }); const entry = await Person.findOne().populate({ path: 'favoriteMovie' }); assert(entry); + await mongoose.disconnect(); }); describe('exports', function() { @@ -871,6 +877,7 @@ describe('mongoose module:', function() { // lean is necessary to avoid defaults by casting const movie = await Movie.findOne({ title: 'Cloud Atlas' }).lean(); assert.equal(movie.genre, 'Action'); + await m.disconnect(); }); it('setting `setDefaultOnInsert` on operation has priority over base option (gh-9032)', async function() { @@ -896,6 +903,7 @@ describe('mongoose module:', function() { // lean is necessary to avoid defaults by casting const movie = await Movie.findOne({ title: 'The Man From Earth' }).lean(); assert.ok(!movie.genre); + await m.disconnect(); }); it('should prevent non-hexadecimal strings (gh-9996)', function() { const badIdString = 'z'.repeat(24); @@ -970,6 +978,7 @@ describe('mongoose module:', function() { // Assert const optionsSentToMongo = nativeAggregateSpy.args[0][1]; assert.strictEqual(optionsSentToMongo.allowDiskUse, undefined); + await m.disconnect(); }); it('works when set to `true` and no option provided', async() => { @@ -992,6 +1001,7 @@ describe('mongoose module:', function() { // Assert const optionsSentToMongo = nativeAggregateSpy.args[0][1]; assert.strictEqual(optionsSentToMongo.allowDiskUse, true); + await m.disconnect(); }); it('can be overridden by a specific query', async() => { // Arrange @@ -1013,6 +1023,7 @@ describe('mongoose module:', function() { // Assert const optionsSentToMongo = nativeAggregateSpy.args[0][1]; assert.equal(optionsSentToMongo.allowDiskUse, false); + await m.disconnect(); }); }); describe('global `timestamps.createdAt.immutable` (gh-10139)', () => { @@ -1097,6 +1108,7 @@ describe('mongoose module:', function() { title: 'The IDless master' }); assert.equal(entry.id, undefined); + await m.disconnect(); }); }); diff --git a/test/model.test.js b/test/model.test.js index eb68df34476..04c7765ce76 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -25,6 +25,8 @@ describe('Model', function() { let Comments; let BlogPost; + const connectionsToClose = []; + beforeEach(() => db.deleteModel(/.*/)); beforeEach(function() { @@ -80,8 +82,9 @@ describe('Model', function() { db = start(); }); - after(function() { - db.close(); + after(async function() { + await db.close(); + await Promise.all(connectionsToClose.map(async(v) => /* v instanceof Promise ? (await v).close() : */ v.close())); }); afterEach(() => util.clearTestData(db)); @@ -3720,6 +3723,7 @@ describe('Model', function() { it('with positional notation on path not existing in schema (gh-1048)', function(done) { const db = start(); + connectionsToClose.push(db); const M = db.model('Test', Schema({ name: 'string' })); db.on('open', function() { @@ -4332,6 +4336,7 @@ describe('Model', function() { it('save max bson size error with buffering (gh-3906)', async function() { this.timeout(10000); const db = start({ noErrorListener: true }); + connectionsToClose.push(db); const Test = db.model('Test', { name: Object }); const test = new Test({ @@ -4350,6 +4355,7 @@ describe('Model', function() { it('reports max bson size error in save (gh-3906)', async function() { this.timeout(10000); const db = await start({ noErrorListener: true }); + connectionsToClose.push(db); const Test = db.model('Test', { name: Object }); const test = new Test({ @@ -5336,6 +5342,7 @@ describe('Model', function() { it('watch() before connecting (gh-5964)', async function() { const db = start(); + connectionsToClose.push(db); const MyModel = db.model('Test5964', new Schema({ name: String })); @@ -5355,6 +5362,7 @@ describe('Model', function() { it('watch() close() prevents buffered watch op from running (gh-7022)', async function() { const db = start(); + connectionsToClose.push(db); const MyModel = db.model('Test', new Schema({})); const changeStream = MyModel.watch(); const ready = new global.Promise(resolve => { @@ -5372,6 +5380,7 @@ describe('Model', function() { it('watch() close() closes the stream (gh-7022)', async function() { const db = await start(); + connectionsToClose.push(db); const MyModel = db.model('Test', new Schema({ name: String })); await MyModel.init(); @@ -5422,6 +5431,7 @@ describe('Model', function() { it('startSession() before connecting', async function() { const db = start(); + connectionsToClose.push(db); const MyModel = db.model('Test', new Schema({ name: String })); diff --git a/test/query.middleware.test.js b/test/query.middleware.test.js index 6e7e4c6ee54..d18118fd3b6 100644 --- a/test/query.middleware.test.js +++ b/test/query.middleware.test.js @@ -14,12 +14,15 @@ describe('query middleware', function() { let Author; let Publisher; + const connectionsToClose = []; + before(function() { db = start(); }); after(async function() { await db.close(); + await Promise.all(connectionsToClose.map((v) => v.close())); }); const initializeData = function(done) { @@ -81,7 +84,8 @@ describe('query middleware', function() { next(); }); - start(); + const conn = start(); + connectionsToClose.push(conn); initializeData(function(error) { assert.ifError(error);