如何使用runSaga在Jest中测试失败动作

时间:2019-06-11 17:58:07

标签: javascript redux jestjs

我已经测试了成功的响应,但是遇到响应失败的麻烦。

这是动作:

export function* searchSaga({ searchParams }) {
  try {
    const { text, customers, searchField } = searchParams;
    const results = customers.filter(c => c[searchField] === text);
    yield put({
      type: "CUSTOMER_SEARCH_SUCCESS",
      results
    });
  } catch (error) {
    yield put({
      type: "CUSTOMER_SEARCH_FAILURE",
      error
    });
  }
}

成功测试通过:

  const initialAction = { type: "SEARCH_CUSTOMERS_START", searchParams };
  it("searches customers with a saga", async () => {
    const dispatched = await recordSaga(searchSaga, initialAction);
    const expectedAction = {
      type: "CUSTOMER_SEARCH_SUCCESS",
      results: [customers[0]]
    };
    expect(dispatched).toContainEqual(expectedAction);
  });

但是,当我尝试测试失败时,我在模拟程序中抛出的错误会阻塞:

it("returns an error on a failure", async () => {
    const error = new Error("uh oh!");
    Array.prototype.filter = jest.fn();
    Array.prototype.filter.mockImplementation(() => throw error);
    const dispatched = await recordSaga(searchSaga, initialAction);
    const expectedAction = { type: "CUSTOMER_SEARCH_FAILURE", error };
    console.log(dispatched);
    expect(dispatched).toContainEqual(expectedAction);
  });

此测试的输出包括引发的错误,但是控制台显示也已分派了正确的故障操作!

 ● Console

    console.log __tests__/CustomerSearch.test.js:48
      [ { type: 'CUSTOMER_SEARCH_FAILURE',
          error:
           Error: uh oh!
               at _callee2$ (/Users/TuzMacbookPro2017/Development/QMG-local/APPS/QMGProzReviews/__tests__/CustomerSearch.test.js:43:19)
               at tryCatch (/Users/TuzMacbookPro2017/Development/QMG-local/APPS/QMGProzReviews/node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:45:40)
               at Generator.invoke [as _invoke] (/Users/TuzMacbookPro2017/Development/QMG-local/APPS/QMGProzReviews/node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:271:22)
               at Generator.prototype.(anonymous function) [as next] (/Users/TuzMacbookPro2017/Development/QMG-local/APPS/QMGProzReviews/node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:97:21)
               at tryCatch (/Users/TuzMacbookPro2017/Development/QMG-local/APPS/QMGProzReviews/node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:45:40)
               at invoke (/Users/TuzMacbookPro2017/Development/QMG-local/APPS/QMGProzReviews/node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:135:20)
               at /Users/TuzMacbookPro2017/Development/QMG-local/APPS/QMGProzReviews/node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:170:11
               at tryCallTwo (/Users/TuzMacbookPro2017/Development/QMG-local/APPS/QMGProzReviews/node_modules/promise/lib/core.js:45:5)
               at doResolve (/Users/TuzMacbookPro2017/Development/QMG-local/APPS/QMGProzReviews/node_modules/promise/lib/core.js:200:13)
               at new Promise (/Users/TuzMacbookPro2017/Development/QMG-local/APPS/QMGProzReviews/node_modules/promise/lib/core.js:66:3) } ]

  ● searchCustomers › returns an error on a failure

    uh oh!

      41 | 
      42 |   it("returns an error on a failure", async () => {
    > 43 |     const error = new Error("uh oh!");
         |                   ^
      44 |     Array.prototype.filter = jest.fn();
      45 |     Array.prototype.filter.mockImplementation(() => throw error);
      46 |     const dispatched = await recordSaga(searchSaga, initialAction);

现在,我尝试将其包装在try / catch块中:

it("returns an error on a failure", async () => {
    const error = new Error("uh oh!");
    Array.prototype.filter = jest.fn();
    Array.prototype.filter.mockImplementation(() => throw error);
    try {
      const dispatched = await recordSaga(searchSaga, initialAction);
    } catch (e) {
      console.log(dispatched);
      const expectedAction = { type: "CUSTOMER_SEARCH_FAILURE", error };
      expect(dispatched).toContainEqual(expectedAction);
    }
  });

这确实可以通过,但这是一个误报。例如,如果我将预期操作的类型更改为任何废话,则测试仍会通过。

我如何进行这项工作?

编辑: 我注意到在最后一个传递的示例中,console.log(dispatched)从未被激活。我在try块中添加了该日志语句的副本,并且看到我 am 返回了正确的故障操作。

在进行日志记录时,似乎错误被抛出并捕获在一些奇怪的地方。

 it("returns an error on a failure", async () => {
    const error = new Error("uh oh!");
    Array.prototype.filter = jest.fn();
    Array.prototype.filter.mockImplementation(() => throw error);
    const expectedAction = { type: "CUSTOMER_SEARCH_FAILURE", error };
    try {
      const dispatched = await recordSaga(searchSaga, initialAction);
      console.log("***DISPATCHED FROM TRY IN TEST***", dispatched);
      expect(dispatched).toContainEqual(expectedAction);
    } catch (e) {
      console.log("***ERROR FROM CATCH IN TEST***", e);
    }
  });

以上内容从try块记录了正确的调度动作,然后从catch块记录了“ uh oh”错误。删除try块的最后一行会记录来自try块的正确操作,就这样,它就通过了。当然,它通过了,因为没有断言失败。

因此,似乎是引发错误的实际断言。我不知道何时引发错误以及如何避免错误。

1 个答案:

答案 0 :(得分:0)

如何内联错误抛出:

it("returns an error on a failure", async () => {
    Array.prototype.filter = jest.fn();
    Array.prototype.filter.mockImplementation(() => throw Error("uh oh!"));
    const dispatched = await recordSaga(searchSaga, initialAction);
    const expectedAction = { type: "CUSTOMER_SEARCH_FAILURE", error };
    console.log(dispatched);
    expect(dispatched).toContainEqual(expectedAction);
  });