-
-
Notifications
You must be signed in to change notification settings - Fork 247
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
next-intl/link
and useRouter
is causing an error in Jest tests
#488
Comments
Thank you for the great report! Hmm, I'm really curious why jest works internally in the monorepo of Anyway, jest doesn't support ES modules out of the box and currently in the stable release, the entry point for In the meantime, you can use this workaround: index b1df010..5145857 100644
--- a/jest.config.js
+++ b/jest.config.js
@@ -22,4 +22,21 @@ const customJestConfig = {
setupFilesAfterEnv: ['@testing-library/jest-dom', '<rootDir>/jest.setup.tsx'],
};
-module.exports = createJestConfig(customJestConfig);
+module.exports = async () => ({
+ ...(await createJestConfig(customJestConfig)()),
+
+ // https://github.com/vercel/next.js/issues/36077#issuecomment-1096635363
+ transformIgnorePatterns: [
+ 'node_modules/(?!(' +
+ [
+ '@formatjs/icu-messageformat-parser',
+ '@formatjs/icu-skeleton-parser',
+ '@formatjs/fast-memoize',
+ 'next-intl',
+ 'use-intl',
+ 'intl-messageformat',
+ 'tslib'
+ ].join('|') +
+ ')/)'
+ ]
+}); Thanks to @barbarah, this is based on https://github.com/colonial-heritage/colonial-collections/blob/main/apps/researcher/jest.config.js. Sorry for the inconvenience! |
Thanks for the sugestion. I chose to use the
console.error
The above error occurred in the <Card> component:
at useRouter (/Users/cjambrosi/GitHub/next-intl-jest-error-reproduce/src/components/Card/Card.tsx:7:18)
at children (/Users/cjambrosi/GitHub/next-intl-jest-error-reproduce/node_modules/use-intl/dist/use-intl.cjs.development.js:663:23)
at children (/Users/cjambrosi/GitHub/next-intl-jest-error-reproduce/node_modules/next-intl/dist/shared/next-intl.cjs.development.js:17:23)
Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.
19 | ) => {
20 | const locale = providerProps?.locale || defaultProviderProps.locale;
> 21 | return render(
| ^
22 | <NextIntlClientProvider
23 | locale={locale}
24 | messages={{
at logCapturedError (node_modules/react-dom/cjs/react-dom.development.js:18687:23)
at Function.render [as customRender] (jest.setup.tsx:21:10)
at Object.customRender (src/components/Card/index.test.tsx:6:10)
src/components/Card/index.test.tsx
Home
✕ Should render Card component (86 ms)
● Home › Should render Card component
invariant expected app router to be mounted
5 |
6 | export default function Card() {
> 7 | const router = useRouter()
| ^
8 |
9 | const handleClick = (e) => {
10 | e.preventDefault()
at Object.useRouter (node_modules/next/src/client/components/navigation.ts:123:11)
at useRouter (src/components/Card/Card.tsx:7:18)
at Function.render [as customRender] (jest.setup.tsx:21:10)
at Object.customRender (src/components/Card/index.test.tsx:6:10) |
You probably have to mock
|
FWIW I've also just resolved a bug with jest where I got the same error message, but I think it's more related to me using pnpm and symlinks here. This was the fix: 88df0fe |
Thank you for helping me @amannn. I managed to solve the |
→ Please refer to [the announcement](https://next-intl-docs.vercel.app/blog/next-intl-3-0) Fixes #488 Fixes #402 Fixes #578 --------- Co-authored-by: 汪心禾 <wangxinhe06@gmail.com> Co-authored-by: Tobias <tobi.l@posteo.at> Co-authored-by: Andres <CptCompiler@users.noreply.github.com> Co-authored-by: Andres <andres@21st.digital> Co-authored-by: narakhan <narakhan@users.noreply.github.com> Co-authored-by: Nicholas Mobbs <nick.mobbs@apporetum.dev> Co-authored-by: James Berry <69924001+jlalmes@users.noreply.github.com> Co-authored-by: Brendon Sled <brendon@tinyroar.com>
Description
Environment
Libs
The errors occur in all component tests that use
next-intl/link
oruseRouter
(maybe it happens to others too).This error has already been reported in another issue, but in next-intl
3.0.0-beta.9
version.next-intl/link
error:useRouter
error:Mandatory reproduction URL (CodeSandbox or GitHub repository)
https://github.com/cjambrosi/next-intl-jest-error-reproduce
Reproduction description
Steps to reproduce:
yarn test
commandSyntaxError: Unexpected token 'export'
Expected behaviour
No error.
The text was updated successfully, but these errors were encountered: