Jon*_*sen 3 dependency-injection typescript nestjs launchdarkly
我创建了一个服务,它的模块如下所示:
launchdarkly.module.ts
@Module({
providers: [LaunchdarklyService],
exports: [LaunchdarklyService],
imports: [ConfigService],
})
export class LaunchdarklyModule {}
Run Code Online (Sandbox Code Playgroud)
(此服务/模块是让应用程序使用 LaunchDarkly 功能标记)
如果您愿意,我很乐意展示服务实现,但为了缩短这个问题,我跳过了它。重要的一点是该服务导入ConfigService(它用于获取 LaunchDarkly SDK 密钥)。
但是我该如何测试Launchdarkly服务呢?它从中读取一个键,ConfigService所以我想编写ConfigService具有各种值的测试,但是经过数小时的尝试,我无法弄清楚如何ConfigService在测试中进行配置。
这是测试:
launchdarkly.service.spec.ts
describe('LaunchdarklyService', () => {
let service: LaunchdarklyService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [LaunchdarklyService],
imports: [ConfigModule],
}).compile();
service = module.get<LaunchdarklyService>(LaunchdarklyService);
});
it("should not create a client if there's no key", async () => {
// somehow I need ConfigService to have key FOO=undefined for this test
expect(service.client).toBeUndefined();
});
it("should create a client if an SDK key is specified", async () => {
// For this test ConfigService needs to specify FOO=123
expect(service.client).toBeDefined();
});
})
Run Code Online (Sandbox Code Playgroud)
我愿意接受任何非 hacky 的建议,我只想标记我的应用程序!
小智 6
forFeature您可以使用ConfigModule 提供的方法获得更清晰的语法。
它接受一个需要返回一个对象的异步函数(在您的例子中,是您的 Env 对象)。
使用该方法的优点forFeature是您可以注册部分对象,因此您无需担心其他变量(或执行大量 ifs)。
beforeEach(async () => {
const moduleRef = await Test.createTestingModule({
imports: [
ConfigModule.forFeature(async () => ({
ANY_KEY_YOU_WANT: 'Any_Value'
}))
],
providers: [AnyService]
}).compile()
service = moduleRef.get<AnyService>(AnyService)
})
Run Code Online (Sandbox Code Playgroud)
假设LaunchdarklyService需要将ConfigService和 注入到构造函数中,您可以ConfigService通过使用Custom Provider来提供所需的自定义凭据来提供 的模拟变体。例如,测试的模拟可能看起来像
describe('LaunchdarklyService', () => {
let service: LaunchdarklyService;
let config: ConfigService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [LaunchdarklyService, {
provide: ConfigService,
useValue: {
get: jest.fn((key: string) => {
// this is being super extra, in the case that you need multiple keys with the `get` method
if (key === 'FOO') {
return 123;
}
return null;
})
}
],
}).compile();
service = module.get<LaunchdarklyService>(LaunchdarklyService);
config = module.get<ConfigService>(ConfigService);
});
it("should not create a client if there's no key", async () => {
// somehow I need ConfigService to have key FOO=undefined for this test
// we can use jest spies to change the return value of a method
jest.spyOn(config, 'get').mockReturnedValueOnce(undefined);
expect(service.client).toBeUndefined();
});
it("should create a client if an SDK key is specified", async () => {
// For this test ConfigService needs to specify FOO=123
// the pre-configured mock takes care of this case
expect(service.client).toBeDefined();
});
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1730 次 |
| 最近记录: |