mikeborozdin / jest-react-hooks-shallow

Bringing React Hooks to shallow rendering
ISC License
114 stars 9 forks source link

dontMockByDefault not working? #43

Open keithjgrant opened 3 years ago

keithjgrant commented 3 years ago

I've just installed jest-react-hooks-shallow and added enableHooks(jest, { dontMockByDefault: true }) in my test setup script, and I'm seeing countless test failures in tests that use mount(), multiple errors several similar to this:

Warning: React has detected a change in the order of Hooks called by Formik. This will lead to bugs and errors if not fixed. For more information, read the Rules of Hooks: https://fb.me/rules-of-hooks

       Previous render            Next render
       ------------------------------------------------------
    1. useRef                     useRef
    2. useRef                     useRef
    3. useRef                     useRef
    4. useRef                     useRef
    5. useRef                     useRef
    6. useRef                     useRef
    7. useReducer                 useEffect
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I'm using react-scripts (create-react-app), so I had to add enableHooks in the setupTests.js script they provide (adding setupFilesAfterEnv directly to my package.json caused an error telling me to add to this file instead, as react-scripts loads their setup file automatically).

Am I missing something? Or is there a problem with dontMockByDefault and react-scripts?

keithjgrant commented 3 years ago

Also, thank you for this library :). I've really missed shallow rendering since we've started using hooks more regularly, and I really hope I can get it working

Lige-w commented 2 years ago

I am also having this problem:

    "react": "^17.0.2",
    "@babel/preset-env": "^7.16.11",
    "@babel/preset-react": "^7.16.7",
    "@wojtekmaj/enzyme-adapter-react-17": "^0.6.6",
    "babel-jest": "^27.5.1",
    "enzyme": "^3.11.0",
    "jest": "^27.5.1",
    "jest-react-hooks-shallow": "^1.5.1",
    "regenerator-runtime": "^0.13.9"

dontMockByDefault seems to not be mocking the hooks for shallow renders as expected, but it's still causing problems with mount. I have found that if I wrap the tests with withoutHooks it works as expected, but my understanding is that this shouldn't be necessary if dontMockByDefault is set to true.