运行NestJS测试用例时出现I18nService依赖注入错误
NestJS测试模块编译错误:无法解析I18nService依赖
运行NestJS测试用例时出现依赖解析错误,Nest无法找到I18nService所需的I18nOptions等依赖项,报错提示需确保这些依赖在I18nService上下文可用。
测试代码
import { Test } from '@nestjs/testing'; import { appConfig } from '../app.config'; import { CommonBotModule } from '../common/common-bot.module'; import { CredentialsManagerModule } from '../credentials/credentials-manager.module'; import { BotModule } from './bot.module'; import { I18nService } from 'nestjs-i18n'; jest.mock('aws-sdk'); // 阻止连接DynamoDB jest.mock('@aws/dynamodb-data-mapper'); // 阻止连接DynamoDB jest.mock('kafkajs'); jest.mock('../cache/cacheConfig.service'); // 阻止连接Redis describe('bot.module.ts', () => { beforeAll(() => { config.init(appConfig); }); it('The bot module should be able to be compiled', async () => { const botModule = await Test.createTestingModule({ imports: [CommonBotModule, CredentialsManagerModule, BotModule, I18nService] }).compile(); expect(botModule).toBeDefined(); }); });
错误日志
Nest can't resolve dependencies of the I18nService (?, I18nTranslations, I18nLanguages, Logger, I18nLoader, I18nLanguagesSubject, I18nTranslationsSubject). Please make sure that the argument I18nOptions at index [0] is available in the I18nService context. Potential solutions: - If I18nOptions is a provider, is it part of the current I18nService? - If I18nOptions is exported from a separate @Module, is that module imported within I18nService? @Module({ imports: [ /* the Module containing I18nOptions */ ] })
问题原因与修复方案
核心问题
测试代码中错误地将I18nService放入了imports数组——imports只能存放用@Module装饰的模块类,而I18nService是一个服务提供者,不能直接导入到这里。Nest无法找到I18nService依赖的I18nOptions,因为没有正确引入提供这些依赖的模块。
修复方式(二选一)
方式1:模拟I18nService(推荐测试环境使用)
通过jest.mock模拟I18nService,避免初始化真实的服务和依赖:
import { Test } from '@nestjs/testing'; import { appConfig } from '../app.config'; import { CommonBotModule } from '../common/common-bot.module'; import { CredentialsManagerModule } from '../credentials/credentials-manager.module'; import { BotModule } from './bot.module'; import { I18nService } from 'nestjs-i18n'; jest.mock('aws-sdk'); jest.mock('@aws/dynamodb-data-mapper'); jest.mock('kafkajs'); jest.mock('../cache/cacheConfig.service'); // 模拟I18nService及其方法 jest.mock('nestjs-i18n', () => ({ ...jest.requireActual('nestjs-i18n'), I18nService: jest.fn().mockImplementation(() => ({ translate: jest.fn().mockReturnValue('模拟翻译内容') })) })); describe('bot.module.ts', () => { beforeAll(() => { config.init(appConfig); }); it('The bot module should be able to be compiled', async () => { const botModule = await Test.createTestingModule({ imports: [CommonBotModule, CredentialsManagerModule, BotModule] // 移除imports中的I18nService }).compile(); expect(botModule).toBeDefined(); }); });
方式2:导入并配置I18nModule
如果测试需要真实的I18n功能,在测试模块中导入并配置I18nModule:
import { Test } from '@nestjs/testing'; import { I18nModule } from 'nestjs-i18n'; import * as path from 'path'; import { appConfig } from '../app.config'; import { CommonBotModule } from '../common/common-bot.module'; import { CredentialsManagerModule } from '../credentials/credentials-manager.module'; import { BotModule } from './bot.module'; jest.mock('aws-sdk'); jest.mock('@aws/dynamodb-data-mapper'); jest.mock('kafkajs'); jest.mock('../cache/cacheConfig.service'); describe('bot.module.ts', () => { beforeAll(() => { config.init(appConfig); }); it('The bot module should be able to be compiled', async () => { const botModule = await Test.createTestingModule({ imports: [ CommonBotModule, CredentialsManagerModule, BotModule, // 配置测试用的I18nModule I18nModule.forRoot({ fallbackLanguage: 'en', loaderOptions: { path: path.join(__dirname, '../i18n'), watch: false } }) ] }).compile(); expect(botModule).toBeDefined(); }); });
内容的提问来源于stack exchange,提问作者Nishad Mahajan
相关产品推荐
相关产品推荐

