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] Package is incompatible with Vitest #54

Closed
2 tasks done
ludwigbacklund opened this issue Jan 19, 2024 · 2 comments · Fixed by #55
Closed
2 tasks done

[BUG] Package is incompatible with Vitest #54

ludwigbacklund opened this issue Jan 19, 2024 · 2 comments · Fixed by #55
Labels
bug Something isn't working

Comments

@ludwigbacklund
Copy link

ludwigbacklund commented Jan 19, 2024

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

Importing anything from next-safe-action/hooks causes Vitest tests to crash due to how React is being imported in that file.
I don't fully understand how the module resolution works here so I cannot tell you exactly why this happens, but I do know that making the following change in the hooks.mjs file fixes the problem 👇

diff --git a/dist/hooks.mjs b/dist/hooks.mjs
index 636b65d50449fa41e913bb5bb632370f8f4f9df9..7f48d3c48278d76c36a73ae6bf99bed51ba5679b 100644
--- a/dist/hooks.mjs
+++ b/dist/hooks.mjs
@@ -3,7 +3,8 @@
 // src/hooks.ts
 import { isNotFoundError } from "next/dist/client/components/not-found.js";
 import { isRedirectError } from "next/dist/client/components/redirect.js";
-import { useCallback, useEffect, useOptimistic, useRef, useState, useTransition } from "react";
+import pkg from 'react';
+const { useCallback, useEffect, useOptimistic, useRef, useState, useTransition } = pkg;

That's a patch I've applied temporarily to fix the issue in my project.

Reproduction steps

  1. Open https://stackblitz.com/edit/vitest-dev-vitest-6lgc1a?file=test%2Fbasic.test.tsx
  2. See test fail
  3. Comment out the 'next-safe-action/hooks' import and the useAction invocation further down
  4. See test pass

Expected behavior

The test should pass without failing due to how React is being imported.

Reproduction example

https://stackblitz.com/edit/vitest-dev-vitest-6lgc1a?file=test%2Fbasic.test.tsx

Operating System

macOS

Library version

6.0.1

Additional context

No response

@ludwigbacklund ludwigbacklund added the bug Something isn't working label Jan 19, 2024
TheEdoRan added a commit that referenced this issue Jan 23, 2024
Vitest fails to run tests if React hook functions are imported via named imports, so switching to a
namespace import fixes the issue.

re #54
@TheEdoRan
Copy link
Owner

TheEdoRan commented Jan 23, 2024

Hi @ludwigbacklund, thanks for opening this issue. I switched from React hooks named imports to a namespace import in the new beta version, that you can install with:

npm i next-safe-action@beta

This should fix the problem with Vitest. Please let me know if everything works fine with the new release, if that's the case, I'll publish a new version in the stable channel soon. Thank you!

@ludwigbacklund
Copy link
Author

@TheEdoRan Thanks! It works 👍

@TheEdoRan TheEdoRan linked a pull request Jan 24, 2024 that will close this issue
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

Successfully merging a pull request may close this issue.

2 participants