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

throw undefined turns into a TypeError: Cannot read property 'message' of undefined #3183

Closed
mbest opened this issue May 3, 2021 · 3 comments · Fixed by #3185
Closed

throw undefined turns into a TypeError: Cannot read property 'message' of undefined #3183

mbest opened this issue May 3, 2021 · 3 comments · Fixed by #3185

Comments

@mbest
Copy link
Contributor

mbest commented May 3, 2021

Basic info:

  • Node.js version: 12.16.1
  • jsdom version: 16.5.1

Minimal reproduction case

const { JSDOM } = require("jsdom");

const dom = new JSDOM(`<div id="test"></div>`);
const test = dom.window.document.getElementById("test");
test.onclick = () => { throw undefined; };
test.click();

result:

Uncaught TypeError: Cannot read property 'message' of undefined
    at reportException (E:\Code\XS.AMC.UX\node_modules\jsdom\lib\jsdom\living\helpers\runtime-script-errors.js:58:84)
    at innerInvokeEventListeners (E:\Code\XS.AMC.UX\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:341:9)
    at invokeEventListeners (E:\Code\XS.AMC.UX\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:274:3)
    at HTMLDivElementImpl._dispatch (E:\Code\XS.AMC.UX\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:221:9)
    at fireAnEvent (E:\Code\XS.AMC.UX\node_modules\jsdom\lib\jsdom\living\helpers\events.js:18:36)
    at HTMLDivElementImpl.click (E:\Code\XS.AMC.UX\node_modules\jsdom\lib\jsdom\living\nodes\HTMLElement-impl.js:79:5)
    at HTMLDivElement.click (E:\Code\XS.AMC.UX\node_modules\jsdom\lib\jsdom\living\generated\HTMLElement.js:110:34)

How does similar code behave in browsers?

image

Why is this important?

We're trying to test a React component that throws undefined and is caught by an error boundary. But instead of the error boundary seeing undefined, it sees an Error with a message of Cannot read property 'message' of undefined.

@TimothyGu
Copy link
Member

TimothyGu commented May 3, 2021

If this is an important use case for you, could you submit a pull request to fix the function in the backtrace to deal better with undefined (and other non-Error objects)?

module.exports = function reportException(window, error, filenameHint) {
// This function will give good results on real Error objects with stacks; poor ones otherwise

@mbest
Copy link
Contributor Author

mbest commented May 3, 2021

Thanks for the quick reply. I'm not familiar with the jsdom code, but I can take a stab at it.

@mbest
Copy link
Contributor Author

mbest commented May 25, 2021

Published in 16.6.0

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

Successfully merging a pull request may close this issue.

2 participants