NestJS TypeORM InjectRepository 无法读取未定义的属性“原型”

ran*_*ery 7 typescript jestjs typeorm nestjs

尝试进行单元测试。出现以下错误:

类型错误:无法读取未定义的属性“原型”

导出类 UserService {

构造函数(@InjectRepository(用户)私有只读userRepository:存储库<用户>){}

规格:

describe('AuthController', () => {
let authController: AuthController;
let authService: AuthService;
let mockRepository = {

};
beforeEach(async () => {
    const module = await Test.createTestingModule({
        imports: [
            TypeOrmModule.forFeature([User]),
        ],
        controllers: [AuthController],
        providers: [AuthService, {
            provide: getRepositoryToken(User),
            useValue: mockRepository
        }]
    }).compile()
    authService = module.get<AuthService>(AuthService);
    authController = module.get<AuthController>(AuthController)
});
Run Code Online (Sandbox Code Playgroud)

有人可以分享解决方案吗?

更多信息

所以看起来好像有问题typeorm

beforeEach(async () => {
    const module = await Test.createTestingModule({

    }).compile()
    authService = module.get<AuthService>(AuthService);
    authController = module.get<AuthController>(AuthController)
});
Run Code Online (Sandbox Code Playgroud)

使用这段代码我得到了完全相同的错误。所以唯一的问题是添加typeorm到这个测试模块。

所以它因为依赖而失败:AuthController->AuthService->UserService->TypeORM

顺便说一句,刚刚检查了UserService使用 Postman 的 API,它工作正常。

仍然没有结果:

 module = await Test.createTestingModule({
        controllers: [AuthController],
        components: [
            {
                provide: AuthService,
                useValue: {}
            },
            {
                provide: UserService,
                useValue: {}
            },
            {
                provide: getRepositoryToken(User),
                useValue: {}
            }
        ],
        providers: [
            {
                provide: AuthService,
                useValue: {}
            },
            {
                provide: UserService,
                useValue: {}
            },
            {
                provide: getRepositoryToken(User),
                useValue: {}
            }
        ]
    }).compile()
    this.authController = module.get<AuthController>(AuthController)
Run Code Online (Sandbox Code Playgroud)

class AuthServiceMock {
    logIn(userName) {
        return { id:100, isDeleted:false, login:"login", password:"password"};
    }

    signUp() {
        return { expireIn: 3600, token:"token" };
    }
}

describe('AuthController', () => {
let module: TestingModule;
let authController: AuthController;
let authService: AuthService;

beforeEach(async () => {
    module = await Test.createTestingModule({
        controllers: [AuthController],
        components: [

        ],
        providers: [
            {
                provide: AuthService,
                useClass: AuthServiceMock
            },
        ]
    }).compile()
    this.authController = module.get<AuthController>(AuthController)
});
Run Code Online (Sandbox Code Playgroud)

Thi*_*ger 4

我查看了您在 Kim Kern 下的评论中提供的项目(https://github.com/rankery/wof-server

您正在使用桶文件 ( src/user/index.ts),导出UserModule

export * from './user.module';
Run Code Online (Sandbox Code Playgroud)

我猜你稍后会使用这个桶文件来导入模块。

现在,每次导入桶文件的内容时,都会执行您的构建版本中包含的代码src/user/user.module.ts,其中包括类的装饰UserModule,这反过来会使 Typeorm 尝试构建存储库,从而导致错误。

您应该从中删除此导出src/user/index.ts(或直接删除该文件)并修复因此删除而导致的损坏的导入,它应该可以工作。