-
Notifications
You must be signed in to change notification settings - Fork 44
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
jest jsdom: ReferenceError: TextEncoder is not defined #1676
Comments
A workarround (or solution?) is to polyfill TextEncoder and TextDecoder in a setupFilesAfterEnv script: import { TextEncoder, TextDecoder } from 'util';
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder; |
Hi @angelo-v ! I think this issue unfortunately goes beyond the scope of |
Thanks, I understand that. Using setupFilesAfterEnv works as well and is more lightweight then creating a custom jest env imo. Perhaps you can at least add a hint to the documentation about that, because usually people just using the inrupt lib do not know about jose and those internals, they just wonder why the inrupt lib causes an error in the tests |
Still an issue today. Jest 29, added the util script |
just import it with require |
How exactly do I add this to a setupFilesAfterEnv script? I am using typescript and I have a jest-config.d.ts and it has this inside,
|
We actually have a bit of an update here: we've just released a package that helps by polyfilling the required Web APIs: https://www.npmjs.com/package/@inrupt/jest-jsdom-polyfills It's still being rolled out across the SDK repos, but in our testing so far it should provide everything needed. We're aware that there may be an issue with UInt8Arrays, and that's due to an underlying jsdom issue: jsdom/whatwg-encoding#13 |
@hjohnsick you can see an example here: https://github.com/inrupt/solid-client-access-grants-js/pull/366/files You should likely have a |
@angelo-v Yup, we agree, we've since moved from custom environments to setupFilesAfterEnv |
Seems like the answers didnt state where to put the said code. |
You can use this repository as an example: see https://github.com/inrupt/solid-client-authn-js/blob/main/jest.setup.ts, where the polyfills are imported, and solid-client-authn-js/jest.config.ts Line 39 in 72cd348
|
Only solution worked to me is to add this line of code to
|
It's strange you needed that as we do that polyfill of textencoder in our polyfill.. I can't comment on testing lib & jest-dom though. If you've a repo to share I'd love to run a few experiments to check our package is working as expected |
Problem still present. Adding this to setupTests.js still works: import { TextEncoder } from 'util';
global.TextEncoder = TextEncoder; |
Closing this issue as it is an error in Jest with a workaround (i.e. #1676 (comment)) |
This worked for me when using CRA. |
This did not work for me. Still getting ' |
Solution above did not work for me either, after some trail and error found this to work for me:
|
@BowaDAO can you describe your setup a bit more so that I would be able to help? @Alfredoeb9 thanks for providing something that works around the issue in your case. Can you elaborate a bit more on your setup as well, so that I may figure out why the solution we use for ourselves now doesn't cover your use case? |
I've been able to resolve the issue. |
import { TextEncoder, TextDecoder } from 'util';
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder; |
I am also having the same issue and it does not help me to resolve that. :( |
Search terms you've used
jsdom, TextEncoder
Impacted package
Which packages do you think might be impacted by the bug ?
Bug description
importing from
@inrupt/solid-client-authn-browser
fails in a jest test using jsdom environment:To Reproduce
Run this test:
Expected result
Test runs successfully
Actual result
ReferenceError: TextEncoder is not defined
Environment
Additional information
@jest-environment node
The text was updated successfully, but these errors were encountered: