Skip to content
This repository was archived by the owner on Jan 16, 2025. It is now read-only.

CustomEvent thrown TypeError is not an instance of TypeError #160

Closed
CMCDragonkai opened this issue Aug 30, 2023 · 4 comments
Closed

CustomEvent thrown TypeError is not an instance of TypeError #160

CMCDragonkai opened this issue Aug 30, 2023 · 4 comments

Comments

@CMCDragonkai
Copy link

I noticed this while using Node 20.x and using the new CustomEvent global.

Inside nodejs this answers true:

try { new CustomEvent('abc', 'abc') } catch (e) { console.log(e instanceof TypeError); }

However when running inside jest it ends up producing false instead.

Initial research points to transpilation being where the source of this problem is likely. However it could also be a jest upstream situation.

@kdy1
Copy link
Member

kdy1 commented Aug 30, 2023

Please provide a minimal repro

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Aug 30, 2023
@kdy1
Copy link
Member

kdy1 commented Aug 30, 2023

Roughly

function _instanceof(left, right) {
    if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
        return !!right[Symbol.hasInstance](left);
    } else {
        return left instanceof right;
    }
}
try {
    new CustomEvent("abc", "abc");
} catch (e) {
    console.log(_instanceof(e, TypeError));
}

@CMCDragonkai
Copy link
Author

CMCDragonkai commented Aug 30, 2023

I'm using:

  transform: {
    "^.+\\.(t|j)sx?$": [
      "@swc/jest",
      {
        jsc: {
          parser: {
            syntax: "typescript",
            tsx: true,
            decorators: compilerOptions.experimentalDecorators,
            dynamicImport: true,
          },
          target: compilerOptions.target.toLowerCase(),
          keepClassNames: true,
        },
      }
    ],
  },

Discovered here MatrixAI/js-events#2 while testing. I ended up just changing my code to do this e.name === 'TypeError' as that worked fine.

Where the target is "target": "ES2022",.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

2 participants