enzyme icon indicating copy to clipboard operation
enzyme copied to clipboard

It looks like you're using the wrong act() around your test interactions.

Open hiennguyenvan-agilityio opened this issue 4 years ago • 5 comments

Current behavior

I got warning It looks like you're using the wrong act() around your test interactions. when call store.dispatch in act

import {act} from 'react-dom/test-utils'

const initState = {
  auth: {
    authState: null
  }
}

const signedInState = {
  auth: {
    authState: 'signedIn'
  }
}

const store = createMockStore()

const AuthWithStore = () => (
  <Provider store={store}>
    <Auth>
      <ChildComponent />
    </Auth>
  </Provider>
)

describe('', () => {
    let wrapper;

    beforeAll(() => {
      wrapper = mount(<AuthWithStore />)
    })

    it('should render Authenticator as default', () => {
      expect(wrapper.find(Authenticator)).toHaveLength(1)
      expect(wrapper.find(ChildComponent)).toHaveLength(0)
    })

    it('should render ChildComponent after signIn', () => {
      act(() => {
        store.dispatch({
          type: 'CHANGE_STATE',
          payload: signedInState
        })
      })
      wrapper.update();

      console.log(wrapper.debug())

      expect(wrapper.find(Authenticator)).toHaveLength(0)
      expect(wrapper.find(ChildComponent)).toHaveLength(1)
    })
  })

All test case is pass but i got warning

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 Auth (created by ConnectFunction)
        in ConnectFunction (created by AuthWithStore)
        in Provider (created by AuthWithStore)
        in AuthWithStore

I tried use act for react-test-renderer but the warning is same

import TestRenderer from 'react-test-renderer';
const {act} = TestRenderer;

Expected behavior

Your environment

API

  • [ ] shallow
  • [x] mount
  • [ ] render

Version

library version
enzyme 3.11.0
react 16.12.0
react-dom 16.12.0
react-test-renderer 16.12.0
adapter (below) 1.15.2

Adapter

  • [x] enzyme-adapter-react-16
  • [ ] enzyme-adapter-react-16.3
  • [ ] enzyme-adapter-react-16.2
  • [ ] enzyme-adapter-react-16.1
  • [ ] enzyme-adapter-react-15
  • [ ] enzyme-adapter-react-15.4
  • [ ] enzyme-adapter-react-14
  • [ ] enzyme-adapter-react-13
  • [ ] enzyme-adapter-react-helper
  • [ ] others ( )

hiennguyenvan-agilityio avatar Apr 10 '20 09:04 hiennguyenvan-agilityio

Please fill out the versions table in the issue template.

ljharb avatar Apr 10 '20 16:04 ljharb

@ljharb Updated

hiennguyenvan-agilityio avatar Apr 10 '20 18:04 hiennguyenvan-agilityio

You're importing the one from react-dom; the error message tells you that you need to import the one from react-test-renderer.

ljharb avatar Apr 12 '20 20:04 ljharb

I tried use react-test-renderer

import TestRenderer from 'react-test-renderer';
const {act} = TestRenderer;

It's same warning. But I trigger UI click action by wrapper.find().simulate() instead of store.dispatch the warning is gone

hiennguyenvan-agilityio avatar Apr 17 '20 11:04 hiennguyenvan-agilityio

if you are using Yarn Workspaces make sure you don't have multiple installs of react-dom. I was having issues with nohoist, and had one react-dom in the root and one inside the enzyme adapter. After fixing this, the warning got fixed too

fedebertolini avatar Dec 11 '20 17:12 fedebertolini