Skip to content

Cannot set headers after they are sent to the client #8

@orimdominic

Description

@orimdominic

I keep getting the error

Cannot set headers after they are sent to the clientError [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

      at send (node_modules/micro/lib/index.js:76:6)
      at sendError (node_modules/micro/lib/index.js:83:2)
      at node_modules/micro/lib/index.js:110:17

I'm testing with Jest and my serverless function (which works in production) is something like this (I whittled it down to what you are seeing in the code and I still got the error)

import { VercelRequest, VercelResponse } from "@vercel/node";

module.exports = async (req: VercelRequest, res: VercelResponse) => {
  res.status(200).send("hello")
}

Here is how my test file looks

import * as request from "request";
import { createServer } from "vercel-node-server";
import listen from "test-listen";
import { promisify } from "util";
const lambdaFn = require("./path-to-lamda-fn");

const get = promisify(request.get);
let server: any;
let url: string;

describe("name of test", () => {
  // beforeAll(async () => {
  //     server = createServer(twitterWebhookLambda);
  //     url = await listen(server)
  //     console.log("url:", url);
  // });

  // afterAll(() => {
  //   server.close();
  // });

  it("should return the expected response", async () => {
    const server = createServer(lambdaFn)
    server.listen(8000)
    const res = await get({url: "http://localhost:8000"})
    expect (res.statusCode).toBe(200)
    server.close()
  });
});

OS: Ubuntu 20
Node version: 14.x
Yarn version: 1.22.x
"vercel": "^22.0.1"

I also tried it with Axios and I got the same error

Nice work here!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions