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

Jest cannot test form actions #54757

Open
1 task done
Dinika opened this issue Aug 30, 2023 · 9 comments
Open
1 task done

Jest cannot test form actions #54757

Dinika opened this issue Aug 30, 2023 · 9 comments
Labels
bug Issue was opened via the bug report template. Testing Related to testing with Next.js.

Comments

@Dinika
Copy link

Dinika commented Aug 30, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: linux
      Arch: x64
      Version: #1 SMP PREEMPT_DYNAMIC Thu Jul 27 20:01:18 UTC 2023
    Binaries:
      Node: 18.16.0
      npm: 9.5.1
      Yarn: 1.22.19
      pnpm: N/A
    Relevant Packages:
      next: 13.4.10
      eslint-config-next: 13.4.10
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.6
    Next.js Config:
      output: standalone

Which area(s) of Next.js are affected? (leave empty if unsure)

Jest (next/jest)

Link to the code that reproduces this issue or a replay of the bug

https://github.com/Dinika/nextjs-serveractions-jest

To Reproduce

git clone https://github.com/Dinika/nextjs-serveractions-jest.git
cd nextjs-serveractions-jest
yarn
yarn test

Describe the Bug

While I am able to test component that call server actions (by using the action prop on form element), I am not able to test that a given server action was called or that the form was submitted. I get the following error:

  console.error
    Warning: Invalid value for prop `action` on <form> tag. Either remove it from the element, or pass a string or number value to keep it in the DOM. For details, see https://reactjs.org/link/attribute-behavior 
        at form
        at Home

 FAIL  app/Home.test.tsx
  Home
    ✓ WORKS - Tests that do not submit form work (62 ms)
    ✕ DOES NOT WORK - Tests that check server action is called dont work (15 ms)

  ● Home › DOES NOT WORK - Tests that check server action is called dont work

    expect(jest.fn()).toHaveBeenCalled()

    Expected number of calls: >= 1
    Received number of calls:    0

      26 |         const formEl = container.querySelector('form');
      27 |         formEl.dispatchEvent(new Event('submit'));
    > 28 |         expect(spy).toHaveBeenCalled();
         |                     ^
      29 |     });
      30 |
      31 | });

      at Object.toHaveBeenCalled (app/Home.test.tsx:28:21)

Expected Behavior

It should be possible to test that the form was submitted.

Which browser are you using? (if relevant)

firefox

How are you deploying your application? (if relevant)

No response

@Dinika Dinika added the bug Issue was opened via the bug report template. label Aug 30, 2023
@github-actions github-actions bot added the Testing Related to testing with Next.js. label Aug 30, 2023
@Fredkiss3
Copy link
Contributor

I think you may need to install experimental version of react for it to work.
Then again, the story around testing with app router is not yet complete so it may also not work.

@mongolyy
Copy link
Contributor

I think you may need to install experimental version of react for it to work.

Thanks for the comment. Thanks to you I was able to make the test work as intended.

It worked with experimental react and react-dom, but even with canary, the test worked as intended in my environment.

Since canary uses code closer to the next version, it may be better to use canary if it works here.
https://react.dev/community/versioning-policy#canary-channel

@BryanAM
Copy link

BryanAM commented Jan 27, 2024

I have the same error for a form action on a component when testing in JEST.

console.error
Warning: Invalid value for prop `action` on <form> tag. Either remove it from the element, or pass a string or number value to keep it in the DOM. For details, see https://reactjs.org/link/attribute-behavior 

Operating System:

      Platform: MacOS
      Sonoma 14.1.2
    Binaries:
      Node: 18.16.0
      npm: 9.5.1
    Relevant Packages:
      next: 14.0.1
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.6

@iugmali
Copy link

iugmali commented Jan 27, 2024

Same isse here, running from Ubuntu 22.04.

console.error
    Warning: Invalid value for prop `action` on <form> tag. Either remove it from the element, or pass a string or number value to keep it in the DOM. For details, see https://reactjs.org/link/attribute-behavior 

Node and npm versions:

Node: 20.9.0
npm: 10.2.5

Relevant packages:

next: 14.0.1
react: 18.2.0
react-dom: 18.2.0

@testing-library/jest-dom: 6.1.5
@testing-library/react: 14.1.2
jest: 29.7.0
typescript: 5.2.2

@Ruffeng
Copy link

Ruffeng commented Feb 15, 2024

I am facing the same issues.

   Platform: MacOS
      Sonoma 14.2.1
    Binaries:
      Node: 21.4.0
      npm: 10.2.4
    Relevant Packages:
      next: 14.1.0
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.3.3

@Kikketer
Copy link

Kikketer commented Mar 7, 2024

Similar here as well, but I had initially tried with Vitest which didn't throw the error seen in the comment: #54757 (comment)
but the action is never called (per the original bug ticket).

Moving over to Jest it does throw the "action is an invalid prop" error.

So sight differences depending on the test framework you wish to use, but neither actually work.

@chancegraff
Copy link

chancegraff commented Mar 17, 2024

I'm encountering this issue using Next.js out of the box

@Ruffeng
Copy link

Ruffeng commented Mar 22, 2024

I had some discussions regarding other problems with the Testing library and NextJS some weeks ago in their repo. They suggested that there's a mismatched version between NextJS envs and test envs. Meanwhile Next is using React Canary, probably the test environment is still in React 18.2. That could explain as well this possible issue. React 19 should solve this issue (if I am not wrong).
testing-library/react-testing-library#1209 (comment)

@jthrilly
Copy link

I experienced a variation of this issue here: #64783

The fundamental problem is that next is using an internal version of react that testing libraries (and any other normal node process) won't have access to. It isn't clear at all when the package version of react/react-dom will be resolved, and when the internal one will. You can configure a resolve alias to fix some of your testing scenarios, but it is just a bad design IMO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Testing Related to testing with Next.js.
Projects
None yet
Development

No branches or pull requests

9 participants