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

request.formData() fails with vitest and file #2166

Open
4 tasks done
haakonph opened this issue May 24, 2024 · 5 comments
Open
4 tasks done

request.formData() fails with vitest and file #2166

haakonph opened this issue May 24, 2024 · 5 comments
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node

Comments

@haakonph
Copy link

haakonph commented May 24, 2024

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 18 or higher

Node.js version

v22.2.0

Reproduction repository

https://github.com/haakonph/msw-examples

Reproduction steps

alter the example to use a request with a file: (as in the example repo)

 const formData = new FormData();
  const file = new File(['some file'], 'filnavn.xml');
  formData.append('fil', file)

in the with-vitest exmple,
run npm i
and run the 'fetches the user info' test

you need to add a file to the request to get it to fail
if you run with npm v20.0.0 it works with jsdom but not without.

it works with jest-jsdom.

Current behavior

with vitest-jsdom it time out waiting to get the request
with vitest without vitestjsdom you get a 500 response.

Expected behavior

get a 200 response as you get with examples/with-jest-jsdom example

@haakonph haakonph added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node labels May 24, 2024
@haakonph haakonph changed the title request.formData() feils with vite request.formData() fails with vite May 24, 2024
@kettanaito kettanaito changed the title request.formData() fails with vite request.formData() fails with jest-jsdom May 25, 2024
@kettanaito
Copy link
Member

Hi, @haakonph. Thanks for reporting this.

I will take a look once I have a minute but generally we have a stance that Jest issues are not addressed. If I am able to reproduce this in raw Node.js or with Vitest, then I will consider this a bug. If this is only reproducible for Jest/JSDOM, I will close this as wont-fix.

@greysteil
Copy link

@haakonph I ran into this today, too, but was able to solve it with the polyfill instructions. In case it helps, my jest.config.js looks like the below and everything is working as expected.

const nextJest = require('next/jest') // eslint-disable-line @typescript-eslint/no-var-requires

const createJestConfig = nextJest({ dir: './' })

const { TextDecoder, TextEncoder } = require('node:util')
const { fetch, Headers, FormData, Request, Response } = require('undici')

const customJestConfig = {
  // Indicates which files should be considered as tests
  testRegex: "(/src/__tests__/.*|(\\.|/)(test|spec))\\.(ts|tsx)?$",
  testPathIgnorePatterns: ["/src/__tests__/.*/mocks/", "/src/__tests__/config/*"],

  testEnvironment: "jest-environment-jsdom",
  testEnvironmentOptions: {
    // Opt-out from JSDOM using browser-style resolution for dependencies as JSDOM is not a browser
    customExportConditions: [""],
  },

  setupFilesAfterEnv: ["./src/__tests__/config/setupTests.ts"],
  globals: {
    fetch,
    Request,
    Response,
    Headers,
    FormData,
    ReadableStream,
    TextEncoder,
    TextDecoder,
  },
};

module.exports = createJestConfig(customJestConfig);

@haakonph haakonph changed the title request.formData() fails with jest-jsdom request.formData() fails with vitest-jsdom May 27, 2024
@haakonph
Copy link
Author

@kettanaito Hi, sorry i was a bit to fast with my copy pasting.

The issue is with vitest and when the body contains a file. I oppdated the issue.

I am not able to reproduce it without vitest.
So feel free to close the issue.

@haakonph haakonph changed the title request.formData() fails with vitest-jsdom request.formData() fails with vitest May 27, 2024
@haakonph haakonph changed the title request.formData() fails with vitest request.formData() fails with vitest and file May 27, 2024
@topaxi
Copy link

topaxi commented Jun 4, 2024

We are also running into this issue with msw, we are also running in an environment that loads jsdom, so this might be an interplay between msw, jsdom and a newer undici version?

Any Node.js version after v20.12.2 breaks our tests that rely on request.formData().

As a workaround we are currently pinned to Node.js v20.12.2.

@jukkahyv
Copy link

jukkahyv commented Jun 6, 2024

Same problem here. Node v20.14.0, msw 2.3.1, vitest. Downgraded node to 20.12.2, and now it works again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node
Projects
None yet
Development

No branches or pull requests

5 participants