最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

node.js - Separated db for each Jest test file for typegoose - Stack Overflow

matteradmin6PV0评论

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?

  1. file 1 calls connect to db1
  2. file 2 calls connect to db2
  3. file 1 calls getModelForClass(User)
  4. 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?

  1. file 1 calls connect to db1
  2. file 2 calls connect to db2
  3. file 1 calls getModelForClass(User)
  4. 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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

I 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();
  });
});
Post a comment

comment list (0)

  1. No comments so far