TheEdoRan / next-safe-action

Type safe and validated Server Actions in your Next.js project.
https://next-safe-action.dev
MIT License
2.29k stars 35 forks source link

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

Closed freirepaulo closed 3 weeks ago

freirepaulo commented 2 months ago

Are you using the latest version of this library?

Is there an existing issue for this?

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

TheEdoRan commented 2 months ago

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 commented 2 months ago

@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 commented 2 months ago

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

TheEdoRan commented 2 months ago

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!