vercel / storage

Vercel Postgres, KV, Blob, and Edge Config
https://vercel.com/storage
Apache License 2.0
516 stars 57 forks source link

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

Closed sarabs3 closed 8 months ago

sarabs3 commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

If you're using Jest check out this solution to define TextDecoder and TextEncoder https://github.com/mswjs/msw/issues/1796#issuecomment-1782351156

vvo commented 8 months ago

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!