Server Actions combine server-side code with a framework-specific invocation model. The most useful tests focus on what the action promises: input validation, authorization, the state change, and the result returned to the caller.
Make the action boundary testable
Keep request parsing and permission checks explicit. Mock framework request context only where the action needs cookies, headers, or navigation behavior; avoid broad mocks that make the test pass regardless of the actual implementation. If core behavior is complex, move that behavior into a small server-side function and test it independently.
Use typed fixtures and mock the data access layer at the boundary. Assert that invalid input causes no write, unauthorized callers cannot read or mutate another user’s data, and success returns the documented result. For redirects or revalidation, verify the framework call separately from the domain behavior.
Cover failure and retry cases
Simulate database errors and ensure the action returns a safe error state instead of exposing internals. Test repeated submissions if the operation can be retried after a lost response. Do not assume a unit test proves CSRF or deployment behavior; those need integration coverage in the actual framework setup.
Vitest can run these tests quickly when module boundaries are clean. Keep mocks close to the dependency being replaced and add a smaller end-to-end test for the user journey. The test should protect behavior, not simply confirm that a function was called.
Test an actual action through the dependency boundary
The relevant test for a server-side save imports the real action or its use case. It injects a failing dependency, calls the operation, checks the returned error, and asserts that nothing reports success. If the action intentionally throws, use the corresponding rejection assertion. A test that only verifies a mock rejects does not exercise the application's behavior. Vitest mocking guide
Verify authentication and resource access from the server entry point too. Server Actions can be invoked by direct requests, so authorization belongs inside the operation. The Next.js data security guide recommends treating arguments as untrusted. Exercise malformed form values, a valid request from a different user, and a failed or conflicting write. Reserve a browser test for wiring that depends on the form and framework runtime. Next.js Server Action security guidance
Worked example
Adapt this test to the actual save use case and its injected writer. The test double supplies the failure, and the save operation must translate it into an error result and preserve the draft.
tsimport { describe, expect, it, vi } from "vitest"; import { saveDraft } from "./save-draft"; describe("saveDraft", () => { it("reports a failed write without signaling success", async () => { const write = vi.fn().mockRejectedValue(new Error("Storage unavailable")); const onSaved = vi.fn(); const result = await saveDraft("Updated text", { write, onSaved }); expect(write).toHaveBeenCalledWith("Updated text"); expect(result).toMatchObject({ success: false }); expect(onSaved).not.toHaveBeenCalled(); }); });




Community
Discussion
Loading comments…