mswjs / jest-fixed-jsdom

A superset of the JSDOM environment for Jest that respects Node.js globals.
https://npm.im/jest-fixed-jsdom
51 stars 5 forks source link

FormData doesn't support Files #12

Open 21echoes opened 3 months ago

21echoes commented 3 months ago

If you add the following lines to the index.test.js for FormData, you get a failure:

const fileData = new FormData();
const file = new File(["foo"], "foo.txt", { type: "text/plain" });
fileData.set("file", file);
expect(fileData.get("file")).toBe(file);

If you console.log(typeof fileData.get("file"), fileData.get("file")), you see that it's a string: "[object File]"

As a comparision, the test can be fixed by doing the following to use JSDOM directly:

const JSDOM = require("jsdom").JSDOM;
const dom = new JSDOM();
const fileData = new dom.window.FormData();
const file = new dom.window.File(["foo"], "foo.txt", { type: "text/plain" });
fileData.set("file", file);
expect(fileData.get("file")).toBe(file);

(note that you have to use both dom.window.FormData and dom.window.File in the fix for it to pass tests)