Skip to content

Commit

Permalink
chore: fix test port number
Browse files Browse the repository at this point in the history
  • Loading branch information
meabed committed Nov 13, 2023
1 parent 27e497c commit 885c409
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/__mocks__/mongooseCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,29 @@

import mongoose from 'mongoose';
import MongoMemoryServer from 'mongodb-memory-server-core';
import net, { AddressInfo } from 'net';

const { Schema, Types } = mongoose;

mongoose.Promise = Promise;

async function getPortFree() {
return new Promise<number>((res) => {
const srv = net.createServer();
srv.listen(0, () => {
const port = (srv.address() as AddressInfo).port;
srv.close(() => res(port));
});
});
}

const originalConnect = mongoose.connect;
mongoose.createConnection = (async () => {
const mongoServer = await MongoMemoryServer.create();
const mongoServer = await MongoMemoryServer.create({
instance: {
port: await getPortFree(),
},
});
const mongoUri = mongoServer.getUri();

originalConnect.bind(mongoose)(mongoUri, {});
Expand Down

0 comments on commit 885c409

Please sign in to comment.