Skip to content

next-intl/link and useRouter is causing an error in Jest tests #488

@cjambrosi

Description

@cjambrosi

Description

Environment

  • Node: v18.16.0
  • OS: MacOS Ventura 13.4
  • Yarn: v1.22.19

Libs

  • nextjs: v13.4.6
  • next-intl: v2.20.0
  • Jest: v29.6.2

The errors occur in all component tests that use next-intl/link or useRouter (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:

$ jest --config=jest.config.js --no-cache
 FAIL  src/components/Card/index.test.tsx
  ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /Users/cjambrosi/GitHub/next-intl-jest-error-reproduce/node_modules/next-intl/dist/src/link/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { default } from './Link';
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

      10 | }
      11 |

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
      at Object.<anonymous> (src/components/I18nLink.tsx:12:54)
      at Object.<anonymous> (src/components/Card/Card.tsx:12:58)
      at Object.<anonymous> (src/components/Card/index.test.tsx:6:54)

useRouter error:

$ jest --config=jest.config.js --no-cache
 FAIL  src/components/Card/index.test.tsx
  ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /Users/cjambrosi/GitHub/next-intl-jest-error-reproduce/node_modules/next-intl/dist/src/client/index.js:4
    export { default as useRouter } from './useRouter';
    ^^^^^^

    SyntaxError: Unexpected token 'export'

      10 |   }

Mandatory reproduction URL (CodeSandbox or GitHub repository)

https://github.com/cjambrosi/next-intl-jest-error-reproduce

Reproduction description

Steps to reproduce:

  1. Open reproduction
  2. Install dependencies
  3. Run tests with yarn test command
  4. See the error SyntaxError: Unexpected token 'export'

Expected behaviour

No error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingunconfirmedNeeds triage.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions