Code Monkey home page Code Monkey logo

Comments (5)

AhmedBaset avatar AhmedBaset commented on June 23, 2024 1

useFormStatus is not available in [email protected] (the version in your package.json). It's available in React Canary/Beta/Experimental chanels. To solve it you can:

  • npm i react@canary react-dom@canary
  • wait until react@19 is released as stable
  • mock useFormStatus until the release.

Note

Next.js doesn't use the react version in your package.json, it uses a pre-compiled canary version. Updating react (or even removing it) will only affect tests.

from next.js.

jthrilly avatar jthrilly commented on June 23, 2024

I get these same types of errors when my tests touch code that contains React.cache. This is included in the internal React version Next is using, but not in the 18.2 release of React itself. It seems like the module resolution that is used by vitest (and presumably jest) isn't the same as whatever Next is doing.

Edit: created an issue for my case of this: #64783

from next.js.

rylew1 avatar rylew1 commented on June 23, 2024

Was this ever resolved ? Got a similar error Error: Uncaught [TypeError: (0 , _reactdom.useFormStatus) is not a function] in a simple jest test.

When I remove uses of useFormStatus in source files, the tests pass.

from next.js.

SunDevils0 avatar SunDevils0 commented on June 23, 2024

Thanks. Successfully implemented the first and third option. For the third, which is what I will be using for projects I am currently working:

import { render, screen } from '@testing-library/react';
import Button from './button';
import { useFormStatus } from 'react-dom';

jest.mock('react-dom', () => ({
    useFormStatus: jest.fn(),
}));

describe('Button', () => {
    it('should render label', () => {
        // Arrange
        const label = 'Button';
        (useFormStatus as jest.Mock).mockResolvedValue(true);
        render(<Button type="button" label={label} />);

        // Act and Assertions ...
    });
});

from next.js.

SunDevils0 avatar SunDevils0 commented on June 23, 2024

Just to set expectations, the code I posted last night mocking useFormStatus just prevents the error. The test I was originally working on when posting this issue doesn't care about useFormStatus or its parameter values. Later I did try a test where I destructured the mock to set the pending parameter to be true. I could not get this to work. Stepping through the code pending always shows undefined.

// Arrange
const label = 'Button';
(useFormStatus as jest.Mock).mockResolvedValue({ pending: true });
render(
    <form action=""> // 
        <Button
            type="button"
            label={label}
        />
    </form>
);

Have to move along to other priorities, but if I do find a way to make this usable in a test will post here.

from next.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.