SUPER REQUEST
Introduction

Introduction

SuperRequest is a powerful tool for simulating HTTP requests in Node.js applications without the need to initialize a server listening on a port. This makes it ideal for unit and integration testing, especially in scenarios where endpoint creation needs to be tested quickly and in isolation.

Quick Start

In this example, we set up a simple application with a GET /hello route that responds with a message. We use superRequest to simulate the request and verify the response. To run this test, you will need to use a testing library such as Jest.

import v from "vkrun"
 
describe("Basic example with superRequest", () => {
  it("Should respond with "Hello, World!" on GET /hello route", async () => {
    const app = v.App()
 
    // Define the GET /hello route
    app.get("/hello", (req: v.Request, res: v.Response) => {
      res.status(200).send("Hello, World!")
    })
 
    // Perform a GET request using superRequest
    await v.superRequest(app).get("/hello").then((response) => {
      // Validate the response
      expect(response.statusCode).toEqual(200)
      expect(response.data).toEqual("Hello, World!")
    })
 
    app.close() // closes all app processes
  })
 
  it("Should return 404 for a non-existent route", async () => {
    const app = v.App()
 
    // Define the GET /hello route
    app.get("/hello", (req: v.Request, res: v.Response) => {
      res.status(200).send("Hello, World!")
    })
 
    // Attempt to access a non-existent route: GET /not-found
    await v.superRequest(app).get("/not-found").catch((error) => {
      // Validate the error
      expect(error.response.statusCode).toEqual(404)
      expect(error.response.statusMessage).toEqual("Not Found")
    })
 
    app.close() // closes all app processes
  })
})
Copyright © 2024 - 2024 MIT by Mario Elvio