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

instanceof Error check returns unexpected result #3082

Closed
akphi opened this issue Dec 9, 2020 · 4 comments
Closed

instanceof Error check returns unexpected result #3082

akphi opened this issue Dec 9, 2020 · 4 comments

Comments

@akphi
Copy link

akphi commented Dec 9, 2020

Basic info:

  • Node.js version: 14.13.0
  • Yarn version: 1.22.5
  • jsdom version: 16.4.0
  • jest version: 26.6.3

Minimal reproduction case

I'm using JSDom via Jest, this is my repo repro
https://github.com/akphi/jest-jsdom-test/blob/main/src/basic.test.js for Jest
https://github.com/akphi/jest-jsdom-test/blob/main/index.js for native JSDOM example

try {
  // invalid URL to be thrown
  const url = new URL("something");
} catch (e) {
  console.log(e instanceof Error); // expect `true` but returns `false`
}

How does similar code behave in browsers?

It behaves as expected in the browser: i.e. instanceof Error returns true

Link: https://codesandbox.io/s/xenodochial-gauss-2cqjx?file=/src/App.tsx

Points to consider

The gist of the problem is by providing an invalid base URL, the package whatwg-url used by jsdom throws a TypeError which gets picked up by my test, but fails my instanceof Error check.

I did a little bit of digging, there are many issues raised regarding sandboxing or instanceof check, though I don't understand fully, I see 2 categories:

  1. Misconfigured JSDom, such as in Implement Blob.stream, Blob.text and Blob.arrayBuffer #2555. But as mentioned, I use Jest with default settings so I hope I did not misconfigured anything here
  2. Something related to sandboxing and the fact that jsdom Error is somehow different than the Error in my instanceof check.
@domenic
Copy link
Member

domenic commented Dec 9, 2020

Can you provide a reproduction that doesn't use Jest? We cannot help debug Jest usage.

@akphi
Copy link
Author

akphi commented Dec 9, 2020

@domenic Sure! I just updated my repo. The code is this

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

const dom = new JSDOM(`<body>
  <script>
    try {
      // invalid URL to be thrown
      const url = new URL("something");
    } catch (e) {
      console.log(e instanceof Error); // expect true
    }
  </script>
</body>`, { runScripts: "dangerously" });

// `node index.js` prints `false` 

@domenic
Copy link
Member

domenic commented Dec 9, 2020

Thanks. I guess then this is "Errors and other globals from the outer Node.js environment are still used" from #2727, which is pretty hard to fix.

@akphi
Copy link
Author

akphi commented Dec 9, 2020

@domenic gotcha, just want to confirm that! Will close this to track #2727
Thanks!

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

No branches or pull requests

2 participants