You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Nest.js和MikroORM运行E2E测试遇依赖注入错误求助

Nest.js E2E测试:依赖解析与类型错误修复

问题现象

  • 执行命令 npm run test:e2e -- src/users/users.e2e-spec.ts 时,抛出 Nest无法解析UserEntityRepository的EntityManager依赖 错误
  • 同时存在 app.close() 类型不兼容问题
  • 注释测试模块中 imports: [UsersModule] 后,出现 找不到PostgreSqlMikroORM 错误

环境版本

  • @mikro-orm/core: ^6.1.6
  • @mikro-orm/nestjs: ^5.2.3
  • @nestjs/core: ^10.0.0
  • @nestjs/platform-fastify: ^10.3.3
  • vitest: ^1.3.1

分步解决方案

1. 修复UserEntityRepository依赖解析失败

MikroORM 6.x 配合 NestJS 时,必须通过模块注册仓库,不能手动实例化。

检查UsersModule配置

确保UsersModule中导入MikroOrmModule.forFeature()注册实体,让Nest能正确注入仓库:

// users.module.ts
import { Module } from '@nestjs/common';
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { UserEntity } from './user.entity';
import { UsersService } from './users.service';
import { UsersController } from './users.controller';

@Module({
  imports: [MikroOrmModule.forFeature([UserEntity])],
  providers: [UsersService],
  controllers: [UsersController],
  exports: [UsersService],
})
export class UsersModule {}

正确注入仓库到Service

在UsersService中使用@InjectRepository()装饰器注入实体仓库,而非直接使用EntityManager:

// users.service.ts
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@mikro-orm/nestjs';
import { UserEntity } from './user.entity';
import { EntityRepository } from '@mikro-orm/postgresql';

@Injectable()
export class UsersService {
  constructor(
    @InjectRepository(UserEntity)
    private readonly userRepo: EntityRepository<UserEntity>,
  ) {}

  // 业务逻辑代码
}

2. 解决app.close()类型错误

Vitest环境下,需显式声明测试应用实例为INestApplication类型,避免类型推断问题:

// users.e2e-spec.ts
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { UsersModule } from './users.module';

describe('Users (e2e)', () => {
  let app: INestApplication;

  beforeEach(async () => {
    const moduleFixture = await Test.createTestingModule({
      imports: [UsersModule],
    }).compile();

    app = moduleFixture.createNestApplication();
    await app.init();
  });

  afterEach(async () => {
    await app.close(); // 类型错误解决
  });

  // 测试用例
});

3. 修复PostgreSqlMikroORM找不到的问题

这个错误是因为测试模块缺少根级MikroORM配置,导致数据库依赖未加载。需在测试模块中导入根配置:

方法1:导入测试专用配置

创建测试环境的MikroORM配置文件,然后在测试模块中导入:

// users.e2e-spec.ts
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { UsersModule } from './users.module';
import { mikroOrmTestConfig } from '../test-mikro-orm.config'; // 自定义测试配置

describe('Users (e2e)', () => {
  let app: INestApplication;

  beforeEach(async () => {
    const moduleFixture = await Test.createTestingModule({
      imports: [
        MikroOrmModule.forRoot(mikroOrmTestConfig),
        UsersModule,
      ],
    }).compile();

    app = moduleFixture.createNestApplication();
    await app.init();
  });

  // 其他测试代码
});

方法2:确认依赖安装

如果仍然提示找不到PostgreSqlMikroORM,检查是否安装了对应包:

npm install @mikro-orm/postgresql --save

4. 版本兼容性校验

确保所有MikroORM相关包版本一致(core、nestjs、postgresql),避免跨版本冲突。当前@mikro-orm/nestjs: ^5.2.3与MikroORM 6.x、NestJS 10.x是兼容的,但如果有零星问题,可以尝试锁定小版本号。


内容的提问来源于stack exchange,提问作者Swix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 09:25:34