You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
jest jsdom: ReferenceError: TextEncoder is not defined #1676
Reacted by Joe McGrath, William N., Hisa Ishibashi, Dezone, Jhonatan Macazana, Maël Mayon, C. T. Lin, Javad Borhani, Etienne Martin, Branden Barber and 58 more
Reacted by TT, Igua95, Sergio Andres Martinez, Serhii Sakal, Randhir Kumar Singh, Luiz Fernando, Moses Gitau, Julian Schmidt, Aldin Nezirić, TonyaV and 2 more
Reacted by Nick Virden, Randhir Kumar Singh, Luiz Fernando, Richard T. Miles, Julian Schmidt, Aldin Nezirić, Roberto Decurnex, Trieu Sagi and Luis Eduardo Brito
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
Reacted by Zwifi, Emelia Smith, Tim Berners-Lee, Johnny Hu, Berkan Bilgin and Daniel B.
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
Reacted by Dmitrii Malinovskii, Jacob A. Carpenter, Steven D'Hondt, Furkan , Jean-Paul NM, jianwu and Tim Frey
You should likely have a jest.config.ts file in your repo, unless you're somehow using jest without any configuration at all (which I've only rarely seen)
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
@angelo-v Yup, we agree, we've since moved from custom environments to setupFilesAfterEnv
Reacted by Shabith Thennakone, Philippe Morier, Jander Silva, Miguel Ángel Rodríguez, Daniel B., Sergey Lebedev, Dan Edvard Halvorsen, Denis Bessa, Tainá Santana and Thong Le
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
Reacted by mgriffoul and Gautam Thapar
Reacted by mgriffoul, Gautam Thapar and Philippe Dionne
@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 just updated my react app to node 20 from 16. I am still having this issue. Also, my react builds using cra through craco. Any suggestions? I have tried assigning TextEncoder/Decoder as some workaround suggests but still getting that reference error. What am I missing?
In our other projects, we use a library that polyfills all these gaps between JSDom and the actual browser environment. It shouldn't be different to manually specifying the TextEncoder and TextDecoder as described in this issue though. Are you using the latest version of the library?
Activity
angelo-v commentedon Sep 10, 2021
A workarround (or solution?) is to polyfill TextEncoder and TextDecoder in a setupFilesAfterEnv script:
NSeydoux commentedon Sep 10, 2021
Hi @angelo-v ! I think this issue unfortunately goes beyond the scope of
@inrupt/solid-client-authn-browser, and we have to work around it ourselves:joseexpectsTextEncoderto be present in the environment, butjsdomdoes not put it there (see jsdom/jsdom#2524). Our solution (suggested somewhere in the linked issue) is to use a custom Jest environment: https://github.com/inrupt/solid-client-authn-js/blob/main/tests/environment/customEnvironment.js, referenced e.g. in https://github.com/inrupt/solid-client-authn-js/blob/main/packages/browser/jest.config.js.angelo-v commentedon Sep 10, 2021
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
Update README.md (#1677)
CalebJamesStevens commentedon Sep 21, 2022
Still an issue today. Jest 29, added the util script
JaldinDeveloper commentedon Oct 12, 2022
just import it with require
const { JSDOM } = require("jsdom");I got the same issue and this solution worked for me
hjohnsick commentedon Oct 12, 2022
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,
ThisIsMissEm commentedon Oct 14, 2022
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
ThisIsMissEm commentedon Oct 14, 2022
@hjohnsick you can see an example here: https://github.com/inrupt/solid-client-access-grants-js/pull/366/files
You should likely have a
jest.config.tsfile in your repo, unless you're somehow using jest without any configuration at all (which I've only rarely seen)ThisIsMissEm commentedon Oct 14, 2022
@angelo-v Yup, we agree, we've since moved from custom environments to setupFilesAfterEnv
allestaire commentedon Jan 20, 2023
Seems like the answers didnt state where to put the said code.
NSeydoux commentedon Jan 20, 2023
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
clarkeustaquio commentedon Feb 2, 2023
Only solution worked to me is to add this line of code to
src/setupTests.tsThisIsMissEm commentedon Feb 2, 2023
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
danielbakas commentedon Jul 3, 2023
Problem still present. Adding this to setupTests.js still works:
jeswr commentedon Jul 3, 2023
Closing this issue as it is an error in Jest with a workaround (i.e. #1676 (comment))
vojdan commentedon Jan 4, 2024
This worked for me when using CRA.
kokombo commentedon Jan 9, 2024
This did not work for me. Still getting '
ReferenceError: TextEncoder is not defined'
Alfredoeb9 commentedon Jan 25, 2024
Solution above did not work for me either, after some trail and error found this to work for me:
NSeydoux commentedon Jan 25, 2024
@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?
kokombo commentedon Feb 28, 2024
I've been able to resolve the issue.
christopher-theagen commentedon Mar 19, 2024
.jest/setup.jsMilan-960 commentedon Oct 19, 2024
I am also having the same issue and it does not help me to resolve that. :(
ssp7 commentedon Jun 25, 2025
I just updated my react app to node 20 from 16. I am still having this issue. Also, my react builds using cra through craco. Any suggestions? I have tried assigning TextEncoder/Decoder as some workaround suggests but still getting that reference error. What am I missing?
NSeydoux commentedon Jun 26, 2025
In our other projects, we use a library that polyfills all these gaps between JSDom and the actual browser environment. It shouldn't be different to manually specifying the
TextEncoderandTextDecoderas described in this issue though. Are you using the latest version of the library?Here is an example of our polyfill setup: https://github.com/inrupt/solid-client-js/blob/main/jest.setup.ts, then consumed in https://github.com/inrupt/solid-client-js/blob/daa08001fd71c0fdf9ef482dc0e345aab3734d7d/jest.config.ts#L27.