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

append method in FormData won't accept File #2276

Closed
6 tasks done
mumumilk opened this issue Nov 4, 2022 · 0 comments · Fixed by #2290
Closed
6 tasks done

append method in FormData won't accept File #2276

mumumilk opened this issue Nov 4, 2022 · 0 comments · Fixed by #2290

Comments

@mumumilk
Copy link

mumumilk commented Nov 4, 2022

Describe the bug

I'm trying to test a piece of code that includes appending a file to a FormData instance, similar to this:

import { assert, expect, test } from "vitest";

export const uploadFile = (data = { files: [] as File[] }): FormData => {
  const formData = new FormData();

  for (const file of data.files) {
    formData.append("file", file, file.name);
  }

  return formData;
};

test("should create a correct formdata", () => {
  const files = [new File([""], "filename", { type: "text/csv" })];
  const formdata = uploadFile({ files });

  expect(formdata.get("file")).toMatchObject(files[0]);
});

Although it works just fine running in Stackblitz, it fails to run locally using node and outputs the following error:

TypeError: Failed to execute 'append' on 'FormData': parameter 2 is not of type 'Blob'

I also tried to use String/Blob types since they are supported in the original FormData spec (according to MDN at least), but without success.

As per discord thread, it seems that node 18 has its own Blob/FormData and so Vitest wont override to use JSDOM polyfills for that.

vite.config.ts
export default defineConfig({
   test: {
     globals: true,
     environment: "jsdom",
   },
 });

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-pv9eck?file=test/basic.test.ts

System Info

System:
    OS: macOS 11.6
    CPU: (8) arm64 Apple M1
    Memory: 122.02 MB / 8.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 18.8.0 - ~/.nvm/versions/node/v18.8.0/bin/node
    Yarn: 1.22.15 - /usr/local/bin/yarn
    npm: 8.18.0 - ~/.nvm/versions/node/v18.8.0/bin/npm
  Browsers:
    Chrome: 107.0.5304.87
    Firefox: 106.0.2
    Safari: 15.2
  npmPackages:
    vitest: ^0.24.5 => 0.24.5

Used Package Manager

npm

Validations

@sheremet-va sheremet-va added the bug label Nov 7, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jun 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants