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

panic when using jest, jsdom and TextEncoder #2532

Closed
Janpot opened this issue Sep 8, 2022 · 5 comments
Closed

panic when using jest, jsdom and TextEncoder #2532

Janpot opened this issue Sep 8, 2022 · 5 comments

Comments

@Janpot
Copy link

Janpot commented Sep 8, 2022

Minimal reproduction repo: https://github.com/Janpot/jest-esbuild-repro

Running esbuild in jest with this particular TextEncoder, textDecoder polyfill setup started breaking from [email protected] onward.

To reproduce, clone the repo, run yarn && yarn test. It will error with

 FAIL  ./index.spec.js
  ✕ repro (18 ms)

  ● repro

    panic: interface conversion: interface {} is map[string]interface {}, not []uint8

    debug.Stack (runtime/debug/stack.go:24)
    helpers.PrettyPrintedStack (internal/helpers/stack.go:9)
    main.(*serviceType).handleIncomingPacket.func1 (cmd/esbuild/service.go:220)
    panic (runtime/panic.go:884)
    main.(*serviceType).handleTransformRequest (cmd/esbuild/service.go:923)
    main.(*serviceType).handleIncomingPacket (cmd/esbuild/service.go:236)
    main.runService.func3 (cmd/esbuild/service.go:163)
    created by main.runService (cmd/esbuild/service.go:162)

      at node_modules/esbuild/lib/main.js:1400:29

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.41 s, estimated 1 s

The README contains a few of the tweaks that all make the error go away.

environment: node v16.17.0 on MacOs

@Janpot Janpot changed the title panic when using jsdom and TextEncoder panic when using jest, jsdom and TextEncoder Sep 8, 2022
@evanw
Copy link
Owner

evanw commented Sep 8, 2022

It's trivial to break esbuild by running it in a broken JavaScript environment. This is not surprising, and does not indicate a problem with esbuild. You can get a similar error by running this code in node without Jest, for example:

global.TextEncoder = class { encode = x => x }
const esbuild = require('esbuild')
esbuild.transform('1+2').then(x => console.log(x))

The support code for esbuild relies on a sane and functioning TextEncoder object. In Jest's case, it seems to have a bug where new TextEncoder().encode() instanceof Uint8Array is incorrectly false. They should fix their bug.

Edit: Or maybe you have written the bug yourself in jest-setup.js? I'm not sure; I know nothing about Jest. Anyway this problem is not esbuild's doing.

@evanw
Copy link
Owner

evanw commented Sep 8, 2022

Closing as out of scope. In a future breaking change, I could have esbuild do a sanity check for this edge case and throw an error earlier if this API is broken.

@evanw evanw closed this as not planned Won't fix, can't repro, duplicate, stale Sep 8, 2022
@Janpot
Copy link
Author

Janpot commented Sep 8, 2022

Thank you for taking the time, this helped me narrow down the issue further jestjs/jest#13227

@evanw
Copy link
Owner

evanw commented Sep 8, 2022

FWIW something like this seems to work:

const util = require("util");

if (!global.TextDecoder) {
  global.TextDecoder = util.TextDecoder;
}

if (!global.TextEncoder) {
  global.TextEncoder = class extends util.TextEncoder {
    encode(text) {
      return new Uint8Array(super.encode(text));
    }
  };
}

In case you're still working on this.

@Janpot
Copy link
Author

Janpot commented Sep 8, 2022

Yes, thank you. I considered it, but ultimately the test that triggered this issue shouldn't have been using the jsdom environment at all. I just made sure it uses the correct environment and that solves it as well.

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