-
-
Notifications
You must be signed in to change notification settings - Fork 533
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
ReferenceError: TextEncoder is not defined (updating to v2) #1796
Comments
In the migration guide there is a section about node globals not being available in jsdom environments. You can try using the suggested solution there to also make TextEncoder and TextDecoder available. https://mswjs.io/docs/migrations/1.x-to-2.x#remap-fetch-api-globals |
I just was trying this as was suggested, but it doesn't really help: In my jest.config.js: still produce |
I have the same issue with a Create React App: Reproduction Repository: https://github.com/joel-daros/msw2-text-encoder-issue FAIL src/App.test.tsx
● Test suite failed to run
ReferenceError: TextEncoder is not defined
> 1 | import { setupServer } from "msw/node";
| ^ |
Thank you for your reply. I have indeed seen this, but unfortunately this does not solve the issue. I have updated the repository to include the globals to showcase this. From my experimentation I'm noticing that when I explicitly remove the Perhaps there's something happening within the jest-environment-jsdom package that is overriding something within |
As a workaround, I managed to make it work by adding this to my jest setupTest: import { TextEncoder } from 'node:util'
global.TextEncoder = TextEncoder |
SolutionI've updated the docs to include the fix: Please follow these instructions to have Jest set up correctly. |
@kettanaito , thanks, it works. Side note, there is a mistype in doc
should be
|
Maybe you are using the The modules in the polyfills file must be loaded in the correct order, so |
This solution doesn't work with create-react-app applications, where you can't specify
|
@sernaferna it appears that using craco is the sole way to avoid ejecting in your case |
You can always avoid
Migrating off Create React App often recommended forking to customize in the past, and that's also viable |
Unfortunately craco doesn’t work either. I’ve updated my reproduction repo with Craco, and even after adding a custom craco config for Jest, the test still get stuck when https://github.com/joel-daros/msw2-text-encoder-issue // craco.config.ts import { CracoConfig } from "@craco/types";
const cracoConfig: CracoConfig = {
jest: {
configure: (jestConfig, { env, paths, resolve, rootDir }) => {
return {
...jestConfig,
setupFiles: [`${rootDir}/src/jest.polyfills`],
};
},
},
};
export { cracoConfig as default }; I know that CRA is dead and outdated package, but it's still widely used in many projects. I don’t have the option to eject our apps, because there are some many other internal factors involved. We might think in a different solution, otherwise, I think this can be a big barrier to migrate to MSW 2 or even the main reason to move to other alternatives in the long run. |
Well it was a journey, but I got there in the end.
|
You can solve it by putting the polyfills in different files.
and so on ...
|
I didn't need to create multiple polyfill files, it worked with just one for me, but the other thing I'd mention is that there have been a few releases lately that broke my tests, causing me to spend a few hours troubleshooting, only to get the next build and it would start working again. So if you've followed all of the advice in the comments above and it's still not working, try
|
For create-react-app,
|
I don't think this works anymore in Undici 6.x All my test failing with the error:
|
I found this, it might help for you as well: langchain-ai/langchainjs#2815 So basically I added this to the jest.polyfills.js file:
|
@marliesparzinski I added your code to my jest.polyfills.js file. It was important to add it to the top of the file. However another error shows
|
Did you figure this out? Also running into this issue. |
Yes, check the solution here: #1916 (comment) |
This worked for me! Project with create-react-app |
This did not work for me. Still gives the same error. I am using react-scripts: 5.0.1 |
why is this closed ? 🤔 i am facing an error.. still facing this issue afte the official docs mentioned too. |
We as a company/team decided to just stay on v1.0. It works out of the box without having to jump through any hoops. Since we just use it for testing we won't have any severe vulnerability issues anyway. Probably we'll try to update at a later point. And that's honestly my advice for anyone else trying to upgrade. |
Another one here... |
import { TextEncoder, TextDecoder } from 'util';
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder; |
please just downgrade your msw to 1.3.3. i had similliar issues i just downgraded it to 1.3.3 and it worked as expected |
I have followed this guide and additionally added this to get rid of the readablestream error
Now I get this error: I cannot find a remedy. Will migrate back to 1.3.2 for now |
I was able to fix it with these steps:
The JS ecosystem is such a joke. Dependencies are holding each other up with scotch tape. Break one and the entire tower falls. It's so bad that frameworks are recommending a new test framework to fix all issues. But when that test framework goes unsupported and meanwhile node v30.x comes out, deprecating many dependencies used in that framework. |
The API for adding mocks was changed completely. Getting MSW 2 to work with Jest and create-react-app needed some workarounds. These were the primary sources for these work-arounds: - https://mswjs.io/docs/migrations/1.x-to-2.x/ - mswjs/msw#1796
The API for adding mocks was changed completely. Getting MSW 2 to work with Jest and create-react-app needed some workarounds. These were the primary sources for these work-arounds: - https://mswjs.io/docs/migrations/1.x-to-2.x/ - mswjs/msw#1796
The API for adding mocks was changed completely. Rewrite all mocks. Getting MSW 2 to work with Jest and create-react-app needed some workarounds. These were the primary sources for these work-arounds: - https://mswjs.io/docs/migrations/1.x-to-2.x/ - mswjs/msw#1796 Remove unused handlers and branches from handlers.ts. * Bump msw from 1.3.3 to 2.4.2 Bumps [msw](https://github.com/mswjs/msw) from 1.3.3 to 2.4.2. - [Release notes](https://github.com/mswjs/msw/releases) - [Changelog](https://github.com/mswjs/msw/blob/main/CHANGELOG.md) - [Commits](mswjs/msw@v1.3.3...v2.4.2) --- updated-dependencies: - dependency-name: msw dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <[email protected]> --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Topias Heinonen <[email protected]>
This works for me, thank you! Just downgraded it to 1.3.3. |
Prerequisites
Environment check
msw
versionNode.js version
18.16.0
Reproduction repository
https://github.com/Danielvandervelden/msw-issue
Reproduction steps
npm i
&&npm run test
Current behavior
Expected behavior
No error, the tests should pass, we are only registering a route to the server in an individual
it
function.The text was updated successfully, but these errors were encountered: