Skip to content
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

SyntaxError: Unexpected token 'export' when using Jest #1796

Closed
3 tasks done
ClementDreptin opened this issue Mar 17, 2025 · 4 comments
Closed
3 tasks done

SyntaxError: Unexpected token 'export' when using Jest #1796

ClementDreptin opened this issue Mar 17, 2025 · 4 comments
Labels
bug Something isn't working unconfirmed Needs triage.

Comments

@ClementDreptin
Copy link

ClementDreptin commented Mar 17, 2025

Description

Since the recent move of next-intl to ESM, running tests with Jest broke.

Verifications

Mandatory reproduction URL

https://github.com/amannn/next-intl/tree/main/examples/example-app-router

Reproduction description

  1. Clone the example-app-router example app.
  2. Run the test:jest script that is in package.json.

Expected behaviour

I expect Jest to run the tests successfully.

What I tried

I feel like I tested just about every configuration combination possible in tsconfig.json and jest.config.ts and followed the Jest docs about using ES modules, to no avail.

I noticed that in #1470 the first breaking change mentioned is

The CommonJS build has been removed. While most tools support ESM out-of-the-box, some might require additional configuration (e.g. Jest might benefit from next/jest).

but I'm not sure what you mean by "Jest might benefit from next/jest", this is probably where I missed something.

@ClementDreptin ClementDreptin added bug Something isn't working unconfirmed Needs triage. labels Mar 17, 2025
@service-paradis
Copy link

service-paradis commented Mar 17, 2025

I also face similar problems using vitest since the upgrade to next-intl@v4.

Ex.

Error: Cannot find module '{project_path}/node_modules/next/server' imported from {project_path}/node_modules/next-intl/dist/esm/development/middleware/middleware.js
Did you mean to import "next/server.js"?

Not sure if this is expected, but I needed to add the following in vitest.config.mts defineConfig:

    deps: {
      inline: ['next-intl'],
    },

amannn added a commit that referenced this issue Mar 17, 2025
Somehow the Jest examples in this repo still worked, but if you try to
use `next-intl` outside of this monorepo there was an issue with Jest.
The problem is that Jest doesn't support ESM (see e.g.
vercel/next.js#40183).

Ref #1796
@amannn
Copy link
Owner

amannn commented Mar 17, 2025

@ClementDreptin Thank you for raising this issue! Somehow the Jest examples in this repo still worked, but I can confirm that usage outside of this repo needed an adjustment. I've now updated the examples in this repo.

@service-paradis Also many thanks for your comment! next-intl@4 had to add a workaround to avoid a deoptimization in Next.js, it seems like this (rightfully!) trips up Vitest.

I've now added new testing docs that address both issues raised in this thread and have also linked to this page from the release notes.

Thanks again for sharing your findings and sorry for the trouble! I hope that in a future where vercel/next.js#77200 is fixed and tools like Jest support ESM natively, we can get rid of these workarounds that are necessary for the time being!

@amannn amannn closed this as completed Mar 17, 2025
@service-paradis
Copy link

Thank you for your work on this package and for the follow-up!

@ClementDreptin
Copy link
Author

Thank you very much for the very quick fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working unconfirmed Needs triage.
Projects
None yet
Development

No branches or pull requests

3 participants