【问题标题】:callback not executing in JEST testing?回调未在 JEST 测试中执行?
【发布时间】:2022-01-27 12:25:48
【问题描述】:

useEffect 内部的回调未在 JEST 测试中执行?我是否必须等待或其他什么。 useEffect 中的重复回调未在 JEST 测试中执行?我是否必须等待或其他。 useEffect 中的重复回调未在 JEST 测试中执行?我必须等待还是其他什么。

【问题讨论】:

    标签: reactjs testing jestjs use-effect


    【解决方案1】:

    从函数调用的名称来看,是的,您必须等待。 fetch 表示您正在对数据执行异步(承诺)调用。这很可能会设置某种形式的状态。这样做会重新渲染一些东西(这需要一点时间)。等待数据更改是编写测试时最难处理的问题之一。

    【讨论】:

    • 我是 JEST 测试的新手。请问您能告诉我如何将测试包起来等待吗?
    • it("检查 TrendsPageContainer", async () => { const props = { history: {}, location: {}, activeLang: "hi", activeLangEn: "Hindi", 匹配: { params: {} } }; const component = await renderer.create( ); const tree = component.toJSON();expect(tree).toMatchSnapshot();});
    • 这不起作用
    • 是的,我正在使用 React-Testing-Library
    • it("check for TrendsPageContainer", async () => { const props = { history: {}, location: {}, activeLang: "hi", activeLangEn: "Hindi", match: { params: {} }, }; const component = renderer.create( <Provider store={store}> {" "} <Router> {" "} <TrendsPageContainer {...props} />{" "} </Router>{" "} </Provider> ); const tree = component.toJSON(); await waitFor(() => { expect(tree).toMatchSnapshot(); }); });
    猜你喜欢
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2021-08-08
    • 2019-09-21
    • 2023-04-06
    相关资源
    最近更新 更多