Skip to content

Error with FormData constructor in React + Jest: TypeError: FormData constructor: Argument 1 could not be converted to: undefined. #32

@oleh-demkovych

Description

@oleh-demkovych

When using this library with React and Jest, I encountered the following error during test execution:

TypeError: FormData constructor: Argument 1 could not be converted to: undefined.

This occurs when trying to create a FormData object from a ref:

const formRef = useRef(null);

// ...

if (formRef.current) {
  const formData = new FormData(formRef.current);
}
<form ref={formRef}>
  ...
</form>

This works fine in the browser or when using testEnvironment: 'jsdom', but in Jest tests it fails.

Steps to Reproduce

  1. Use useRef(null) to create a form reference and attach that ref to the form.
  2. Attempt to create FormData from formRef.current in a Jest test.

Expected Behavior

FormData should be constructed if formRef.current is a valid form element, or at least the test environment should mimic the browser behavior more closely.

Environment

React version: 19.1.0
Jest version: 29.7.0
Node version: 20.13.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions