import mongoose from 'mongoose' ; import MongoMemoryServer from 'mongodb-memory-server-core' ; const { Schema , Types } = mongoose ; mongoose . Promise = Promise ; const originalConnect = mongoose . connect ; mongoose . createConnection = ( async ( ) => { const mongoServer = await MongoMemoryServer . create ( ) ; const mongoUri = mongoServer . getUri ( ) ; originalConnect . bind ( mongoose ) ( mongoUri , { useNewUrlParser : true , useUnifiedTopology : true , } as any ) ; mongoose . connection . on ( 'error' , ( e ) => { if ( e . message . code === 'ETIMEDOUT' ) { console . error ( e ) ; } else { throw e ; } } ) ; mongoose . connection . once ( 'open' , ( ) => { } ) ; mongoose . connection . once ( 'disconnected' , ( ) => { mongoServer . stop ( ) ; } ) ; } ) as any ; mongoose . connect = mongoose . createConnection as any ; export { mongoose , Schema , Types } ;