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

[Usage with Vitest/JSDOM] payload must be an instance of Uint8Array #671

Closed
2 tasks done
raulfdm opened this issue May 6, 2024 · 2 comments
Closed
2 tasks done

Comments

@raulfdm
Copy link

raulfdm commented May 6, 2024

What happened?

Hey folks 👋

I'm opening this issue just for cross-reference because I couldn't find anything in the repo.

I was writing a tiny function that generates a signed JWT from a payload and when I tried to write a unit test, it started failing:

TypeError: payload must be an instance of Uint8Array
 ❯ new FlattenedSign node_modules/jose/dist/node/esm/jws/flattened/sign.js:14:19
 ❯ new CompactSign node_modules/jose/dist/node/esm/jws/compact/sign.js:5:27
 ❯ SignJWT.sign node_modules/jose/dist/node/esm/jwt/sign.js:12:21
 ❯ Module.generateJWT src/generate-jwt.js:20:6
     18|     .setProtectedHeader({ alg: "RS256" })
     19|     .setExpirationTime("5m") // 5 minutes from now
     20|     .sign(privateKey);
       |      ^
     21|
     22|   return jwt;
 ❯ src/generate-jwt.test.js:13:17

After some investigation, I realize that this happened because I had JSDOM as test environment. The Uint8Array class used to encode the payload isn't the same when the Sign class use to check if it's instance of Uint8Array

If you're curious, watch this video
CleanShot.2024-05-06.at.15.51.52.mp4

I don't think there's nothing to you to do here though. Just opened this to have some cross reference because I couldn't find this problem reported anywhere.

Links

Version

5.2.4

Runtime

Node.js

Runtime Details

node 20.10.0

Code to reproduce

- https://github.com/raulfdm/jsdom-jose-crypto-bug

Required

  • I have searched the issues tracker and discussions for similar topics and couldn't find anything related.
  • I agree to follow this project's Code of Conduct
@raulfdm raulfdm added the triage label May 6, 2024
@raulfdm raulfdm changed the title [Usage with Vitest/JSDOM] [Usage with Vitest/JSDOM] payload must be an instance of Uint8Array May 6, 2024
@panva
Copy link
Owner

panva commented May 6, 2024

jest is also notorious for this nonsense with jsdom #441

As you said, there's nothing I can do here but ask myself why do tools insist on messing with globally exposed classes and why is this acceptable for its users.

Great video btw!

The root cause is also clear now: jsdom/jsdom#3711 (comment)

@panva panva closed this as completed May 6, 2024
@panva panva removed the triage label May 6, 2024
@raulfdm
Copy link
Author

raulfdm commented May 8, 2024

@panva Found the guilty: vitest-dev/vitest#5685

I don't have any context about why vitest needs to do such global class manipulation, but it's their fault.

@github-actions github-actions bot locked and limited conversation to collaborators Aug 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants