Skip to content
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

TextEncoder is not defined #2015

Closed
4 tasks done
dev-faisaal opened this issue Feb 6, 2024 · 2 comments
Closed
4 tasks done

TextEncoder is not defined #2015

dev-faisaal opened this issue Feb 6, 2024 · 2 comments
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node

Comments

@dev-faisaal
Copy link

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 18 or higher

Node.js version

v20.10.0

Reproduction repository

https://github.com/dev-faisaal/apitesting.git

Reproduction steps

..

Current behavior

I will encountered this issue

FAIL src/users/users.test.jsx
● Test suite failed to run

ReferenceError: TextEncoder is not defined

> 1 | import { setupServer } from 'msw/node'
    | ^
  2 | import { handlers } from './handlers'
  3 |
  4 | export const server = setupServer(...handlers)

  at Object.<anonymous> (node_modules/@mswjs/interceptors/src/utils/bufferUtils.ts:1:17)
  at Object.<anonymous> (node_modules/@mswjs/interceptors/lib/node/index.js:7:24)
  at Object.<anonymous> (node_modules/msw/src/node/SetupServerApi.ts:6:8)

Expected behavior

it shoud not give that error

@dev-faisaal dev-faisaal added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node labels Feb 6, 2024
@kettanaito
Copy link
Member

Hi, @dev-faisaal. Thanks for reporting this.

It looks like you are using the latest MSW with Jest (via Create React App). To correctly set up Jest to be able to run modern JavaScript code, you should follow these instructions in the docs.

Note that Create React App abstracts the Jest setup from you so you cannot apply those instructions in your current repository. You have a choice to either eject and follow MSW docs, or migrate away from CRA (it's not recommended for use to anyone).

@dev-faisaal
Copy link
Author

dev-faisaal commented Feb 6, 2024

I use this and I got error in this variable globalThis
the error is " 'globalThis' is not defined."
If you have any sample code for this would you please share me its repository link

I have updated the code in the repository
https://github.com/dev-faisaal/apitesting.git

const { TextDecoder, TextEncoder } = require('node:util')

Object.defineProperties(globalThis, {
TextDecoder: { value: TextDecoder },
TextEncoder: { value: TextEncoder },
})

@github-actions github-actions bot locked and limited conversation to collaborators Oct 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node
Projects
None yet
Development

No branches or pull requests

2 participants