Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

next-test-api-routes

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

next-test-api-routes

A Lightweight library for testing Next.js API routes

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

⚡️ Next Test API Routes

Tests npm version npm

Installation

npm install --save-dev next-test-api-routes 

Usage

import {createMocks, parseResponse} from 'next-test-api-routes'

const route = require('./api/hello')

describe('api/hello', () => {
   it('should return a 200 status code', async () => {
      const {req, res} = createMocks({
         method: 'GET',
      })

      const resp = parseResponse(await route(req, res))

      expect(res.statusCode).toBe(200)
   })

   it('should return a JSON object', async () => {
      const {req, res} = createMocks({
         method: 'POST',
         body: {
            name: 'John Doe',
         },
      })

      const resp = parseResponse(await route(req, res))

      expect(resp.json().message).toBe('Hello John Doe')
   })
})

Methods

createMocks

Creates a mock request and response object.

Parameters
  • options Object
    • options.method string (optional, default 'GET')
    • options.body Object (optional, default undefined)
    • options.query Object (optional, default {})
    • options.headers Object (optional, default {})
    • options.cookies Object (optional, default {})
    • options.url string (optional, default /)
    • options.params Object (optional, default {})
Examples
const {req, res} = createMocks({
   method: 'GET',
    body: {
        name: 'John Doe',
    },
    query: {
        page: 1,
    }
})

parseResponse

Parses the response object from the route handler.

Parameters
Returns
  • response Object
  • response.json Function Returns the parsed JSON object.
  • response.body string The response body as a string.
  • response.headers Object The response headers.
  • response.statusCode number The response status code.
Examples
const {req, res} = createMocks({
   method: 'GET',
})

const resp = parseResponse(await route(req, res))

expect(resp.json().message).toBe('Hello World')

License

This project is licensed under the MIT License - see the LICENSE file for details

Keywords

FAQs

Package last updated on 28 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc