Nest.js 处理 HttpService 的错误

Jim*_*ton 5 rxjs axios nestjs

我正在尝试测试 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);
    }
  });
Run Code Online (Sandbox Code Playgroud)

我的实现是:

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;
  }
Run Code Online (Sandbox Code Playgroud)

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

And*_*tej 8

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

mockHttpService.post = jest.fn(() => of(failAddAuth));
Run Code Online (Sandbox Code Playgroud)

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

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

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

你可以试试这个:

// Something to comply with `HttpException`'s arguments
const err = { response: 'resp', status: '4xx' };

mockHttpService.post = jest.fn(() => throwError(err));
Run Code Online (Sandbox Code Playgroud)

throwError(err)new Observable(s => s.error(err))与(源代码)相同。

  • 好吧,这是有道理的。问题出在模拟本身。谢谢! (2认同)