I have a bunch of test files that should use separate memory db since they run at the same time. I use Jest on NodeJS with Typegoose
sutup.ts:
import { MongoMemoryServer } from "mongodb-memory-server";
module.exports = async () => {
const mongoServer = await MongoMemoryServer.create();
global.__MONGOINSTANCE__ = mongoServer; // Store the instance for later access
process.env.MONGODB_URL_TEST = mongoServer.getUri();
};
testFile1.ts and testFile2.ts have the same initial code:
import { mongoose, getModelForClass } from "@typegoose/typegoose";
...
beforeAll(async () => {
if (!process.env.MONGODB_URL_TEST) {
throw new Error("MONGODB_URL_TEST is not set");
}
const uniqueUri = `${process.env.MONGODB_URL_TEST}-${Date.now()}`; // Append a unique identifier
await mongoose.connect(uniqueUri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
});
Since both files use shared mongoose instance, they both will operate with db that was connected last, right?
- file 1 calls connect to db1
- file 2 calls connect to db2
- file 1 calls getModelForClass(User)
- file 2 calls getModelForClass(User)
sinse mongoose is the same dosn't it mean that file 1 will get user model from db2? please correct me if i am wrong and if I am right please help to resolve the problem.
I have a bunch of test files that should use separate memory db since they run at the same time. I use Jest on NodeJS with Typegoose
sutup.ts:
import { MongoMemoryServer } from "mongodb-memory-server";
module.exports = async () => {
const mongoServer = await MongoMemoryServer.create();
global.__MONGOINSTANCE__ = mongoServer; // Store the instance for later access
process.env.MONGODB_URL_TEST = mongoServer.getUri();
};
testFile1.ts and testFile2.ts have the same initial code:
import { mongoose, getModelForClass } from "@typegoose/typegoose";
...
beforeAll(async () => {
if (!process.env.MONGODB_URL_TEST) {
throw new Error("MONGODB_URL_TEST is not set");
}
const uniqueUri = `${process.env.MONGODB_URL_TEST}-${Date.now()}`; // Append a unique identifier
await mongoose.connect(uniqueUri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
});
Since both files use shared mongoose instance, they both will operate with db that was connected last, right?
- file 1 calls connect to db1
- file 2 calls connect to db2
- file 1 calls getModelForClass(User)
- file 2 calls getModelForClass(User)
sinse mongoose is the same dosn't it mean that file 1 will get user model from db2? please correct me if i am wrong and if I am right please help to resolve the problem.
Share Improve this question asked Nov 16, 2024 at 11:26 TaraskoTarasko 456 bronze badges1 Answer
Reset to default 0I suggest this should do the trick:
import * as mongoose from "@typegoose/typegoose";
import { getModelForClass } from "@typegoose/typegoose";
...
const typegoose = mongoose.mongoose;
...
describe("MembershipService", () => {
beforeAll(async () => {
if (!process.env.MONGODB_URL_TEST) {
throw new Error("MONGODB_URL_TEST is not set");
}
const uniqueUri = `${process.env.MONGODB_URL_TEST}-MembershipService`;
await typegoose.connect(uniqueUri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log("MembershipService start", typegoose.connection.name);
...
MemberModel = getModelForClass(MembershipMember);
});
afterAll(async () => {
console.log("MembershipService end", typegoose.connection.name);
await typegoose.connection.close();
await typegoose.disconnect();
});
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745659250a4638734.html
评论列表(0条)