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

Cannot read property createContext of undefined with Jest #2339

Closed
TrueWill opened this issue Jun 2, 2021 · 11 comments · Fixed by #2884
Closed

Cannot read property createContext of undefined with Jest #2339

TrueWill opened this issue Jun 2, 2021 · 11 comments · Fixed by #2884
Labels
help wanted Extra attention is needed v4
Milestone

Comments

@TrueWill
Copy link

TrueWill commented Jun 2, 2021

Describe the bug

Getting

TypeError: Cannot read property 'createContext' of undefined

on import { useQuery } from 'react-query' when running tests with Jest.

To Reproduce
Steps to reproduce the behavior:

We saw this when using a jest.config.js with the line moduleDirectories: ['node_modules', './'],
react-query v3.16.0

Expected behavior
Every other library works fine with Jest.

Desktop (please complete the following information):

  • OS: macOS Catalina
  • Browser: n/a - running tests with Jest v26.6.3 and Node v14.17.0

Additional context

A coworker figured out a fix based on blitz-js/blitz#1323 - changing the Jest config to moduleDirectories: ['node_modules', '<rootDir>'],. But this is the only library that's affected by it. According to the linked issue, this is caused by react-query having require("react") inside a folder named react.

@brainer1
Copy link

brainer1 commented Jun 10, 2021

+1, having the same issue here
Edit: the fix you posted worked for me :)

@TkDodo
Copy link
Collaborator

TkDodo commented Jun 27, 2021

This is not the first time that having a sub-directory named react has led to some confusion for bundlers. I’d generally support a rename, not sure to what though (suggestions welcome), and also not sure if @tannerlinsley agrees :)

@TimMouskhelichvili
Copy link

Hey any updates on this?

@TkDodo
Copy link
Collaborator

TkDodo commented Oct 3, 2021

should we just rename it to reactjs ?

@TkDodo
Copy link
Collaborator

TkDodo commented Oct 9, 2021

@TrueWill would you like to contribute this?

@TkDodo
Copy link
Collaborator

TkDodo commented Oct 23, 2021

Any updates here? If someone wants to contribute the change, I’d be happy to look at it and merge it

@TrueWill
Copy link
Author

TrueWill commented Nov 1, 2021

@TrueWill would you like to contribute this?

Sorry - I don't even have time to update my own OS projects right now. :/

@TkDodo TkDodo added the help wanted Extra attention is needed label Nov 1, 2021
@EddyVinck
Copy link
Contributor

I am going to try to fix this on Friday, we have an OSS contribution day at work.

@EddyVinck
Copy link
Contributor

EddyVinck commented Nov 5, 2021

@TrueWill @TkDodo I tried creating a reproduction in CodeSandbox but I can't reproduce it there

https://codesandbox.io/s/react-query-2339-xgupz?file=/src/TestComponent.test.js

I'll try to create a reproduction locally

Update: reproduction repo https://github.com/EddyVinck/react-query-2339

image

Running into some issues with yarn link causing me to have multiple React versions, might take a bit longer than I thought

@EddyVinck
Copy link
Contributor

PR open 🚀

@TkDodo TkDodo added the v4 label Nov 17, 2021
@TkDodo TkDodo added this to the v4 milestone Nov 17, 2021
@TkDodo
Copy link
Collaborator

TkDodo commented Nov 17, 2021

will be available in v4.

@TkDodo TkDodo closed this as completed Nov 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed v4
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants