Nestjs:测试时无法注入 ConfigService
Nestjs: cannot inject ConfigService while testing
我在设置使用 ConfigService
设置 TypeORM 的单元测试时遇到问题。下面的测试失败并显示以下消息:
Nest can't resolve dependencies of the TypeOrmModuleOptions (?).
Please make sure that the argument ConfigService at index [0] is
available in the TypeOrmCoreModule context.
我试过将 ConfigService
添加为提供商,但没有成功。对我做错了什么有什么想法吗?
import { ConfigModule, ConfigService } from '@nestjs/config';
import { Test } from '@nestjs/testing';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
describe('TypeORM setup', () => {
beforeEach(async () => {
await Test.createTestingModule({
providers: [ConfigService],
imports: [
ConfigModule.forRoot(),
TypeOrmModule.forRootAsync({
useFactory: (config: ConfigService) => ({ ...config.get('db') }),
inject: [ConfigService],
}),
],
}).compile();
});
it('dummy', () => {
true === true;
});
});
您需要将 { isGlobal: true }
设置为 ConfigModule
的选项,或者您需要将 imports: [ConfigModule]
添加到 TypeOrmModule.forRootAsync()
配置中
我在设置使用 ConfigService
设置 TypeORM 的单元测试时遇到问题。下面的测试失败并显示以下消息:
Nest can't resolve dependencies of the TypeOrmModuleOptions (?). Please make sure that the argument ConfigService at index [0] is available in the TypeOrmCoreModule context.
我试过将 ConfigService
添加为提供商,但没有成功。对我做错了什么有什么想法吗?
import { ConfigModule, ConfigService } from '@nestjs/config';
import { Test } from '@nestjs/testing';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
describe('TypeORM setup', () => {
beforeEach(async () => {
await Test.createTestingModule({
providers: [ConfigService],
imports: [
ConfigModule.forRoot(),
TypeOrmModule.forRootAsync({
useFactory: (config: ConfigService) => ({ ...config.get('db') }),
inject: [ConfigService],
}),
],
}).compile();
});
it('dummy', () => {
true === true;
});
});
您需要将 { isGlobal: true }
设置为 ConfigModule
的选项,或者您需要将 imports: [ConfigModule]
添加到 TypeOrmModule.forRootAsync()
配置中