NestJS TypeORM InjectRepository 无法读取未定义的属性“原型" [英] NestJS TypeORM InjectRepository Cannot read property 'prototype' of undefined
问题描述
尝试进行单元测试.出现以下错误:
Trying to unit test. Got following error:
TypeError: 无法读取未定义的属性 'prototype'
TypeError: Cannot read property 'prototype' of undefined
导出类 UserService {
export class UserService {
constructor(@InjectRepository(User) 私有只读 userRepository:存储库用户>) { }
constructor(@InjectRepository(User) private readonly userRepository: Repository < User>) { }
规范:
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)
});
有人可以分享一个解决方案吗?
Can someone share a solution please?
更多信息:
所以看起来 typeorm
beforeEach(async () => {
const module = await Test.createTestingModule({
}).compile()
authService = module.get<AuthService>(AuthService);
authController = module.get<AuthController>(AuthController)
});
使用此代码,我得到完全相同的错误.所以唯一的问题是将 typeorm
添加到这个测试模块中.
With this code I'm getting exact the same error. So only problem is adding typeorm
to this test Module.
所以它因为依赖而失败:AuthController->AuthService->UserService->TypeORM
So it fails because of dependency: AuthController->AuthService->UserService->TypeORM
顺便说一句,刚刚使用 Postman API 检查了 UserService
并且它工作正常.
Btw just checked UserService
using API with Postman and it works fine.
仍然没有结果:
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)
还有
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)
});
推荐答案
我查看了您在 Kim Kern 下的评论中提供的项目 (https://github.com/rankery/wof-server)
I looked at the project you provided in a comment under Kim Kern (https://github.com/rankery/wof-server)
您正在使用桶文件 (src/user/index.ts
),导出 UserModule
You are using a barrel file (src/user/index.ts
), exporting the UserModule
export * from './user.module';
我猜你稍后会使用这个桶文件来导入模块.
I guess you are using this barrel file to import the module later on.
现在,每次导入barrel 文件的内容时,都会执行src/user/user.module.ts
构建版本中包含的代码,其中包括对UserModule
类,这反过来会使 Typeorm 尝试构建一个 Repository,从而导致错误.
Now, every time the content of the barrel file is imported , the code contained in the built version of your src/user/user.module.ts
is executed, which include the decoration of the UserModule
class, which in turns will make Typeorm try to build a Repository, which causes the error.
您应该从 src/user/index.ts
中删除此导出(或简单地删除该文件)并修复由此删除导致的损坏的导入,它应该可以工作.
You should remove this export from src/user/index.ts
(or simply delete the file) and fix the broken imports caused by this removal and it should work.
这篇关于NestJS TypeORM InjectRepository 无法读取未定义的属性“原型"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!