如何测试位于saga文件使用jest文件中的生成器函数中的yield delaysome_delay?

问题描述

saga文件中的

。生成器函数如下:

function* handleCall(){
   yield delay(1000);
   yield put(act.call());
}

我想测试test.js文件中的delay(1000)

解决方法

遵循 Unit Testing 文档。我们可以使用 testSaga 函数创建一个模拟 saga 供您断言效果。

例如

saga.ts

import { delay,put } from 'redux-saga/effects';
import * as act from './actionCreators';

export function* handleCall() {
  yield delay(1000);
  yield put(act.call());
}

actionCreators.ts

export function call() {
  return {
    type: 'HANDLE_CALL',};
}

saga.test.ts

import { testSaga } from 'redux-saga-test-plan';
import { handleCall } from './saga';
import * as act from './actionCreators';

describe('63494870',() => {
  it('should pass',() => {
    testSaga(handleCall).next().delay(1000).next().put(act.call()).next().isDone();
  });
});

测试结果:

 PASS  src/stackoverflow/63494870/saga.test.ts
  63494870
    ✓ should pass (3 ms)

-------------------|---------|----------|---------|---------|-------------------
File               | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
-------------------|---------|----------|---------|---------|-------------------
All files          |     100 |      100 |     100 |     100 |                   
 actionCreators.ts |     100 |      100 |     100 |     100 |                   
 saga.ts           |     100 |      100 |     100 |     100 |                   
-------------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed,1 total
Tests:       1 passed,1 total
Snapshots:   0 total
Time:        3.689 s

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...