question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Cannot read property 'createContext' of undefined. All files in index.ts compiling when only one is referenced.

See original GitHub issue

Issue:

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:

https://github.com/bscaspar/ts-jest-repro

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12

github_iconTop GitHub Comments

22reactions
iamtekestecommented, Aug 3, 2021

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 updating moduleDirectories entry from moduleDirectories: ["node_modules", "."] to moduleDirectories: ["node_modules", "<rootDir>"], in jest.config.js file. I found this solution from this PR.

4reactions
dannymidnightcommented, Feb 5, 2020

I think your issue is due to https://github.com/kulshekhar/ts-jest/issues/632

I managed to resolve this issue by updatingtsconfig.json compilerOptions to use esModuleInterop: true or alternatively update your imports as suggested above.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot read properties of undefined (reading 'createContext ...
im having some trouble with passing data from one child component to another child component using the context api. But i get this...
Read more >
Working with React Context in TypeScript
In React, one thing I generally see first when refactoring a codebase ... TypeError: Cannot read property 'name' of undefined. let { name, ......
Read more >
TypeScript for React Developers - freeCodeCamp
TypeScript or TS is an open source language developed and maintained ... file will be converted into an index.js that the browser can...
Read more >
typeerror: cannot read properties of null (reading 'usecontext')
1. You are trying to destruct the context from the object. const { showModal } = useContext (); But the initial value is...
Read more >
useContext - React Docs
You might be running into some build issue with your tooling that causes SomeContext as seen from the providing component and SomeContext as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found