Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsdom with CRA throwing TypeError _ownerDocument null

I have a craco project that runs just fine, but when running tests with jest it throws the following error. I don't even have jsdom installed, it appears to be a peer dependency of craco? No idea how to solve this one, but can clarify any questions in the comments since I don't know what is causing this.

/Users/munsterberg/code/creatordao/creatordao-portal/node_modules/jsdom/lib/jsdom/living/xhr/XMLHttpRequest-impl.js:111
      referrer: _ownerDocument.URL,
                               ^

TypeError: Cannot read properties of null (reading 'URL')
    at new XMLHttpRequestImpl (/Users/munsterberg/code/creatordao/creatordao-portal/node_modules/jsdom/lib/jsdom/living/xhr/XMLHttpRequest-impl.js:111:32)
    at Object.exports.setup (/Users/munsterberg/code/creatordao/creatordao-portal/node_modules/jsdom/lib/jsdom/living/generated/XMLHttpRequest.js:62:12)
    at new XMLHttpRequest (/Users/munsterberg/code/creatordao/creatordao-portal/node_modules/jsdom/lib/jsdom/living/generated/XMLHttpRequest.js:101:22)
    at /Users/munsterberg/code/creatordao/creatordao-portal/node_modules/whatwg-fetch/dist/fetch.umd.js:514:17
    at new Promise (<anonymous>)
    at fetch (/Users/munsterberg/code/creatordao/creatordao-portal/node_modules/whatwg-fetch/dist/fetch.umd.js:507:12)
    at /Users/munsterberg/code/creatordao/creatordao-portal/node_modules/@firebase/installations/src/functions/create-installation-request.ts:54:51
    at /Users/munsterberg/code/creatordao/creatordao-portal/node_modules/@firebase/installations/src/functions/common.ts:94:24
    at step (/Users/munsterberg/code/creatordao/creatordao-portal/node_modules/@firebase/installations/node_modules/tslib/tslib.js:143:27)
    at Object.next (/Users/munsterberg/code/creatordao/creatordao-portal/node_modules/@firebase/installations/node_modules/tslib/tslib.js:124:57)

I noticed firebase is a part of the stack trace. Is it possible this is related?

like image 291
Munsterberg Avatar asked Mar 29 '26 17:03

Munsterberg


2 Answers

After hours of debugging I found out it was related to the getAnalytics function exported from the firebase/analytics package. The fix for me was to mock the getAnalytics function as it wasn't required for testing purposes anyways.

like image 107
Munsterberg Avatar answered Apr 02 '26 20:04

Munsterberg


Just a few tips for future uses:

  • When you encounter errors, especially the ones with long traceback errors (e.g. HTTP requests), you should look out for the errors:
    at /Users/munsterberg/code/creatordao/creatordao-portal/node_modules/@firebase/installations/src/functions/create-installation-request.ts:54:51
    at /Users/munsterberg/code/creatordao/creatordao-portal/node_modules/@firebase/installations/src/functions/common.ts:94:24
    at step (/Users/munsterberg/code/creatordao/creatordao-portal/node_modules/@firebase/installations/node_modules/tslib/tslib.js:143:27)
    at Object.next (/Users/munsterberg/code/creatordao/creatordao-portal/node_modules/@firebase/installations/node_modules/tslib/tslib.js:124:57)

Here @firebase package is mentioned quite a few times, functions too, so you can deduce it has something to with it, especially with it under node_module.

  • Quick point out on mock functions, which are:

special functions that allow us to track how a particular function is called by external code

So mocking a function can actually be important, especially when running tests with external packages and files.

Related:

  • Pluralsight.com guides
like image 33
DialFrost Avatar answered Apr 02 '26 20:04

DialFrost



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!