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

ReferenceError: TextDecoder is not defined While using blob storage in test cases #628

Closed
sarabs3 opened this issue Mar 7, 2024 · 3 comments

Comments

@sarabs3
Copy link

sarabs3 commented Mar 7, 2024

I am using blob storage for the first time and after implementing it in the code, while running the tests I am getting this error.
ReferenceError: TextDecoder is not defined

This is my code

import { upload } from '@vercel/blob/client';

export const uploadMediaFile = async (file: File, userName: string) => {
  try {
    const newBlob = await upload(`${userName}/${file.name}`, file, {
      access: 'public',
      handleUploadUrl: upload,
    });
    return newBlob.url;
  } catch (error) {
    console.error(error);
    throw new Error('error uploading media files');
  }
};

Error in jest

Screenshot 2024-03-07 at 9 45 25 PM
@dferber90
Copy link
Collaborator

dferber90 commented Mar 8, 2024

Which node version are you using? Older node versions don't expose TextDecoder as a global. Try upgrading node if it's below version 12. Otherwise it might be related to how your testing environment is configured.

@dferber90
Copy link
Collaborator

If you're using Jest check out this solution to define TextDecoder and TextEncoder mswjs/msw#1796 (comment)

@vvo
Copy link
Member

vvo commented Mar 11, 2024

Hey there, yes this is a Jest issue. We actually fixed it in our code too: https://github.com/vercel/storage/blob/main/packages/blob/jest/setup.js

Good luck!

@vvo vvo closed this as completed Mar 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants