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?

  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();
  });
});

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();
  });
});

本文标签: nodejsSeparated db for each Jest test file for typegooseStack Overflow