NestJS ve zaman uyumsuz işlevli Jest

Aug 18 2020

Ben bir test için bir bir zaman uyumsuz fonksiyonunu çalışıyorum serviceiç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, findOneişlevle nasıl dalga geçileceğidir repository..

Düzenleme : @golevelup/nestjs-testingAlay etmek için kullanmaya çalışıyorum Repository!

Zaten alay ettim repository, ama nedense resolveasla 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 .thenfindOne deposunun hiçbir zaman çağrılmadığını görüyorum.

Güncelleme : Depoyu kullanarak alay etmeye çalışıyorum @golevelup/nestjs-testingve 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

2 RobertoCorreia Aug 21 2020 at 22:25

Ben alay ediyorum nasıl Yani, benim gerçek bir sorundur RepositoryON NestJS. Bazı nedenlerden dolayı, kullanmakla alay ettiğimde @golevelup/nestjs-testingtuhaf ş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 Jestve NestJSiş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

1 Leo Aug 18 2020 at 19:53

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 itişlev Promisetamamen çö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))