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

File upload fails in happy-dom #940

Closed
DGollings opened this issue Apr 25, 2022 · 6 comments · Fixed by #1007
Closed

File upload fails in happy-dom #940

DGollings opened this issue Apr 25, 2022 · 6 comments · Fixed by #1007
Labels
environment Issue with the environment released

Comments

@DGollings
Copy link

Reproduction example

https://codesandbox.io/s/wonderful-shadow-zqehu0

Prerequisites

Open a new terminal npx vitest

Expected behavior

Test passes

Actual behavior

Test fails - but edit vite.config.ts to jsdom and it'll pass

User-event version

14.1.1

Environment

Testing Library framework:

JS framework:

Test environment:

DOM implementation:

Additional context

Not sure if this has anything to do with user-event, but it might be related to testing-library/vue-testing-library#267 ? I seem to run into all kinds of minor/weird issues using this 'stack'

@DGollings DGollings added bug Something isn't working needs assessment This needs to be looked at by a team member labels Apr 25, 2022
@ph-fritsche
Copy link
Member

The environment doesn't implement FileList. Workaround:

class FileList {
  readonly length: 0;
  readonly item: (index: number) => undefined;
  readonly [Symbol.iterator] = function* next() {};
}
Object.defineProperty(globalThis, "FileList", {
  configurable: true,
  enumerable: false,
  get: () => FileList
});

The environment also incorrectly implements HTMLInputElement.files as writable property { value: [], writable: true, enumerable: true, configurable: true } on the element. Workaround (after the element is created):

  Object.defineProperty(input, "files", {
    configurable: true,
    enumerable: false,
    get: () => new FileList()
  });

This is not a bug with user-event, but we might make our implementation more robust for such environments. Let's keep the issue open.

@ph-fritsche ph-fritsche removed bug Something isn't working needs assessment This needs to be looked at by a team member labels Apr 25, 2022
@DGollings
Copy link
Author

DGollings commented Apr 25, 2022

Hi Philipp,

Thanks, can confirm the workarounds work.

For my education/amusement, what does 'this environment' mean? All of it? (Vue 3+Vite+Vitest+happy-dom)
Or specifically happy-dom? Because I'd be quite surprised to hear I'm the only person 'uploading' 'files' with happy-dom :)

Either way, thanks

@ph-fritsche
Copy link
Member

From the user-event perspective "the environment" is everything that is neither your component, your test code, nor brought by user-event. In your stack it's specifically happy-dom that is responsible for implementing the DOM and it doesn't implement FileList and HTMLInputElement.files correctly.

@github-actions
Copy link

github-actions bot commented Aug 2, 2022

🎉 This issue has been resolved in version 14.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@ph-fritsche
Copy link
Member

@all-contributors add @DGollings bug

@allcontributors
Copy link
Contributor

@ph-fritsche

I've put up a pull request to add @DGollings! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
environment Issue with the environment released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants