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

Imports from next-intl/server break Jest due to export syntax #681

Closed
sawyerh opened this issue Nov 30, 2023 · 1 comment · Fixed by #683
Closed

Imports from next-intl/server break Jest due to export syntax #681

sawyerh opened this issue Nov 30, 2023 · 1 comment · Fixed by #683
Labels
bug Something isn't working

Comments

@sawyerh
Copy link

sawyerh commented Nov 30, 2023

Description

Jest fails to run when the tested source file has an import from next-intl/server.

Similar to #452 and #488, but a different module.

Mandatory reproduction URL (CodeSandbox or GitHub repository)

https://github.com/sawyerh/next-intl-bug-examples

Reproduction description

Adding a line like the following to a source file:

import { getTranslations } from "next-intl/server";

Then running a Jest test on that source file, results in Jest reporting:

node_modules/next-intl/dist/esm/server.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export{default as getRequestConfig}from"./server/getRequestConfig.js";export{default as getFormatter}from"./server/getFormatter.js";export{default as getNow}from"./server/getNow.js";export{default as getTimeZone}from"./server/getTimeZone.js";export{default as getTranslations}from"./server/getTranslations.js";export{default as getMessages}from"./server/getMessages.js";export{default as getLocale}from"./server/getLocale.js";export{setRequestLocale as unstable_setRequestLocale}from"./server/RequestLocale.js";
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

Expected behaviour

I would like to use Jest to test React components that import from next-intl/server without additional compiler config.

@sawyerh sawyerh added bug Something isn't working unconfirmed Needs triage. labels Nov 30, 2023
@amannn
Copy link
Owner

amannn commented Dec 1, 2023

That's a fair point, thanks for the hint!

Note that next-intl/server can probably not be used in a meaningful way in unit tests with Jest currently, since it requires the RSC environment. However, in your case you're merely testing a page that happens to also have a metadata export and next-intl/server isn't used in the tested code. I think we can make this work as long as you're not invoking any functions from next-intl/server.

For other cases, next-intl/server should probably be mocked for the time being (see also testing-library/react-testing-library#1209).

@amannn amannn removed the unconfirmed Needs triage. label Dec 1, 2023
amannn added a commit that referenced this issue Dec 1, 2023
…er not call any functions). This allows for easier testing of page components with multiple exports. (#683)

Fixes #681
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants