Enzyme: Invalid hook call after dynamic `import`

Created on 28 Sep 2019  路  13Comments  路  Source: enzymejs/enzyme

Description

// Component.ts
import React, {useState} from 'react';
import { someDep } from 'some-dep';

const Component = () => {
  const [count] = useState(0);

  const someResult = someDep(count);

  return <div>{someResult}</div>
}

export default Component
// Component.spec.ts
import { mount } from 'enzyme'
import React from 'react'

describe('Component', async () => {
  beforeEach(() => jest.resetModules())

  it('should ...' () => {
    jest.doMock('some-dep')
                                   // same with require('./Component')
    const { default: Component } = await import(
      './Component'
    )

    const wrapper = mount(<Component />); // errors out here

    expect(...).to...
  })

  it(...)
);

I get a standard invalid hook call error.
Without dynamic imports and with global jest.mock everything works

Current behavior

_Invariant Violation: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons..._

Expected behavior

No invalid hook

Your environment

MacOS 10.14.6
ts-jest

API

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

Version

| library | version
| ------------------- | -------
| enzyme | 3.10.0
| react | 16.8.6
| react-dom | 16.8.6
| react-test-renderer | 16.9.0
| adapter (below) | 1.0.5

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 ( )
invalid

Most helpful comment

@sgb-io I think this should be the related issue: https://github.com/facebook/jest/issues/8987

All 13 comments

This seems like it's not an enzyme issue at all, since it's only happening with jest mocks.

Specifically, why are you using import() rather than require?

@ljharb I added the comment that with require the outcome is the same. ts-jest
dynamic imports seem to correctly give back the imported Component, require as well. The problem is when I pass it into render or mount it blows up. If I just jest.mock globally without dynamic imports it works.

What happens if you, instead of enzyme, do something like ReactDOM.render(<Component />, document.body)?

oh. also, mount takes an element, not a component. mount('<Component />), not mount(Component).

What happens if you, instead of enzyme, do something like ReactDOM.render(<Component />, document.body)?

Thanks for pointing. Let me check.

oh. also, mount takes an _element_, not a component. mount('<Component />), not mount(Component).

Typo in example. I do pass correct react element. But thanks for pointing out.

What happens if you, instead of enzyme, do something like ReactDOM.render(<Component />, document.body)?

Just checked. Same error with dynamic require() or import().

In that case, it's not an enzyme issue at all. It's likely a conflict between React hooks and jest mocking.

Since you now have a reproducible example that doesn't involve enzyme at all, you may want to file a bug on jest and/or react for this.

In that case, it's not an enzyme issue at all. It's likely a conflict between React hooks and jest mocking.

Since you now have a reproducible example that doesn't involve enzyme at all, you may want to file a bug on jest and/or react for this.

Thanks for helping out and thanks for recommendation. All the best.

@kapral18 is there a replacement issue in the react project you could link?

@sgb-io I think this should be the related issue: https://github.com/facebook/jest/issues/8987

@kapral18 is there a replacement issue in the react project you could link?

I didn't create one in react, I did create one though in jest mentioned by @sgb-io

@kapral18 Hey, did you find any solution? I have the same problem, I have a child component which I would like to mock with doMock but mocking doesn't work without any warning or error.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

blainekasten picture blainekasten  路  3Comments

timhonders picture timhonders  路  3Comments

AdamYahid picture AdamYahid  路  3Comments

potapovDim picture potapovDim  路  3Comments

ahuth picture ahuth  路  3Comments