11/* eslint-disable @typescript-eslint/no-non-null-assertion */
22import { MongoClient , MongoServerError } from 'mongodb' ;
3- import * as tmp from 'tmp' ;
43import MongoMemoryServer , {
54 CreateUser ,
65 MongoInstanceData ,
@@ -15,7 +14,6 @@ import { assertIsError } from './testUtils/test_utils';
1514import { promises as fspromises } from 'fs' ;
1615import * as path from 'path' ;
1716
18- tmp . setGracefulCleanup ( ) ;
1917jest . setTimeout ( 100000 ) ; // 10s
2018
2119afterEach ( ( ) => {
@@ -770,7 +768,7 @@ describe('MongoMemoryServer', () => {
770768
771769 describe ( 'cleanup()' , ( ) => {
772770 /** Cleanup the created tmp-dir, even if the cleanup test tested to not clean it up */
773- let tmpdir : tmp . DirResult | undefined ;
771+ let tmpdir : string | undefined ;
774772
775773 // "beforeAll" dosnt work here, thanks to the top-level "afterAll" hook
776774 beforeEach ( ( ) => {
@@ -779,9 +777,9 @@ describe('MongoMemoryServer', () => {
779777 jest . spyOn ( semver . default , 'lt' ) ; // it needs to be ".default" otherwise "lt" is only an getter
780778 } ) ;
781779
782- afterEach ( ( ) => {
780+ afterEach ( async ( ) => {
783781 if ( ! utils . isNullOrUndefined ( tmpdir ) ) {
784- tmpdir . removeCallback ( ) ;
782+ await utils . removeDir ( tmpdir ) ;
785783
786784 tmpdir = undefined ; // reset, just to be sure its clean
787785 }
@@ -820,8 +818,8 @@ describe('MongoMemoryServer', () => {
820818 } ) ;
821819
822820 it ( 'should properly cleanup with force (without tmpDir) (old)' , async ( ) => {
823- const tmpDir = tmp . dirSync ( { prefix : 'mongo-mem-cleanup-' , unsafeCleanup : true } ) ;
824- const mongoServer = await MongoMemoryServer . create ( { instance : { dbPath : tmpDir . name } } ) ;
821+ const tmpDir = await utils . createTmpDir ( 'mongo-mem-cleanup-' ) ;
822+ const mongoServer = await MongoMemoryServer . create ( { instance : { dbPath : tmpDir } } ) ;
825823 const dbPath = mongoServer . instanceInfo ! . dbPath ;
826824
827825 tmpdir = mongoServer . instanceInfo ?. tmpDir ;
@@ -873,8 +871,8 @@ describe('MongoMemoryServer', () => {
873871 } ) ;
874872
875873 it ( 'should properly cleanup with force (without tmpDir) (new)' , async ( ) => {
876- const tmpDir = tmp . dirSync ( { prefix : 'mongo-mem-cleanup-' , unsafeCleanup : true } ) ;
877- const mongoServer = await MongoMemoryServer . create ( { instance : { dbPath : tmpDir . name } } ) ;
874+ const tmpDir = await utils . createTmpDir ( 'mongo-mem-cleanup-' ) ;
875+ const mongoServer = await MongoMemoryServer . create ( { instance : { dbPath : tmpDir } } ) ;
878876 const dbPath = mongoServer . instanceInfo ! . dbPath ;
879877
880878 const cleanupSpy = jest . spyOn ( mongoServer , 'cleanup' ) ;
@@ -926,10 +924,10 @@ describe('MongoMemoryServer', () => {
926924
927925 it ( 'should resolve "isNew" to "true" and set "createAuth" to "true" when dbPath is set, but empty' , async ( ) => {
928926 const readdirSpy = jest . spyOn ( fspromises , 'readdir' ) ;
929- const tmpDbPath = tmp . dirSync ( { prefix : 'mongo-mem-getStartOptions1-' , unsafeCleanup : true } ) ;
927+ const tmpDbPath = await utils . createTmpDir ( 'mongo-mem-getStartOptions1-' ) ;
930928
931929 const mongoServer = new MongoMemoryServer ( {
932- instance : { dbPath : tmpDbPath . name } ,
930+ instance : { dbPath : tmpDbPath } ,
933931 auth : { } ,
934932 } ) ;
935933
@@ -942,22 +940,22 @@ describe('MongoMemoryServer', () => {
942940 new Error ( 'Expected "options.data.dbPath" to be defined' )
943941 ) ;
944942 expect ( await utils . pathExists ( options . data . dbPath ) ) . toEqual ( true ) ;
945- expect ( options . data . dbPath ) . toEqual ( tmpDbPath . name ) ;
943+ expect ( options . data . dbPath ) . toEqual ( tmpDbPath ) ;
946944 expect ( readdirSpy ) . toHaveBeenCalledTimes ( 1 ) ;
947945 expect ( options . createAuth ) . toEqual ( true ) ;
948946
949- tmpDbPath . removeCallback ( ) ;
947+ await utils . removeDir ( tmpDbPath ) ;
950948 } ) ;
951949
952950 it ( 'should resolve "isNew" to "false" and set "createAuth" to "false" when dbPath is set, but not empty' , async ( ) => {
953951 const readdirSpy = jest . spyOn ( fspromises , 'readdir' ) ;
954- const tmpDbPath = tmp . dirSync ( { prefix : 'mongo-mem-getStartOptions1-' , unsafeCleanup : true } ) ;
952+ const tmpDbPath = await utils . createTmpDir ( 'mongo-mem-getStartOptions2-' ) ;
955953
956954 // create dummy file, to make the directory non-empty
957- await fspromises . writeFile ( path . resolve ( tmpDbPath . name , 'testfile' ) , '' ) ;
955+ await fspromises . writeFile ( path . resolve ( tmpDbPath , 'testfile' ) , '' ) ;
958956
959957 const mongoServer = new MongoMemoryServer ( {
960- instance : { dbPath : tmpDbPath . name } ,
958+ instance : { dbPath : tmpDbPath } ,
961959 auth : { } ,
962960 } ) ;
963961
@@ -970,11 +968,11 @@ describe('MongoMemoryServer', () => {
970968 new Error ( 'Expected "options.data.dbPath" to be defined' )
971969 ) ;
972970 expect ( await utils . pathExists ( options . data . dbPath ) ) . toEqual ( true ) ;
973- expect ( options . data . dbPath ) . toEqual ( tmpDbPath . name ) ;
971+ expect ( options . data . dbPath ) . toEqual ( tmpDbPath ) ;
974972 expect ( readdirSpy ) . toHaveBeenCalledTimes ( 1 ) ;
975973 expect ( options . createAuth ) . toEqual ( false ) ;
976974
977- tmpDbPath . removeCallback ( ) ;
975+ await utils . removeDir ( tmpDbPath ) ;
978976 } ) ;
979977
980978 it ( 'should generate a port when no suggestion is defined' , async ( ) => {
@@ -1027,17 +1025,17 @@ describe('MongoMemoryServer', () => {
10271025 } ) ;
10281026
10291027 it ( '"getDbPath" should return the dbPath' , async ( ) => {
1030- const tmpDir = tmp . dirSync ( { prefix : 'mongo-mem-getDbPath-' , unsafeCleanup : true } ) ;
1028+ const tmpDir = await utils . createTmpDir ( 'mongo-mem-getDbPath-' ) ;
10311029 const mongoServer = new MongoMemoryServer ( {
1032- instance : { dbPath : tmpDir . name } ,
1030+ instance : { dbPath : tmpDir } ,
10331031 } ) ;
10341032
10351033 await mongoServer . start ( ) ;
10361034
1037- expect ( mongoServer . instanceInfo ! . dbPath ) . toEqual ( tmpDir . name ) ;
1035+ expect ( mongoServer . instanceInfo ! . dbPath ) . toEqual ( tmpDir ) ;
10381036
10391037 await mongoServer . stop ( ) ;
1040- tmpDir . removeCallback ( ) ;
1038+ await utils . removeDir ( tmpDir ) ;
10411039 } ) ;
10421040
10431041 it ( '"state" should return correct state' , ( ) => {
0 commit comments