react-testing-library - Invalid hook call

Viewed 596

I'm using react-testing-library and I'm receiving "Invalid hook call. Hooks can only be called inside of the body of a function component." message.

Tests were working until I've extracted the hook to separate library, which I'm including with (package.json):

devDependencies: {
  "react-hooks": "../react-hooks",
}

I think it's interesting that it's throwing this error on calling hook in separate library (line 25), and not on calling hook inside the same library (line 24):

      23 | export default function Button({ id, onClick, children, ...props }: ButtonProps) {
      24 |   const theme = useThemeContext();
    > 25 |   const { isMouseOver, onMouseOver, onMouseOut } = useMouseOver();

It's working if I publish the library and use published version. It's also not working if I use yarn link instead.

I've also checked if there's multiple React instances, and it seems there aren't:

$ yarn list react
yarn list v1.22.0
warning package.json: No license field
└─ react@16.13.1
Done in 1.05s.

I'm bundling both libraries with parcel (tsdx). They don't have React bundled in them.

Any clue on what's going on?

0 Answers
Related