首页 > 解决方案 > 如果结合 saga 测试 (*.spec.ts) 和组件测试 (*tsx),使用 mocha 运行测试会超时

问题描述

我有一个项目,我在其中编写了测试:

  1. 包含 sagas 的打字稿文件

    mySaga.test.ts

使用 redux-saga-test-plan 和 sinon 进行模拟

  1. tsx 组件

    MyComponent.test.tsx

当我在单独的测试运行中运行每种测试类型时,

spec: ["./test/**/*.test.ts"]

或者

spec: ["./test/**/*.test.tsx"]

他们都成功了。

但是,如果我在一次测试中一起运行所有测试

spec: ["./test/**/*.test.ts", "./test/**/*.test.tsx"]

现在,所有 saga 测试都因 mocha 的超时消息而失败。所有组件测试仍然成功。

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

我试过提高超时但没有成功。这是来自 saga 测试的示例测试代码:

describe("MyWonderfulSagas", () => {
    let readStub: any;

    describe("mySaga1", () => {
        afterEach(() => {
            readStub.restore();
        });
        it("should dispatch an action containing data from the local storage", () => {
            readStub = sinon.stub(StorageHelper, "read").resolves(mockData);
            return expectSaga(mySaga1)
                .dispatch(DataActions.loadData())
                .put({
                    type: DataActions.dataSet.type,
                    payload: mockData
                })
                .silentRun()
                .then(result => {
                    sinon.assert.calledOnce(readStub);
                });
        });
        
    });

});

被测试的 saga 看起来像这样(加载数据是另一个加载数据并调度结果的生成器函数):

export function* mySaga1(): SagaIterator {
    yield takeEvery(DataActions.loadData.type, loadData);
}

有没有人遇到过这个问题?我需要在哪里寻找这个问题?在 redux-saga-test-plan 或 mocha 或...?

标签: typescriptunit-testingmocha.jsredux-saga-test-plan

解决方案


推荐阅读