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

[Bug]: jest-environment-jsdom messes with global Uint8Array #13227

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

[Bug]: jest-environment-jsdom messes with global Uint8Array #13227

Janpot opened this issue Sep 8, 2022 · 4 comments

Comments

@Janpot
Copy link

Janpot commented Sep 8, 2022

Version

29.0.2

Steps to reproduce

  1. clone https://github.com/Janpot/jest-esbuild-repro
  2. run yarn && yarn test
  3. observe failing test
  4. remove testEnvironment: "jest-environment-jsdom", from 'jest.config.js'
  5. observe test passing

Expected behavior

The test should pass, jsdom shouldn't touch Uint8Array if it exists in global scope

Actual behavior

the test fails, jsdom seems to have overwritten Uint8Array

Additional context

No response

Environment

System:
    OS: macOS 12.4
    CPU: (8) arm64 Apple M1
  Binaries:
    Node: 16.17.0 - ~/.nvm/versions/node/v16.17.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.17.0/bin/yarn
    npm: 8.15.0 - ~/.nvm/versions/node/v16.17.0/bin/npm
  npmPackages:
    jest: ^29.0.2 => 29.0.2
@Janpot
Copy link
Author

Janpot commented Sep 8, 2022

Ok, after reading #9983 I don't imagine this will get fixed. Closing proactively

@Janpot Janpot closed this as completed Sep 8, 2022
@SimenB
Copy link
Member

SimenB commented Sep 8, 2022

Yeah, the issue is that jsdom is missing TextEncoder (but does have Uint8Array). You're pulling TextEncoder in from util which is a node library not meant to run in a browser environment.

nodejs/node#31852 would also fix your instanceof issue, but the main problem here is that you're trying to mix Node realm into the JSDOM (vm) realm.


If you really want to check the type in the test, you can use https://nodejs.org/api/util.html#utiltypesisuint8arrayvalue which works cross-realm.

diff --git i/index.spec.js w/index.spec.js
index 477703f..491cb60 100644
--- i/index.spec.js
+++ w/index.spec.js
@@ -2,5 +2,5 @@ const util = require("util");
 
 test("repro", async () => {
   const encoder = new util.TextEncoder();
-  expect(encoder.encode("€")).toBeInstanceOf(Uint8Array);
+  expect(util.types.isUint8Array(encoder.encode("€"))).toBe(true);
 });

This works for both node and jsdom envs.

@Janpot
Copy link
Author

Janpot commented Sep 8, 2022

Yep, thank you!

If you really want to check the type in the test

Not really, another library crashed on Uint8Array not being the node.js implementation. I ultimately took a step back and sorted out polyfilling the jsdom environment differently (as custom testEnvironment and not in setupFilesAfterEnv). That way the node environment doesn't get tainted as well.

@github-actions
Copy link

github-actions bot commented Oct 9, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants