-
Notifications
You must be signed in to change notification settings - Fork 26k
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
Link component used in Jest not working properly #43769
Comments
I confirm I am getting the same error even though I am not using the |
I get the same error for dynamic routes with version
|
Right, but this is still a bug because we should not be needing to update our current code to pass an |
It throws when this is evaluated to true: This check relies on being able to get hold of the router context, however I don't see this router context being exported anywhere for public consumption, so its not easy to even mock or wrap tests in this router context. |
any updates on this ? |
This is a pretty rough bug when adopting Next 13. In the meantime, here's a workaround for jest: // setupTests.js
import { createContext } from 'react';
jest.mock('next/dist/shared/lib/router-context.js', () => ({
RouterContext: createContext(true),
})); I'm using this in a setupFile to only define it once (and hopefully remove it soon 🤞): // jest.config.js
/** @type {import('jest').Config} */
const config = {
setupFiles: ['./setupTests.js']
};
module.exports = config; See @m-rutter's comment for more context. |
Any updates on this? |
Hello, the comment above fixed it for me (#43769 (comment)), did it not resolve it for you? |
Hello, yes. I was looking for a fix instead of a workaround. |
I don't know if this issue has already some better fix than the workaround. But in my case i had error:
Which as i found in other issue - the solution is to replace After this change i had another error:
In this case - as for Next 13.5.1 (maybe earlier also) the filename is changed, and finally my working code was:
👍 ✌️ |
Thank you for the updated path, it works! To help anyone who gets an error of:
The solution I found was to use a variable prefixed with the word "mock", so My working jest.setup.ts file looks like this
|
Because
it does not work with const mockContext = createContext(true);
vi.mock('next/dist/shared/lib/router-context.shared-runtime.js', () => ({
RouterContext: mockContext,
})); edit: const Wrapper = (props: ComponentProps<typeof MyComponent>) => {
return (
<MemoryRouterProvider>
<MyComponent {...props} />
</MemoryRouterProvider>
);
}; Not really satisfied with this solution but the cleanest without any weird workarounds. |
Using Vitest, and this also worked for me. At the time of writing, the import is
|
Verify canary release
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 21.6.0: Thu Sep 29 20:13:56 PDT 2022; root:xnu-8020.240.7~1/RELEASE_ARM64_T6000
Binaries:
Node: 16.15.1
npm: 8.11.0
Yarn: 1.22.19
pnpm: N/A
Relevant packages:
next: 13.0.7-canary.1
eslint-config-next: N/A
react: 18.2.0
react-dom: 18.2.0
Which area(s) of Next.js are affected? (leave empty if unsure)
No response
Link to the code that reproduces this issue
https://github.com/viktorrenkema/testing-link
To Reproduce
yarn test
Describe the Bug
next/link
does not format the URL string properly with URL object for a dynamic route. #42715) that we also experienced ourselves.We also went through all prior versions of Next 13 with the following results:
13.0.1
works13.0.2-5
breaks, known issue #42715 looking something like:13.0.6
breaks, with the error described in this issue. We get the error as introduced in the PR named in step 2.Expected Behavior
Jest test is supposed to know we are:
Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
The text was updated successfully, but these errors were encountered: