-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
[Feature] Add feature to allow mocking useRouter in Next.js for component testing #14707
Comments
Could you provide a complete example of what you'd like to achieve? Note that in most cases, putting all the mocking / init code into the |
Given component
the test
will result in:
What I would like to do, is mock what router would return in the test:
With Jest, it seems is possible to mock what the router would return on test by test basis (like in blog example I posted earlier) |
Once the patch above lands, you should be able to do the following: // index.js
import router from 'next/router';
import { beforeMount, afterMount } from '@playwright/experimental-ct-react/hooks';
beforeMount(async ({ hooksConfig }) => {
// Before mount, redefine useRouter to return mock value from test.
router.useRouter = () => hooksConfig.router;
}); // test.spec.jsx
// Pass mock value from test into `beforeMount`.
await mount(<App></App>, {
hooksConfig: {
route: {
query: {page: 1, per_page: 10},
asPath: '/posts'
}
}
}); |
I'll close it, but please feel free to reopen (file new) if there are outstanding issues. |
@pavelfeldman |
As a-vershinin I'm interested in this too, I don't really understand how hooks should be mocked. Also, is there an example to make component-testing work with the react-router? Thanks for the work you are doing, I'm approaching now to component-testing and it looks very promising |
+1 to the above, I'd also love to see how to get component testing working with Update: import { test, expect } from '@playwright/experimental-ct-react';
import { MemoryRouterProvider } from 'next-router-mock/MemoryRouterProvider/next-12';
import YourComponent from 'components/YourComponent';
// N.B. make sure to import the MemoryRouterProvider from the next-12 folder,
// the generic import didn't work for me.
test('it works', async ({ mount }) => {
const component = await mount(
<MemoryRouterProvider url="/">
<YourComponent />
</MemoryRouterProvider>
);
}); See the next-router-mock documentation for info on what features are supported. |
IMHO mocking is essential for component tests - or we are stuck with only being to test components that render purely on props passed in - hooks are a major part of react development these days and not being able to mock a hook before tests is a big issue for us. |
can you provide the same example but for the useParams() from the react-router-dom please? |
If the component uses
useRouter()
hook, tests will fail because the hook will be null/undefined.Jest has
jest.spyOn
to mock it (reference - https://jamespotz.github.io/blog/how-to-mock-userouter). It would be helpful to have something similar here.The text was updated successfully, but these errors were encountered: