Skip to content

[BUG] Package is incompatible with Vitest #54

@ludwigbacklund

Description

@ludwigbacklund

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions