简体   繁体   English

ReactDOM act() function 使用酶导致混淆

[英]ReactDOM act() function causing confusion using Enzyme

I am trying to write a unit test to make sure my component for changing a user's password calls the correct functions inside onSubmit when the form is submitted.我正在尝试编写一个单元测试,以确保我的用于更改用户密码的组件在提交表单时调用onSubmit中的正确函数。 There are several other tests using the same method as this one and they all pass and give the same error.还有其他几个测试使用与此相同的方法,它们都通过并给出相同的错误。

Here's one of the tests:这是其中一项测试:

import { act } from "react-dom/test-utils";

it("should render an Alert component if password and confirmPassword do not match", async () => {
    global.scrollTo = jest.fn();
    const wrapper = mount(
        <Provider store={reduxStore}>
            <Router>
                <ChangePassword />
            </Router>
        </Provider>
    );

    pwFuncs.checkCurrentPassword = jest.fn(() => true);
    pwFuncs.comparePW = jest.fn(() => false);

    const fakeEvent = {
        preventDefault: () => console.log("Prevent default..."),
    };

    wrapper
        .find("input")
        .at(0)
        .simulate("change", {
            target: { name: "currentPassword", value: "password1234" },
        });
    wrapper
        .find("input")
        .at(1)
        .simulate("change", {
            target: { name: "newPassword", value: "1234drowssap" },
        });
    wrapper
        .find("input")
        .at(2)
        .simulate("change", {
            target: { name: "confirmPassword", value: "password4321" },
        });

    await act(async () => {
        wrapper.find("form").simulate("submit", fakeEvent);
    });

    expect(pwFuncs.checkCurrentPassword).toBeCalled();
    expect(pwFuncs.comparePW).toBeCalled();
    expect(wrapper.find(Alert).length).toEqual(1);
});

The test passes so I don't have any problems with the implementation but the act() function is giving me some trouble.测试通过了,所以我的实现没有任何问题,但是act() function 给我带来了一些麻烦。

Here's the error:这是错误:

console.error node_modules/react-test-renderer/cjs/react-test-renderer.development.js:131
Warning: It looks like you're using the wrong act() around your test interactions.
Be sure to use the matching version of act() corresponding to your renderer:

// for react-dom:
import {act} from 'react-dom/test-utils';
// ...
act(() => ...);

// for react-test-renderer:
import TestRenderer from 'react-test-renderer';
const {act} = TestRenderer;
// ...
act(() => ...);
    in ConnectFunction (at ChangePassword.js:191)
    in form (at ChangePassword.js:131)
    in div (at ChangePassword.js:130)
    in section (at ChangePassword.js:125)
    in main (at ChangePassword.js:124)
    in ChangePassword (created by Context.Consumer)
    in withRouter(ChangePassword) (created by ConnectFunction)
    in ConnectFunction (at ChangePassword.test.js:51)
    in Router (created by MemoryRouter)
    in MemoryRouter (at ChangePassword.test.js:50)
    in Provider (at ChangePassword.test.js:49)

Firstly, Enzyme's docs say that I shouldn't even have to wrap my state updating functions in act() because the mount function does this automatically (I'm using React v16.12.0), and yet without it the test suite fails to run saying act() is needed.首先,Enzyme 的文档说我什至不必在act()中包装我的 state 更新函数,因为mount function 会自动执行此操作(我使用的是 React v16.12.0),但没有它,测试套件将无法运行说act()是需要的。

From https://github.com/enzymejs/enzyme#reacttestutilsact-wrap来自https://github.com/enzymejs/enzyme#reacttestutilsact-wrap

If you're using React 16.8+ and.mount(), Enzyme will wrap apis including.simulate(), .setProps(), .setContext(), .invoke() with ReactTestUtils.act() so you don't need to manually wrap it.如果您使用 React 16.8+ 和 .mount(),Enzyme 将使用 ReactTestUtils.act() 包装 api,包括 .simulate()、.setProps()、.setContext()、.invoke(),因此您不需要手动包装它。

and secondly I've tried using:其次,我尝试使用:

import TestRenderer from 'react-test-rendrer';
const { app } = TestRenderer;

but it gives exactly the same error.但它给出了完全相同的错误。

I don't understand why the error is showing up since all the tests are passing anyway.我不明白为什么会出现错误,因为所有测试都通过了。 It's quite hard to find any info online referring to errors with act() so if anyone could shed any light on this that would be great.很难在网上找到任何关于act()错误的信息,所以如果有人能对此有所了解,那就太好了。

OK so there should have been more information in the question.好的,所以问题中应该有更多信息。 The component I am trying to test is wrapped in withRouter so when I added WrappedComponent...我要测试的组件被包装在withRouter中,所以当我添加 WrappedComponent...

const wrapper = mount(
    <Provider store={reduxStore}>
        <Router>
            <ChangePassword.WrappedComponent {...props} />
        </Router>
    </Provider>
);

...the error went away and all tests now pass normally. ...错误消失了,所有测试现在都正常通过了。 Hope this helps someone希望这可以帮助某人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM