@@ -56,6 +56,7 @@ describe('connections:', function() {
5656 } ) . asPromise ( ) ;
5757
5858 assert . strictEqual ( conn . config . autoIndex , false ) ;
59+ await conn . close ( ) ;
5960 } ) ;
6061
6162 it ( 'with autoCreate (gh-6489)' , async function ( ) {
@@ -80,6 +81,7 @@ describe('connections:', function() {
8081 const res = await conn . collection ( 'gh6489_Conn' ) .
8182 find ( { } ) . sort ( { name : 1 } ) . toArray ( ) ;
8283 assert . deepEqual ( res . map ( v => v . name ) , [ 'alpha' , 'Zeta' ] ) ;
84+ await conn . close ( ) ;
8385 } ) ;
8486
8587 it ( 'with autoCreate = false (gh-8814)' , async function ( ) {
@@ -95,6 +97,7 @@ describe('connections:', function() {
9597
9698 const res = await conn . db . listCollections ( ) . toArray ( ) ;
9799 assert . ok ( ! res . map ( c => c . name ) . includes ( 'gh8814_Conn' ) ) ;
100+ await conn . close ( ) ;
98101 } ) ;
99102
100103 it ( 'autoCreate when collection already exists does not fail (gh-7122)' , async function ( ) {
@@ -108,6 +111,7 @@ describe('connections:', function() {
108111 } , { autoCreate : true } ) ;
109112
110113 await conn . model ( 'Actor' , schema ) . init ( ) ;
114+ await conn . close ( ) ;
111115 } ) ;
112116
113117 it ( 'throws helpful error with legacy syntax (gh-6756)' , function ( ) {
@@ -133,9 +137,10 @@ describe('connections:', function() {
133137
134138 const _conn = await bootMongo . promise ;
135139 assert . equal ( _conn , conn ) ;
140+ await conn . close ( ) ;
136141 } ) ;
137142
138- it ( 'connection plugins (gh-7378)' , function ( ) {
143+ it ( 'connection plugins (gh-7378)' , async function ( ) {
139144 const conn1 = mongoose . createConnection ( start . uri ) ;
140145 const conn2 = mongoose . createConnection ( start . uri ) ;
141146
@@ -149,6 +154,8 @@ describe('connections:', function() {
149154 conn1 . model ( 'Test' , schema ) ;
150155 assert . equal ( called . length , 1 ) ;
151156 assert . equal ( called [ 0 ] , schema ) ;
157+ await conn1 . close ( ) ;
158+ await conn2 . close ( ) ;
152159 } ) ;
153160 } ) ;
154161
@@ -511,7 +518,7 @@ describe('connections:', function() {
511518 } ) .
512519 then ( ( ) => {
513520 assert . ok ( session . serverSession . lastUse > lastUse ) ;
514- conn . close ( ) ;
521+ return conn . close ( ) ;
515522 } ) ;
516523 } ) ;
517524
@@ -593,6 +600,7 @@ describe('connections:', function() {
593600 const nothing2 = await m2 . findById ( i1 . id ) ;
594601 assert . strictEqual ( null , nothing2 ) ;
595602
603+ await db . close ( ) ;
596604 await db2 . close ( ) ;
597605 } ) ;
598606
@@ -752,6 +760,7 @@ describe('connections:', function() {
752760
753761 await db . openUri ( start . uri ) ;
754762 assert . strictEqual ( db . client , db2 . client ) ;
763+ await db . close ( ) ;
755764 } ) ;
756765
757766 it ( 'closes correctly for all dbs, closing secondary db' , function ( done ) {
@@ -764,29 +773,29 @@ describe('connections:', function() {
764773 db2 . close ( ) ;
765774 } ) ;
766775
767- it ( 'cache connections to the same db' , function ( ) {
776+ it ( 'cache connections to the same db' , function ( done ) {
768777 const db = start ( ) ;
769778 const db2 = db . useDb ( start . databases [ 1 ] , { useCache : true } ) ;
770779 const db3 = db . useDb ( start . databases [ 1 ] , { useCache : true } ) ;
771780
772781 assert . strictEqual ( db2 , db3 ) ;
773- db . close ( ) ;
782+ db . close ( done ) ;
774783 } ) ;
775784 } ) ;
776785
777786 describe ( 'shouldAuthenticate()' , function ( ) {
778787 describe ( 'when using standard authentication' , function ( ) {
779788 describe ( 'when username and password are undefined' , function ( ) {
780- it ( 'should return false' , function ( ) {
789+ it ( 'should return false' , function ( done ) {
781790 const db = mongoose . createConnection ( start . uri , { } ) ;
782791
783792 assert . equal ( db . shouldAuthenticate ( ) , false ) ;
784793
785- db . close ( ) ;
794+ db . close ( done ) ;
786795 } ) ;
787796 } ) ;
788797 describe ( 'when username and password are empty strings' , function ( ) {
789- it ( 'should return false' , function ( ) {
798+ it ( 'should return false' , function ( done ) {
790799 const db = mongoose . createConnection ( start . uri , {
791800 user : '' ,
792801 pass : ''
@@ -795,7 +804,7 @@ describe('connections:', function() {
795804
796805 assert . equal ( db . shouldAuthenticate ( ) , false ) ;
797806
798- db . close ( ) ;
807+ db . close ( done ) ;
799808 } ) ;
800809 } ) ;
801810 describe ( 'when both username and password are defined' , function ( ) {
@@ -808,20 +817,20 @@ describe('connections:', function() {
808817
809818 assert . equal ( db . shouldAuthenticate ( ) , true ) ;
810819
811- db . close ( ) ;
820+ db . close ( ) ; // does not actually do anything
812821 } ) ;
813822 } ) ;
814823 } ) ;
815824 describe ( 'when using MONGODB-X509 authentication' , function ( ) {
816825 describe ( 'when username and password are undefined' , function ( ) {
817- it ( 'should return false' , function ( ) {
826+ it ( 'should return false' , function ( done ) {
818827 const db = mongoose . createConnection ( start . uri , { } ) ;
819828 db . on ( 'error' , function ( ) {
820829 } ) ;
821830
822831 assert . equal ( db . shouldAuthenticate ( ) , false ) ;
823832
824- db . close ( ) ;
833+ db . close ( done ) ;
825834 } ) ;
826835 } ) ;
827836 describe ( 'when only username is defined' , function ( ) {
@@ -833,7 +842,7 @@ describe('connections:', function() {
833842 db . asPromise ( ) . catch ( ( ) => { } ) ;
834843 assert . equal ( db . shouldAuthenticate ( ) , true ) ;
835844
836- db . close ( ) ;
845+ db . close ( ) ; // does not actually do anything
837846 } ) ;
838847 } ) ;
839848 describe ( 'when both username and password are defined' , function ( ) {
@@ -847,23 +856,24 @@ describe('connections:', function() {
847856
848857 assert . equal ( db . shouldAuthenticate ( ) , true ) ;
849858
850- db . close ( ) ;
859+ db . close ( ) ; // does not actually do anything
851860 } ) ;
852861 } ) ;
853862 } ) ;
854863 } ) ;
855864
856865 describe ( 'passing a function into createConnection' , function ( ) {
857- it ( 'should store the name of the function (gh-6517)' , function ( ) {
866+ it ( 'should store the name of the function (gh-6517)' , function ( done ) {
858867 const conn = mongoose . createConnection ( start . uri ) ;
859868 const schema = new Schema ( { name : String } ) ;
860869 class Person extends mongoose . Model { }
861870 conn . model ( Person , schema ) ;
862871 assert . strictEqual ( conn . modelNames ( ) [ 0 ] , 'Person' ) ;
872+ conn . close ( done ) ;
863873 } ) ;
864874 } ) ;
865875
866- it ( 'deleteModel()' , function ( ) {
876+ it ( 'deleteModel()' , async function ( ) {
867877 const conn = mongoose . createConnection ( start . uri ) ;
868878
869879 let Model = conn . model ( 'gh6813' , new Schema ( { name : String } ) ) ;
@@ -883,7 +893,8 @@ describe('connections:', function() {
883893
884894 Model = conn . model ( 'gh6813' , new Schema ( { name : String } ) ) ;
885895 assert . ok ( Model ) ;
886- return Model . create ( { name : 'test' } ) ;
896+ await Model . create ( { name : 'test' } ) ;
897+ await conn . close ( ) ;
887898 } ) ;
888899
889900 it ( 'throws a MongooseServerSelectionError on server selection timeout (gh-8451)' , function ( ) {
@@ -926,6 +937,7 @@ describe('connections:', function() {
926937 await nextChange ;
927938 assert . equal ( changes . length , 1 ) ;
928939 assert . equal ( changes [ 0 ] . operationType , 'insert' ) ;
940+ await conn . close ( ) ;
929941 } ) ;
930942
931943 it ( 'useDB inherits config from default connection (gh-8267)' , async function ( ) {
@@ -947,6 +959,7 @@ describe('connections:', function() {
947959 await conn . createCollection ( 'test' ) ;
948960 const res = await conn . dropCollection ( 'test' ) ;
949961 assert . ok ( res ) ;
962+ await conn . close ( ) ;
950963 } ) ;
951964
952965 it ( 'connection.asPromise() resolves to a connection instance (gh-9496)' , async function ( ) {
@@ -1065,7 +1078,7 @@ describe('connections:', function() {
10651078 } ) ;
10661079
10671080 describe ( 'mongoose.createConnection' , function ( ) {
1068- it ( 'forces autoIndex & autoCreate to be false if read preference is secondary or secondaryPreferred (gh-9374)' , function ( ) {
1081+ it ( 'forces autoIndex & autoCreate to be false if read preference is secondary or secondaryPreferred (gh-9374)' , async function ( ) {
10691082 const conn = new mongoose . createConnection ( start . uri , { readPreference : 'secondary' } ) ;
10701083
10711084 assert . equal ( conn . get ( 'autoIndex' ) , false ) ;
@@ -1075,13 +1088,16 @@ describe('connections:', function() {
10751088
10761089 assert . equal ( conn2 . get ( 'autoIndex' ) , false ) ;
10771090 assert . equal ( conn2 . get ( 'autoCreate' ) , false ) ;
1091+ await conn . close ( ) ;
1092+ await conn2 . close ( ) ;
10781093 } ) ;
10791094
1080- it ( 'keeps autoIndex & autoCreate as true by default if read preference is primaryPreferred (gh-9374)' , function ( ) {
1095+ it ( 'keeps autoIndex & autoCreate as true by default if read preference is primaryPreferred (gh-9374)' , async function ( ) {
10811096 const conn = new mongoose . createConnection ( start . uri , { readPreference : 'primaryPreferred' } ) ;
10821097
10831098 assert . equal ( conn . get ( 'autoIndex' ) , undefined ) ;
10841099 assert . equal ( conn . get ( 'autoCreate' ) , undefined ) ;
1100+ await conn . close ( ) ;
10851101 } ) ;
10861102
10871103 it ( 'throws if options try to set autoIndex to true' , function ( ) {
@@ -1164,6 +1180,7 @@ describe('connections:', function() {
11641180 indexes = await Test . collection . listIndexes ( ) . toArray ( ) ;
11651181 assert . equal ( indexes . length , 2 ) ;
11661182 assert . equal ( indexes [ 1 ] . name , 'name_1' ) ;
1183+ await conn . close ( ) ;
11671184 } ) ;
11681185
11691186 it ( 're-runs init() if running setClient() after disconnecting (gh-12047)' , async function ( ) {
@@ -1206,13 +1223,20 @@ describe('connections:', function() {
12061223
12071224 describe ( 'Connection#syncIndexes() (gh-10893) (gh-11039)' , ( ) => {
12081225 let connection ;
1209- this . beforeEach ( async ( ) => {
1210- const mongooseInstance = new mongoose . Mongoose ( ) ;
1226+ let mongooseInstance ;
1227+
1228+ before ( async ( ) => {
1229+ mongooseInstance = new mongoose . Mongoose ( ) ;
12111230 connection = mongooseInstance . createConnection ( start . uri ) ;
12121231 } ) ;
1213- this . afterEach ( async ( ) => {
1232+ beforeEach ( ( ) => connection . deleteModel ( / .* / ) ) ;
1233+ afterEach ( async ( ) => {
12141234 await connection . dropDatabase ( ) ;
12151235 } ) ;
1236+ after ( async ( ) => {
1237+ await connection . close ( ) ;
1238+ } ) ;
1239+
12161240 it ( 'Allows a syncIndexes option with connection mongoose.connection.syncIndexes (gh-10893)' , async function ( ) {
12171241 const coll = 'tests2' ;
12181242
@@ -1235,6 +1259,7 @@ describe('connections:', function() {
12351259 const indexesAfterDropping = await connection . syncIndexes ( ) ;
12361260 assert . deepEqual ( indexesAfterDropping , { Test : [ 'name_1' ] } ) ;
12371261 } ) ;
1262+
12381263 it ( 'does not sync indexes automatically when `autoIndex: true` (gh-11039)' , async function ( ) {
12391264 // Arrange
12401265 const buildingSchema = new Schema ( { name : String } , { autoIndex : false } ) ;
@@ -1272,6 +1297,7 @@ describe('connections:', function() {
12721297 assert . deepEqual ( floorIndexes . map ( index => index . key ) , [ { _id : 1 } ] ) ;
12731298 assert . deepEqual ( officeIndexes . map ( index => index . key ) , [ { _id : 1 } ] ) ;
12741299 } ) ;
1300+
12751301 it ( 'stops as soon as one model fails with `continueOnError: false` (gh-11039)' , async function ( ) {
12761302 // Arrange
12771303 const buildingSchema = new Schema ( { name : String } , { autoIndex : false } ) ;
@@ -1356,6 +1382,7 @@ describe('connections:', function() {
13561382 assert . equal ( err . errors [ 'Book' ] . code , 11000 ) ;
13571383 assert . equal ( err . errors [ 'Book' ] . code , 11000 ) ;
13581384 } ) ;
1385+
13591386 it ( 'when `continueOnError: true` it will continue to sync indexes even if one model fails' , async ( ) => {
13601387 // Arrange
13611388 const buildingSchema = new Schema ( { name : String } , { autoIndex : false } ) ;
@@ -1451,6 +1478,8 @@ describe('connections:', function() {
14511478 assert . ok ( Array . isArray ( result [ 'Building' ] ) ) ;
14521479 assert . ok ( result [ 'Floor' ] . name , 'MongoServerError' ) ;
14531480 assert . ok ( Array . isArray ( result [ 'Office' ] ) ) ;
1481+
1482+ await m . disconnect ( ) ;
14541483 } ) ;
14551484 } ) ;
14561485
0 commit comments