Skip to content

Jest/Vitest unit testing: TypeError: (0 , _reactdom.useFormStatus) is not a function #63868

@SunDevils0

Description

@SunDevils0

Link to the code that reproduces this issue

https://github.com/SunDevils0/with-jest-app

To Reproduce

  1. Followed Next js instructions on creating project with jest: https://nextjs.org/docs/app/building-your-application/testing/jest#optional-extend-jest-with-custom-matchers
  2. In my app folder, created a very simple button component. Copied/pasted the same code from the Next JS documentation on useFormStatus.
  3. Within the same components folder, added a test file and wrote my unit test: button.test.tsx. The only thing the test does is render the button (the only thing needed to recreate this issue).
  4. Run tests; error received.

Current vs. Expected behavior

Current
Receive error: TypeError: useFormStatus is not a function

Expected
No error received and rest of test able to be evaluated.

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Enterprise
Binaries:
  Node: 20.11.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.1.1
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: N/A
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Jest (next/jest), TypeScript (plugin, built-in types)

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

  • If I modify the button component and remove the useFormStatus hook, the test will pass.
  • When creating the next with jest project, many of the packages were not at the latest version. Error occurs in this "initial" state, with all packages updated (environment information was taken at this point), and when upgrading next to latest canary.
  • To be clear, I have no problems with these hooks when I run my application. Such issues have been previously reported and, from what I found in my Internet searches, resolved. But none of those issues I found were specific to testing.
  • I am new to NextJs/React/TypeScript, and my first project is using Vitest as the test framework. To eliminate user error - my initial thought - this morning I created as bare-bones projects as possible, one using jest (the repo linked in this issue) and the other Vitest: https://github.com/SunDevils0/next-poc-for-unit-testing-error. The Vitest had a few more manual steps in setting up. I get this issue in both. These are the only testing frameworks I have tried in recreating the issue.
  • I also get the same error in my work Vitest project with useFormState. (I know this is being replaced with useActionState.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    TestingRelated to testing with Next.js.TypeScriptRelated to types with Next.js.bugIssue was opened via the bug report template.staleThe issue has not seen recent activity.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions