capricorn86 / happy-dom

A JavaScript implementation of a web browser without its graphical user interface
MIT License
3.09k stars 185 forks source link

File Reader #1413

Open mirinio opened 2 months ago

mirinio commented 2 months ago

Describe the bug I am running into issues with FileReader when running my jest tests. This happens when I reference happy-dom jest-environment. testEnvironment: '@happy-dom/jest-environment',

I get following error running a test for a pure function:

reader.readAsDataURL is not a function
TypeError: reader.readAsDataURL is not a function

the funcrion I am testing:

export function convertBlobToBase64(blob: Blob): Observable<string> {
    const reader = new FileReader();
>  reader.readAsDataURL(blob);
    return fromEvent(reader, 'load').pipe(map(() => (reader.result as string).split(',')[1]));
}

To Reproduce Steps to reproduce the behavior:

  1. Reference testEnvironment: '@happy-dom/jest-environment' in your test setup.
  2. Write a jest test including FileReader.
  3. Run the reader.readAsDataURL(..) function
  4. See error

Expected behavior FileReader should have a readAsDataURL function.

Screenshots If applicable, add screenshots to help explain your problem.

Device:

Additional context I am working with an Angular project writing jest tests for util functions.

earl-warren commented 2 weeks ago

Adding tests for https://codeberg.org/forgejo/forgejo/src/branch/forgejo/web_src/js/utils.js#L68 is blocked by this bug.