【问题标题】:Nest.js handling errors for HttpServiceNest.js 处理 HttpService 的错误
【发布时间】:2020-08-27 18:09:04
【问题描述】:

我正在尝试测试 NestJS 内置的 HttpService(基于 Axios)。不过,我在测试错误/异常状态时遇到了麻烦。在我的测试套件中,我有:

let client: SomeClearingFirmClient;

  const mockConfigService = {
    get: jest.fn((type) => {
      switch(type) {
        case 'someApiBaseUrl': {
          return 'http://example.com'
        }
        case 'someAddAccountEndpoint': {
          return '/ClientAccounts/Add';
        }
        case 'someApiKey': {
          return 'some-api-key';
        }

        default:
          return 'test';
      }
    }),
  };

  const successfulAdd: AxiosResponse = {
    data: {
      batchNo: '39cba402-bfa9-424c-b265-1c98204df7ea',
      warning: '',
    },
    status: 200,
    statusText: 'OK',
    headers: {},
    config: {},
  };

  const failAddAuth: AxiosError = {
    code: '401',
    config: {},
    name: '',
    message: 'Not Authorized',
  }

  const mockHttpService = {
    post: jest.fn(),
    get: jest.fn(),
  }

  it('Handles a failure', async () => {
    expect.assertions(1);
    mockHttpService.post = jest.fn(() => of(failAddAuth));

    const module: TestingModule = await Test.createTestingModule({
      providers: [
        {
          provide: ConfigService,
          useValue: mockConfigService,
        },
        {
          provide: HttpService,
          useValue: mockHttpService,
        },
        SomeClearingFirmClient,
      ],
    }).compile();

    client = module.get<SomeClearingFirmClient>(SomeClearingFirmClient);

    const payload = new SomeClearingPayload();
    try {
      await client.addAccount(payload);
    } catch(e) {
      console.log('e', e);
    }
  });

我的实现是:

async addAccount(payload: any): Promise<SomeAddResponse> {
    const addAccountEndpoint = this.configService.get('api.someAddAccountEndpoint');
    const url = `${this.baseUrl}${addAccountEndpoint}?apiKey=${this.apiKey}`;
    const config = {
      headers: {
        'Content-Type': 'application/json',
      }
    };

    const response = this.httpService.post(url, payload, config)
      .pipe(
        map(res => {
          return res.data;
        }),
        catchError(e => {
          throw new HttpException(e.response.data, e.response.status);
        }),
      ).toPromise().catch(e => {
        throw new HttpException(e.message, e.code);
      });

    return response;
  }

无论我使用 Observables 还是 Promises,我都无法捕捉到任何东西。 4xx 级错误成功通过。我觉得我记得 Axios 添加了某种配置选项来拒绝/向失败的订阅者发送 Observable 错误......但我可以想象这一点。我在测试工具中做错了吗?我看到的其他 StackOverflow 帖子似乎说通过 catchError 管道应该可以解决问题,但我的错误是通过 map 运算符。

【问题讨论】:

    标签: rxjs axios nestjs


    【解决方案1】:

    您的mockHttpService 似乎没有返回错误,而是返回一个值:

    mockHttpService.post = jest.fn(() => of(failAddAuth));
    

    of(failAddAuth) 所做的是发出一个值(failAddAuth)然后完成。

    这就是为什么永远无法到达来自this.httpService.post(url, payload, config)catchError 的原因,因为不会发生错误。

    为了确保catchError 被命中,post() 返回的 observable 必须发出错误通知

    你可以试试这个:

    // Something to comply with `HttpException`'s arguments
    const err = { response: 'resp', status: '4xx' };
    
    mockHttpService.post = jest.fn(() => throwError(err));
    

    throwError(err)new Observable(s =&gt; s.error(err))(Source code) 相同。

    【讨论】:

    • 好的,这很有道理。问题在于模拟本身。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    相关资源
    最近更新 更多