Skip to content

Commit

Permalink
test(middleware): add standalone middleware test
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Aug 12, 2024
1 parent 1764d8d commit 70612d5
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/next-safe-action/src/__tests__/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { test } from "node:test";
import { z } from "zod";
import {
createSafeActionClient,
experimental_createMiddleware,
formatBindArgsValidationErrors,
formatValidationErrors,
returnValidationErrors,
Expand Down Expand Up @@ -394,3 +395,24 @@ test("overridden formatted validation errors in execution result from middleware

assert.deepStrictEqual(middlewareResult, expectedResult);
});

test("standalone middleware extends context", async () => {
const myMiddleware = experimental_createMiddleware<{ ctx: { foo: string } }>().define(async ({ next }) => {
return next({ ctx: { baz: "qux" } });
});

const action = ac.use(myMiddleware).action(async ({ ctx }) => {
return {
ctx,
};
});

const actualResult = await action();
const expectedResult = {
data: {
ctx: { foo: "bar", baz: "qux" },
},
};

assert.deepStrictEqual(actualResult, expectedResult);
});

0 comments on commit 70612d5

Please sign in to comment.