如何在用玩笑捕获错误时测试 Redux-Saga

How to test Redux-Saga when catch error with jest

各位。我用 jest 框架测试 saga。我可以在正常情况下测试我的传奇。但是我想测试catch()中的代码,所以我必须模拟一个错误来触发catch。我在 redux-saga 官方文档和其他 Whosebug 答案中找到了一些解决方案。但是还是有问题。

当我像下面的示例一样在 saga.test.js 中使用 throw() 时,它会显示 "Error [object Object] thrown"。所以它真的不能通过这个测试。我没有看到有人问同样的问题。谁能帮帮我?非常感谢。

错误结果屏幕:

api.js

const api = {
  fetchProductAPI() {
    return 'iphone';
  },
};
export default api;

saga.js

import { call, put } from 'redux-saga/effects';
import api from './api';

export default function* fetchProduct() {
  try {
    yield call(api.fetchProductAPI);
    yield put({ type: 'PRODUCTS_RECEIVED', product: 'iphone' });
  } catch (error) {
    yield put({ type: 'PRODUCTS_REQUEST_FAILED', error });
  }
}

saga.test.js

import { put, call } from 'redux-saga/effects';
import fetchProduct from './saga';
import api from './api';

describe('fetchProduct()', () => {
  it('try', () => {
    const gen = fetchProduct();
    expect(gen.next().value).toEqual(call(api.fetchProductAPI));
    expect(gen.next().value).toEqual(put({ type: 'PRODUCTS_RECEIVED', product: 'iphone' }));
  });
  it('catch', () => {
    const error = 'product not found';
    const gen = fetchProduct();
    expect(
      gen.throw({
        error: 'product not found',
      }).value
    ).toEqual(put({ type: 'PRODUCTS_REQUEST_FAILED', error }));
  });
});

我在下面找到的相关解决方案答案:

Redux-Saga Error Handling

我的朋友帮我解决了这个问题。所以我自己回答我的问题...

我需要在投掷之前添加 gen.next()。下面是解决方案代码。

it('catch', () => {
  const error = 'product not found';
  const gen = fetchProduct();
  gen.next(); //add gen.next() before throw
  expect(
    gen.throw('product not found').value).
    toEqual(put({ type: 'PRODUCTS_REQUEST_FAILED', error }));
  });
});

您可以使用 jest-generator 轻松完成。 https://github.com/doniyor2109/jest-generator

it('catch', () => {
  const error = new Error('product not found');

  expect(fetchProduct()).toMatchYields([
    [call(api.fetchProductAPI), error],
    [put({ type: 'PRODUCTS_REQUEST_FAILED', error.message }))]
  ]);
});