Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dontMockByDefault not working? #43

Open
keithjgrant opened this issue Jun 29, 2021 · 2 comments
Open

dontMockByDefault not working? #43

keithjgrant opened this issue Jun 29, 2021 · 2 comments

Comments

@keithjgrant
Copy link

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
Copy link
Author

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
Copy link

Lige-w commented Apr 1, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants