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
Add support for TextEncoder and TextDecoder #2524
Comments
@domenic can you advice where this can be added in jsdom ? |
There are a lot of upvotes and linked issues to this one (which is the correct etiquette), but given this has not received any explicit interaction for over a year, I hope nobody minds if I give it a jolt! ⚡ I am currently running a patched custom environment in jest to work around this not being in jsdom itself: const Environment = require('jest-environment-jsdom');
module.exports = class CustomTestEnvironment extends Environment {
async setup() {
await super.setup();
if (typeof this.global.TextEncoder === 'undefined') {
const { TextEncoder, TextDecoder } = require('util');
this.global.TextEncoder = TextEncoder;
this.global.TextDecoder = TextDecoder;
}
}
}; |
@JamesHenry looks a lot like the example on Stackoverflow which also shows the react-scripts command to use that custom env: |
I would also appreciate support for TextDecoder and TextEncoder. Didn't have much joy with @JamesHenry 's custom test environment solution but adding this to the top of my test files worked:
|
Note: A breaking change in |
Is there any plan to fix this in |
jsdom and whatwg-url recently created new major versions which require the latest Node.js releases. There is no plan to stop requiring the latest Node.js releases in the new major versions. You can use the old unsupported versions of jsdom or whatwg-url if you want to continue using non-supported Node.js releases. |
I'm not sure if this was in reply to my question above, but I am on Node 14 and am getting an error about |
You need the supported LTS version of Node 14, which at this time is Node 14.17.5. |
I am testing with Node 14.17.5 locally and am still getting that error. I have seen the workaround where one adds code to one's Jest config, but this was working fine with FWIW, I get the same error with Node 16.7.0 |
Fascinating. If you can post a small repro example in a new issue following the issue template that would be helpful. (In particular not using Jest or anything similar.) |
FWIW I cannot reproduce; jsdom 16.7.0 did not have TextEncoder/TextDecoder exposed on its windows: https://runkit.com/domenicdenicola/611d6c290366f4001b2745f2 |
I also use Node 14.7.5. Here is a minimal example: https://github.com/cpiber/jest-demo I can use jsdom just fine in normal code and in jest with test environment node, but in jest test environment jsdom I get the following error: I should mention that jest itself has no problems, test environment jsdom works normally, I just can't import/require inside a test. It seems to be a problem when the jsdom environment is active, so it's probably not a problem with jsdom directly. |
I understand why this issue is open for so long and it isn't treated as bug due it's about point of view. If we saw from Jsdom side then Problem is when Jsdom loaded under Jest then it mean then here is question.
I don't know actual answer from maintainer but if it's me, it need to run under Node.js for sure. but even answer is like that then it doesn't mean we should add those TextEncoder and TextDecoder into jest-environment-jsdom as how this package know which variable in global that Jsdom need. If in future this happen with other global variable then we will see another issue similar like this again. Right now I don't have actual answer that this should be fix in which repo. I just post this to make everyone aware situation that we faced because we might have some miss communication. then here is just maybe good news. I have other solution that you might interest. As I said above that because jest-environment-jsdom make change of
Adding code to initial Jsdom in https://jestjs.io/docs/configuration#globalsetup-string or import * as jsdom from 'jsdom'
const dom = new jsdom.JSDOM('<!DOCTYPE html><head/><body></body>', {
url: 'http://someexample.com/',
referrer: 'https://example.com/',
contentType: 'text/html',
userAgent: 'Jest',
includeNodeLocations: true,
storageQuota: 10000000,
pretendToBeVisual: true,
})
// Access value that you need with these.
// dom.window as unknown as Window & typeof globalThis
// dom.window.document
// dom.window.navigator
// dom.window.localStorage With this you can use |
@domenic Thank you for pointing this out then I think only option is adding it in https://jestjs.io/docs/configuration#globalsetup-string or I hope someone can be in middle between this lib and maintainer of I updated above comment. |
* Update dependency mapbox-gl to v3 * polyfil for TextDecoder in jsdom as according to jsdom/jsdom#2524 --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Matthew Dean <matthew.dean@digicatapult.org.uk>
- Add msw as a test dependency. Use v1 since v2 has jsdom issues (see mswjs/msw#1916 and jsdom/jsdom#2524) - Resolve fetch polyfill for the test environment
- Add msw as a test dependency. Use v1 since v2 has jsdom issues (see mswjs/msw#1916 and jsdom/jsdom#2524) - Resolve fetch polyfill for the test environment
@dmmulroy thank-you so much, this worked for me!!! (with a slight variation - I had to change the top import to just |
This solution requires a lot of code, I am in a TS environment, and if I remember correctly I just did this in the jest setup file: const { TextEncoder, TextDecoder } import 'util';
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder; |
@luchillo17 That doesn't work for me and neither does
|
@pdfabbro Where are you importing it? maybe I remember it wrong, for example, the solutions Backstage.io maintainers came up with are all over the place, I believe I fixed it globally then the maintainers were paranoid and instead decided to move the fix to each plugin that required it, in an inconsistent way might I add: https://github.com/search?q=repo%3Abackstage%2Fbackstage%20TextEncoder&type=code If you look there the |
From a user's perspective, it feels like jsdom is responsible for setting up the environment in such a way that it resembles a browser, with all of the browser globals provided for me. I've read through the comments here and in some other places, but I don't really understand the developer perspective here. Over at msw (which I'm trying to use with jest, bringing me here), it sounds like this is considered to be a jest problem, but it makes perfect sense to me that jest would completely strip the default node environment down and allow jsdom to set up the new environment. Why isn't this a jsdom issue? |
It is a JSDom issue, basically, the browser has TextEncoder and TextDecoder, I can only assume JSDom doesn't provide it by default because it is not used by most projects, thus reducing the size of the test environment in those cases, and then for us that do use them we need to add it to the environment, this whole thread is basically a bunch of different ways to do so. |
It is a jsdom issue. Jsdom doesnt provide it because nobody has implemented TextEncoder/TextDecoder in jsdom. Just like nobody has implemented fetch(), or the dialog element, or many many other features. If you want a feature added to jsdom, working on a PR that passes all the tests is the correct way of doing so. That is very hard for many web platform features, which is why browsers employ hundreds of full-time employees. |
TextEncoder and TextDecoder are added as globals in node11.
Maybe it is possible to use these for jsdom?
https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder
https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder
https://nodejs.org/api/globals.html#globals_textdecoder
https://nodejs.org/api/globals.html#globals_textencoder
I made a change in jest to support these in jest-environment-node. jestjs/jest#8022 I can make simmilar change in jsdom but I am not sure were it should be done.
The text was updated successfully, but these errors were encountered: