NestJS ve zaman uyumsuz işlevli Jest
Ben bir test için bir bir zaman uyumsuz fonksiyonunu çalışıyorum service
içinde nestJS .
bu işlev eşzamansızdır ... temelde veritabanından bir değer (JSON) alır (depo - TypeORM kullanarak) ve verileri başarıyla aldığında, farklı bir sınıfa (DTO) "dönüştürün" ... uygulama:
async getAppConfig(): Promise<ConfigAppDto> {
return this.configRepository.findOne({
key: Equal("APPLICATION"),
}).then(config => {
if (config == null) {
return new class implements ConfigAppDto {
clientId = '';
clientSecret = '';
};
}
return JSON.parse(config.value) as ConfigAppDto;
});
}
bir denetleyici kullanarak, bunun iyi çalıştığını kontrol ettim. Şimdi, testleri yapmak için Jest'i kullanmaya çalışıyorum, ancak başarılı olamadım ... Benim sorunum, findOne
işlevle nasıl dalga geçileceğidir repository
..
Düzenleme : @golevelup/nestjs-testing
Alay etmek için kullanmaya çalışıyorum Repository
!
Zaten alay ettim repository
, ama nedense resolve
asla aranmadı ..
describe('getAppConfig', () => {
const repo = createMock<Repository<Config>>();
beforeEach(async () => {
await Test.createTestingModule({
providers: [
ConfigService,
{
provide: getRepositoryToken(Config),
useValue: repo,
}
],
}).compile();
});
it('should return ConfigApp parameters', async () => {
const mockedConfig = new Config('APPLICATION', '{"clientId": "foo","clientSecret": "bar"}');
repo.findOne.mockResolvedValue(mockedConfig);
expect(await repo.findOne()).toEqual(mockedConfig); // ok
const expectedReturn = new class implements ConfigAppDto {
clientId = 'foo';
clientSecret = 'bar';
};
expect(await service.getAppConfig()).toEqual(expectedReturn);
// jest documentation about async -> https://jestjs.io/docs/en/asynchronous
// return expect(service.getAppConfig()).resolves.toBe(expectedReturn);
});
})
expect(await repo.findOne()).toEqual(mockedConfig);
Büyük çalışır;expect(await service.getAppConfig()).toEqual(expectedReturn);
zaman aşımı var =>Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout
;
hata ayıklamayı kullanarak, the ' service.getAppConfig()
nin çağrıldığını görüyorum repository.findOne()
, ancak .then
findOne deposunun hiçbir zaman çağrılmadığını görüyorum.
Güncelleme : Depoyu kullanarak alay etmeye çalışıyorum @golevelup/nestjs-testing
ve bazı nedenlerden dolayı alay edilen sonuç hizmette çalışmıyor. Depoyu yalnızca kullanarak jest
(aşağıdaki kod gibi) alay edersem , test çalışır ... bu yüzden, asıl sorunum olduğunu düşünüyorum @golevelup/nestjs-testing
.
...
provide: getRepositoryToken(Config),
useValue: {
find: jest.fn().mockResolvedValue([new Config()])
},
...
Yanıtlar
Ben alay ediyorum nasıl Yani, benim gerçek bir sorundur Repository
ON NestJS
. Bazı nedenlerden dolayı, kullanmakla alay ettiğimde @golevelup/nestjs-testing
tuhaf şeyler oluyor!
Bu konuda gerçekten iyi bir belge bulamadım @golevelup/nestjs-testing
, bu yüzden kullanmaktan vazgeçtim.
Soru için benim çözümüm sadece Jest
ve NestJS
işlevlerini kullanmaktı ... sonuç kodu şuydu:
Hizmet :
// i'm injecting Connection because I need for some transactions later;
constructor(@InjectRepository(Config) private readonly configRepo: Repository<Config>, private connection: Connection) {}
async getAppConfig(): Promise<ConfigApp> {
return this.configRepo.findOne({
key: Equal("APPLICATION"),
}).then(config => {
if (config == null) {
return new ConfigApp();
}
return JSON.parse(config.value) as ConfigApp;
})
}
Test :
describe('getAppConfig', () => {
const configApi = new Config();
configApi.key = 'APPLICATION';
configApi.value = '{"clientId": "foo", "clientSecret": "bar"}';
beforeEach(async () => {
const module = await Test.createTestingModule({
providers: [
ConfigAppService,
{
provide: getRepositoryToken(Config),
useValue: {
findOne: jest.fn().mockResolvedValue(new
Config("APPLICATION", '{"clientId": "foo", "clientSecret": "bar"}')),
},
},
{
provide: getConnectionToken(),
useValue: {},
}
],
}).compile();
service = module.get<ConfigAppService>(ConfigAppService);
});
it('should return ConfigApp parameters', async () => {
const expectedValue: ConfigApp = new ConfigApp("foo", "bar");
return service.getAppConfig().then(value => {
expect(value).toEqual(expectedValue);
})
});
})
bu çözüm için kullanılan bazı kaynaklar: https://github.com/jmcdo29/testing-nestjs/tree/master/apps/typeorm-sample
Bence expect(await repo.findOne()).toEqual(mockedConfig);
işe yarıyor çünkü onunla alay ettin, bu yüzden hemen geri dönüyor. Durumunda, expect(await service.getAppConfig()).toEqual(expectedReturn);
alay etmediniz, bu nedenle muhtemelen daha fazla zaman alıyor, bu nedenle it
işlev Promise
tamamen çözülmeden önce geri dönüyor .
Jest belgelerinden gönderdiğiniz yorumlar, çağrıyla dalga geçerseniz hile yapmalıdır getAppConfig()
.
service.getAppConfig = jest.fn(() => Promise.resolve(someFakeValue))
veya
spyOn(service, 'getAppConfig').and.mockReturnValue(Promise.resolve(fakeValue))