reactive/data-client

Cannot import makeCacheProvider in rest-hook version 7 for hook unit tests

Closed this issue · 6 comments

React version
18.2.0

TypeScript version (if used)
N/A

Rest Hooks version (e.g., 6.3.11)
rest-hook 7.0.17
@rest-hooks/react 6.1.0
@rest-hooks/redux 6.3.6
@rest-hooks/test 9.1.4

Package manager version
yarn 1.22.19

Environment version
yarn test (otherwise chrome 114)

Node version
v16.20.0

Describe the bug
I upgraded rest-hooks to version 7 using the guide by running:
yarn add rest-hooks@7 @rest-hooks/react@6 @rest-hooks/redux@6 @rest-hooks/test@9
The application works fine but the tests, specifically the unit tests for hooks are failing with:
Cannot find module '@rest-hooks/react/makeCacheProvider' from 'node_modules/rest-hooks/dist/index.js'

I tried change the import from @rest-hooks/test to import makeCacheProvider from '@rest-hooks/react/makeCacheProvider'; by following the prepare for the future section (import { makeCacheProvider } from '@rest-hooks/test'; -> import makeCacheProvider from '@rest-hooks/react/makeCacheProvider';) but this changes nothing.
In fact, anything that in the project is imported instead using "rest-hooks" and not "@rest-hooks/react" seems to trigger this specific import error. I tried replacing every imports but it became error hell.
I confused as to why this import does not work while the documentation says using it should still be fine.
I am also getting the same error after updating all rest-hooks packages to latest version or latest version of the major version.
I also tried downgrading the @rest-hooks/test to version 7 and below to no avail.
I tried using CacheProvider instead of makeCacheProvider and same result.

To Reproduce
Steps to reproduce the behavior:

  1. Install rest-hooks version 7, have one import in the project on "rest-hooks" (not "@rest-hooks/react") and use it in a hook (?)
  2. Have a unit test for a hook
  3. Run yarn test

Expected behavior
Test pass

I managed to get a different error:

 FAIL  src/tests/frames/useFrames.test.js
  ● useFrames() should return frames

    TypeError: makeProvider is not a function

      47 |
      48 | test("useFrames() should return frames", async () => {
    > 49 |     const {result, waitForValueToChange} = renderRestHook(() => useFrames({
         |                                            ^
      50 |         uuid: "31b0fe17-11ec-4f86-8312-8cc24201a7f2",
      51 |         setUnavailable,
      52 |         authorized: true,

      at renderRestHook (node_modules/@rest-hooks/test/dist/index.js:227:22)
      at Object.<anonymous> (src/tests/frames/useFrames.test.js:49:44)

Currently on vacation. I think as a workaround for now you could try forcing @rest-hooks/react@6.0.1 and it will likely fix your problem.

@bparsy-odin Please try with @rest-hooks/react@6.2.0 and tell me if this works.

Thanks for your answer, after some more attempts at clean reinstalls by deleting anything rest-hooks related in my yarn.lock, I ended up with a working configuration:

    "@rest-hooks/hooks": "^3.1.14",
    "@rest-hooks/legacy": "8.1.2",
    "@rest-hooks/react": "^7.4.1",
    "@rest-hooks/rest": "^6.6.3",
    "@rest-hooks/test": "^10.3.7",
    "@testing-library/react-hooks": "^8.0.1",

Good to hear it's working for you now. Just out of curiosity, why did you update @rest-hooks/react to latest but not @rest-hooks/rest?

I believe I followed the upgrade guide, to upgrade from version 5 to 6: https://resthooks.io/docs/upgrade/upgrading-to-6,
the upgrade command is yarn add rest-hooks@6 @rest-hooks/rest@3
and then from 6 to 7:
yarn add rest-hooks@7 @rest-hooks/react@6 @rest-hooks/redux@6 @rest-hooks/test@9