test: Make sure response is still writable before setting headers (#12478)

This commit is contained in:
Guilherme Santos 2023-12-08 10:26:49 -03:00 committed by GitHub
parent 83c8f97afd
commit 5fe919452b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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();
});
});