How to provide fresh instance of Mikro ORM entity manager into repositories during testing?

I am trying to test my command handler which has multiple repositories injected into it. When running the test I am getting the following error:

ValidationError: Using global EntityManager instance methods for context specific actions is disallowed. If you need to work with the global instance's identity map, use `allowGlobalContext` configuration option or `fork()` instead.

My DatabaseModule boots up the MikroORM connection.

I understand that with the app, RequestContext is created with each request which provides me with a fresh instance of EntityManager and it is working perfectly fine. But for testing I cannot figure out how to do it.

This is the command handler itself (removed the actual handle function)

@Injectable()
export class RegisterUserCommandHandler
  implements ICommandHandler<RegisterUserCommand, { userId: string }>
{
  private readonly logger = new Logger(RegisterUserCommandHandler.name);

  constructor(
    private readonly authorizationService: AuthorizationService,
    private readonly userFactory: UserFactory,
    private readonly userRepository: UserRepository,
    private readonly physicianRepository: PhysicianRepository,
    private readonly representativeRepository: LocalRepresentativeRepository,
    private readonly pharmacistRepository: PharmacistRepository
  ) {}
}

This is the test code (I only pasted the relevant section)

import { Test } from "@nestjs/testing";
import { ConfigModule } from "@nestjs/config";
import { EventEmitterModule } from "@nestjs/event-emitter";
import { MikroORM, EntityManager } from "@mikro-orm/core";
import { isLeft, isRight } from "fp-ts/Either";

describe("RegisterUserCommandHandler", () => {
  let orm: MikroORM;
  let entityManager: EntityManager;
  let handler: RegisterUserCommandHandler;
  let userFactory: UserFactory;
  let userRepository: UserRepository;
  let physicianRepository: PhysicianRepository;
  let representativeRepository: LocalRepresentativeRepository;
  let pharmacistRepository: PharmacistRepository;

  beforeEach(async () => {
    const moduleRef = await Test.createTestingModule({
      imports: [
        EventEmitterModule.forRoot(),
        ConfigModule.forRoot({
          isGlobal: true,
        }),
        DatabaseModule,
      ],
      providers: [
        RegisterUserCommandHandler,
        {
          provide: AuthorizationService,
          useValue: {
            isUserAuthorizedAsAdmin: jest.fn(),
          },
        },
        {
          provide: PasswordGenerator,
          useValue: {
            generatePasswordHash: jest.fn(),
            validatePassword: jest.fn(),
            generateRandomPassword: jest.fn().mockResolvedValue("Password1"),
          },
        },
        UserFactory,
        UserRepository,
        PhysicianRepository,
        LocalRepresentativeRepository,
        PharmacistRepository,
      ],
    }).compile();

    orm = moduleRef.get<MikroORM>(MikroORM);
    handler = moduleRef.get<RegisterUserCommandHandler>(
      RegisterUserCommandHandler
    );
    userFactory = moduleRef.get<UserFactory>(UserFactory);
    userRepository = moduleRef.get<UserRepository>(UserRepository);
    physicianRepository =
      moduleRef.get<PhysicianRepository>(PhysicianRepository);
    representativeRepository = moduleRef.get<LocalRepresentativeRepository>(
      LocalRepresentativeRepository
    );
    pharmacistRepository =
      moduleRef.get<PharmacistRepository>(PharmacistRepository);

    
    entityManager = orm.em.fork();
  });
});

This is the base repository (every repository extends this in the system)

import {
  EntityManager,
  EntityRepository,
  SqlEntityManager,
} from "@mikro-orm/mysql";
import { AnyEntity, EntityName } from "@mikro-orm/core";
import { FindOptions } from "@mikro-orm/core/drivers/IDatabaseDriver";

import { IRepository } from "./repository.interface";
import { SOFT_DELETABLE_FILTER } from "../decorators";
import type { PopulatePath } from "@mikro-orm/core/enums";

export abstract class BaseRepository<T extends AnyEntity<T>>
  extends EntityRepository<T>
  implements IRepository<T>
{
  protected constructor(em: SqlEntityManager, entity: EntityName<T>) {
    super(em, entity);
  }

  //...more code here
}

And finally this is the database module

import { Module } from "@nestjs/common";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { MikroOrmModule } from "@mikro-orm/nestjs";

import { CoreModule, SoftDeletableHandlerSubscriber } from "@riskomed/core";

import { DatabaseConfig } from "./database.config";
import { GlobalSubscriber } from "./global.subscriber";
import mikroOrmConfig from "./mikro-orm.config";

@Module({
  imports: [
    CoreModule,
    ConfigModule.forFeature(DatabaseConfig),
    MikroOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => ({
        ...mikroOrmConfig(configService),
      }),
    }),
  ],
  providers: [GlobalSubscriber, SoftDeletableHandlerSubscriber],
})
export class DatabaseModule {}

How can I get past that error without using the allowGlobalContext?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật