Over Martinez
Over Martinez

Reputation: 113

ReferenceError: TextEncoder is not defined after update [email protected]

I had been using Axios v1.2.2 in my project without issues and I decided to update Axios to v1.3.1, after that my app continues running as before, however, all my tests started to fails... ALL OF THEM!! 😑

Some package versions that I'm using:

"axios": "^1.3.1",
"@jest/globals": "^29.4.1",
"@types/jest": "^29.4.0",
"@testing-library/dom": "^8.20.0",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^14.4.3",

This is the error that I'm getting in all my tests:

 Test suite failed to run

    ReferenceError: TextEncoder is not defined

    > 1 | import { AxiosError } from "axios";
        | ^
      2 |
      3 | class UnknownResponseFormat<T = unknown, D = unknown> extends AxiosError<T, D> {
      4 |   static readonly ERR_UNKNOWN_RESPONSE_FORMAT = "ERR_UNKNOWN_RESPONSE_FORMAT";

I also found some workarounds for this on the internet, some things like this...

if (typeof global.TextEncoder === "undefined") {
  const { TextEncoder, TextDecoder } = require("util");
  global.TextEncoder = TextEncoder;
  global.TextDecoder = TextDecoder;
}

...but it doesn't work either.

PLEASE! SOMEBODY HELP ME! 🙏

Upvotes: 3

Views: 3246

Answers (2)

JoRo
JoRo

Reputation: 46

For me I had to add the following lines before running axios to resolve a similar issue (have never used axios before):

import { TextEncoder, TextDecoder } from 'text-encoding';

global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;

Upvotes: 0

Filip Baszak
Filip Baszak

Reputation: 55

I've encountered same problem. I think problem appered from version 1.3.0, I've checked version 1.2.6 and it's ok.

I didn't see any breaking changes in release notes, so I created support/question issue on github project.

I hope I will find solution or they will answer me.

If you need quick workaround, just change axios version to 1.2.6 in your package.json file. If any your dependencies are fetching axios in >= 1.3.0 use override option, ex.:

...
"overrides": {
    "your-dependecy-package": {
        "axios": "1.2.6"
    }
},
...

Upvotes: 4

Related Questions