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

[BUG] Jest error "SyntaxError: Cannot use import statement outside a module" #252

Open
2 tasks done
freirepaulo opened this issue Aug 29, 2024 · 4 comments
Open
2 tasks done
Labels
bug Something isn't working

Comments

@freirepaulo
Copy link

Are you using the latest version of this library?

  • I verified that the issue exists in the latest next-safe-action release

Is there an existing issue for this?

  • I have searched the existing issues and found nothing that matches

Describe the bug

First of all, I would like to thank you for the excellent work you have been doing with this library. However, I am facing an issue in my new project.

When I try to run my tests (just a simple test), it returns this error within the dist of safe-action. I noticed that a similar issue was opened in the past and resolved, but the problem seems to have returned:

Screenshot 2024-08-29 at 15 47 10

These are the dependencies I am using:
Screenshot 2024-08-29 at 15 49 20

Reproduction steps

  1. add the library
  2. try to run a base unit test

is also important to say that the page.tsx is a server component and the SearchArea.tsx is a client component.

this the unit test that is breaking:
Screenshot 2024-08-29 at 15 51 31

Expected behavior

the test should pass

Minimal reproduction example

private repo

Operating System

macOS

Library version

7.8.0

Next.js version

14.2.5

Node.js version

21.7.3

Additional context

No response

@freirepaulo freirepaulo added the bug Something isn't working label Aug 29, 2024
@TheEdoRan
Copy link
Owner

Hi, please set up a public repo or a CodeSandbox project with a minimal reproduction of the issue so I can investigate the problem, thanks.

@freirepaulo
Copy link
Author

freirepaulo commented Aug 29, 2024

@TheEdoRan https://github.com/freirepaulo/next-safe-action public repo that has the issue - to reproduce is just checkout on the main branch > run yarn > yarn test

Screenshot 2024-08-29 at 18 18 12

@freirepaulo
Copy link
Author

If I change all the imports and exports in the hook.mjs and index.mjs resolves the problem, but I don't know what will impact the library structure....

Screenshot 2024-08-30 at 11 46 34
Screenshot 2024-08-30 at 11 48 06

@freirepaulo freirepaulo changed the title [BUG] Nextjs 14 run test with erro "SyntaxError: Cannot use import statement outside a module" [BUG] Nextjs 14 run jest test with erro "SyntaxError: Cannot use import statement outside a module" Aug 30, 2024
@TheEdoRan TheEdoRan changed the title [BUG] Nextjs 14 run jest test with erro "SyntaxError: Cannot use import statement outside a module" [BUG] Jest error "SyntaxError: Cannot use import statement outside a module" Aug 30, 2024
@TheEdoRan
Copy link
Owner

Thank you for providing the repro. I solved the issue on my end by setting this option in next.config.mjs:

const nextConfig = {
+  transpilePackages: ["next-safe-action"],
};

image

Vitest should be a better testing framework though, if you can switch to it. Let me know if setting that option fixes the issue for you as well, thanks!

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
Development

No branches or pull requests

2 participants