【问题标题】:How to test all effect with redux-saga-test-plan. All effect do not match如何使用 redux-saga-test-plan 测试所有效果。所有效果不匹配
【发布时间】:2021-09-20 09:30:38
【问题描述】:

我有一个生成器,我想用单元测试来介绍

export default function* gatawayFlow() {
  yield all([
    takeEvery(actionTypes.GET_GATEWAYS_REQUEST, getGatewaysFlow),
    takeLatest(actionTypes.SELECT_GATEWAY_REQUEST, selectGatewayFlow),
  ]);
}

我用 redux-sag-test-plan 写了一个简单的测试

import {expectSaga, testSaga} from 'redux-saga-test-plan';
import gatawayFlow, {getGatewaysFlow, selectGatewayFlow} from '../logic/sagas';
import * as actions from '../logic/actions';
import * as actionTypes from '../logic/actionTypes';
import {takeEvery, takeLatest} from '@redux-saga/core/effects';

// Unit-test
describe('Unit tests', () => {
  test('Test all effect', () => {
    const saga = testSaga(gatawayFlow);
    saga
        .next()
        .all({
          [actionTypes.GET_GATEWAYS_REQUEST]: takeEvery(actionTypes.GET_GATEWAYS_REQUEST, getGatewaysFlow),
          [actionTypes.SELECT_GATEWAY_REQUEST]: takeLatest(actionTypes.SELECT_GATEWAY_REQUEST, selectGatewayFlow)
        })
        .next()
        .isDone();
    // expect(gatawayFlow().next().value).toEqual(all([
    //   takeEvery(actionTypes.GET_GATEWAYS_REQUEST, getGatewaysFlow),
    //   takeEvery(actionTypes.SELECT_GATEWAY_REQUEST, selectGatewayFlow),
    // ])); ---> THIS TEST WORKS CORRECT
  });
});

我的测试没有通过。我的终端出现此错误。有什么想法可以解决吗?

【问题讨论】:

    标签: javascript unit-testing redux redux-saga redux-saga-test-plan


    【解决方案1】:

    您应该使用all([...effects]) - parallel effects 而不是{label: effect, ...} 形式的字典对象

    saga.ts:

    import { all, takeEvery, takeLatest } from 'redux-saga/effects';
    
    export const actionTypes = {
      GET_GATEWAYS_REQUEST: 'GET_GATEWAYS_REQUEST',
      SELECT_GATEWAY_REQUEST: 'SELECT_GATEWAY_REQUEST',
    };
    
    export function* getGatewaysFlow() {}
    export function* selectGatewayFlow() {}
    
    export default function* gatawayFlow() {
      yield all([
        takeEvery(actionTypes.GET_GATEWAYS_REQUEST, getGatewaysFlow),
        takeLatest(actionTypes.SELECT_GATEWAY_REQUEST, selectGatewayFlow),
      ]);
    }
    

    saga.test.ts:

    import { expectSaga, testSaga } from 'redux-saga-test-plan';
    import gatawayFlow, { actionTypes, getGatewaysFlow, selectGatewayFlow } from './saga';
    import { takeEvery, takeLatest } from '@redux-saga/core/effects';
    
    describe('Unit tests', () => {
      test('Test all effect', () => {
        const saga = testSaga(gatawayFlow);
        saga
          .next()
          .all([
            takeEvery(actionTypes.GET_GATEWAYS_REQUEST, getGatewaysFlow),
            takeLatest(actionTypes.SELECT_GATEWAY_REQUEST, selectGatewayFlow),
          ])
          .next()
          .isDone();
      });
    });
    

    测试结果:

     PASS   redux-saga-examples  packages/redux-saga-examples/src/stackoverflow/69252089/saga.test.ts
      Unit tests
        ✓ Test all effect (2 ms)
    
    ----------|---------|----------|---------|---------|-------------------
    File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
    ----------|---------|----------|---------|---------|-------------------
    All files |      75 |      100 |      20 |     100 |                   
     saga.ts  |      75 |      100 |      20 |     100 |                   
    ----------|---------|----------|---------|---------|-------------------
    Test Suites: 1 passed, 1 total
    Tests:       1 passed, 1 total
    Snapshots:   0 total
    Time:        3.553 s
    Ran all test suites related to changed files.
    

    【讨论】:

    • 谢谢。你能告诉我你是如何为 redux-saga-test-plan 运行覆盖脚本的吗?我试图运行这个npm test -- --coverage,但我只看到我用玩笑写的文件
    • @ElliZorro testPathIgnorePatterns 配置忽略node_modules,不需要测试第三方库。您应该使用完全自测的库
    猜你喜欢
    • 2019-07-20
    • 1970-01-01
    • 2017-09-17
    • 2017-04-07
    • 2022-01-08
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多