admin管理员组文章数量:1026989
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();
});
});
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();
});
});
本文标签: nodejsSeparated db for each Jest test file for typegooseStack Overflow
版权声明:本文标题:node.js - Separated db for each Jest test file for typegoose - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745659249a2161788.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论