From 5fe919452bc9371a5deae36ff3e9bb1decd63a73 Mon Sep 17 00:00:00 2001 From: Guilherme Santos Date: Fri, 8 Dec 2023 10:26:49 -0300 Subject: [PATCH] test: Make sure response is still writable before setting headers (#12478) --- packages/lib/server/defaultResponder.test.ts | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 packages/lib/server/defaultResponder.test.ts diff --git a/packages/lib/server/defaultResponder.test.ts b/packages/lib/server/defaultResponder.test.ts new file mode 100644 index 0000000000..25276230be --- /dev/null +++ b/packages/lib/server/defaultResponder.test.ts @@ -0,0 +1,22 @@ +import type { NextApiRequest, NextApiResponse } from "next"; +import { describe, it, expect, vi } from "vitest"; + +import { defaultResponder } from "./defaultResponder"; + +describe("defaultResponder", () => { + it("should call res.json when response is still writable and result is not null", async () => { + const f = vi.fn().mockResolvedValue({}); + const req = {} as NextApiRequest; + const res = { json: vi.fn(), writableEnded: false } as unknown as NextApiResponse; + await defaultResponder(f)(req, res); + expect(res.json).toHaveBeenCalled(); + }); + + it("should not call res.json when response is not writable", async () => { + const f = vi.fn().mockResolvedValue({}); + const req = {} as NextApiRequest; + const res = { json: vi.fn(), writableEnded: true } as unknown as NextApiResponse; + await defaultResponder(f)(req, res); + expect(res.json).not.toHaveBeenCalled(); + }); +});