Nest JS-为返回可观察到的Axios响应的函数编写Jest测试用例的问题 [英] Nest JS - Issue writing Jest Test Case for a function returning Observable Axios Response

查看:117
本文介绍了Nest JS-为返回可观察到的Axios响应的函数编写Jest测试用例的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对NestJS + Typescript + RxJs技术堆栈还很陌生.我正在尝试使用Jest为我的功能之一编写单元测试用例,但不确定是否正确执行了.

I am fairly new to NestJS + Typescript + RxJs tech stack. I am trying to write a unit test case using Jest for one of my functions but not sure if doing it correctly.

component.service.ts

public fetchComponents(queryParams) {
  const url = this.prepareUrl(queryParams);

  const data$ = this.httpService.get(url);

  return data$
    .pipe(map(({ data }) => data));
}

component.sevice.spec.ts

测试用例可以通过并通过

Test case works and passes

describe('fetchComponents', () => {
  const query = {
    limit: 10,
    offset: 0
  };

  const result: AxiosResponse = {
    data: 'Components',
    status: 200,
    statusText: 'OK',
    headers: {},
    config: {}
  };
  it('should return Dummy Data when called successfully', () => {
    componentService.prepareUrl = jest.fn();

    jest.spyOn(httpService, 'get').mockImplementation(() => of(result));

   componentService.fetchComponents(market, query)
    .subscribe(
      (res) => {
        expect(res).toEqual('Components');
      }
    );
  });
});

能否请您提供一些有关我应该如何正确测试此功能的建议和指示.也无需使用像marbel-rx这样的库 我不确定是否测试正确.还有其他我应该测试的东西吗?

Can you please provide suggestions and pointers on how exactly I should test this function. Also without using Library like marbel-rx I am not sure if I am testing it correctly. Is there something else also which I should test?

推荐答案

由于Observables是异步的,因此必须添加异步done参数,并在最后执行的expect之后调用done().否则,在subscribe()被调用之后,jest将完成测试运行,而无需等待subscribe回调的异步执行的执行.尝试使测试失败,例如通过预期'Komponents'.测试不会失败.

Since Observables are asynchronous, you have to add the asynchronous done paramter and call done() after the expect that is executed last. Otherwise, jest will finish the test run after subscribe() is called without waiting for the execution of the asynchronous execution of subscribe's callback. Try to make your test fail by for example by expecting 'Komponents'. The test will not fail.

此外,我建议尽可能使用mockImplementationOnce而不是mockImplementation,以避免在以后的调用中隐式重用模拟行为,从而避免创建隐式依赖项.

Also, I'd recommend to use mockImplementationOnce instead of mockImplementation when possible, to avoid implicitly reusing mock behaviors in later calls and therewith creating implicit dependencies.

it('should return Dummy Data when called successfully', done => {
// Add done parameter                                   ^^^^
  componentService.prepareUrl = jest.fn();

  jest.spyOn(httpService, 'get').mockImplementationOnce(() => of(result));
// Prefer mockImplementationOnce                   ^^^^

  componentService.fetchComponents(market, query)
    .subscribe(
      (res) => {
        expect(res).toEqual('Components');
        done();
//      ^^^^^^  Call done() when test is finished
      }
    );
});

这篇关于Nest JS-为返回可观察到的Axios响应的函数编写Jest测试用例的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆