TypeError: Cannot read property 'createContext' of undefined. All files in index.ts compiling when only one is referenced.
See original GitHub issueIssue:
I am getting an error that references a component unrelated to my test. This appears to be happening because components are exported from an index.ts file - when the component I am testing references a different component in this index.ts file, all components exported from the index.ts the file are compiled, even ones not used in the component being tested.
In this case, the error is happening because I am using React Context in a component exported from the index.ts file. In the repo below, when Component3 is not exported from the index.ts file, component1.test.js works as expected. When Component3 is included in the index.ts file, the test fails with the following error. (Component3 isn’t used anywhere in Component1).
This is a bit of a mouthful but the repo should make it clear - comment out line 3 in src/index.ts and jest
works as expected. Uncomment and it fails.
Test suite failed to run
TypeError: Cannot read property 'createContext' of undefined
Expected behavior:
The unreferenced component in index.ts is not compiled and does not throw an error in my test.
I wasn’t able to get a log file printed out, I’m happy to add it with a little direction on how to do so.
Minimal repo:
Issue Analytics
- State:
- Created 4 years ago
- Comments:12
Top GitHub Comments
For anyone else landing here because they searched for a react-query issue related to
Cannot read property 'createContext' of undefined
the solution for me was updatingmoduleDirectories
entry frommoduleDirectories: ["node_modules", "."]
tomoduleDirectories: ["node_modules", "<rootDir>"],
injest.config.js
file. I found this solution from this PR.I think your issue is due to https://github.com/kulshekhar/ts-jest/issues/632
I managed to resolve this issue by updating
tsconfig.json
compilerOptions to useesModuleInterop: true
or alternatively update your imports as suggested above.