🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

@next-testing/api

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

@next-testing/api

Next API Test Suite - Simplifying the mocking and asserting of Next API Request & Responses

2.0.0
unpublished
latest
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
logo

Simplifying the struggle of mocking and reimplementing NextAPIRequests and NextAPIResponses in order to test and validate NextJS serverless functions.

npm (tag) NPM npm

Maintainability Rating Reliability Rating Coverage

Installation

Choose your favorite package manager to install the next-testing library

NPM

npm install --save-dev @next-testing/api

Yarn

yarn add -D @next-testing/api

PNPM

pnpm add -D @next-testing/api

Usage

Mocking Simple GET Request

import ServerlessFunctionApi from './pages/api/v1/myApi'
import { NextApiRequestBuilder, ResponseMock } from '@next-testing/api'

test("Get Request and Response Mock", () => {
  const req = new NextApiRequestBuilder().setMethod('GET').build()
  const res = ResponseMock<MyResult>()

  ServerlessFunctionApi(req, res)

  expect(res.getStatusCode()).toEqual(405)
  expect(res.getBodyJson()).toStrictEqual({
      success: false,
      message: 'Method not allowed',
  })
})

Mocking Simple POST Request

import ServerlessFunctionApi from './pages/api/v1/myApi'
import { NextApiRequestBuilder, ResponseMock } from '@next-testing/api'

it('Post Request and Response Mock', async () => {
    const req = new NextApiRequestBuilder().setMethod('POST').build()
    const res = ResponseMock<MyResult>()

    ServerlessFunctionApi(req, res)

    expect(res.getStatusCode()).toEqual(401)
    expect(res.getBodyJson()).toStrictEqual({
        success: false,
        message: 'access denied',
    })
})

Mocking Request with Headers

import ServerlessFunctionApi from './pages/api/v1/myApi'
import { NextApiRequestBuilder, ResponseMock } from '@next-testing/api'

it('Mock Request and Response with headers', async () => {
  const req = new NextApiRequestBuilder()
          .setMethod('POST')
          .setHeaders({ authorization: 'Bearer ABC123' })
          .build()
  const res = ResponseMock<CronResult>()

  ServerlessFunctionApi(req, res)

  expect(res.getStatusCode()).toEqual(401)
  expect(res.getBodyJson()).toStrictEqual({
      success: false,
      message: 'access denied',
  })
})

Mocking Request with Headers, Body & Cookies

import ServerlessFunctionApi from './pages/api/v1/myApi'
import { NextApiRequestBuilder, ResponseMock } from '@next-testing/api'

it('Mock Request and Response with headers', async () => {
  const req = new NextApiRequestBuilder()
          .setMethod('POST')
          .setHeaders({ authorization: 'Bearer ABC123' })
          .setCookies({ apiKey: "mytoken" })
          .setBody({
              posts: [{
                  content: "hello world"
              }]
          })
          .build()
  const res = ResponseMock<CronResult>()

  ServerlessFunctionApi(req, res)

  expect(res.getStatusCode()).toEqual(401)
  expect(res.getBodyJson()).toStrictEqual({
      success: false,
      message: 'access denied',
  })
})

Mocking Request with route parameters

If your handler depends on some parameters found in the path, eg. it's defined in pages/api/[foo]/echo, then you can specify it with setQuery().

import EchoHandler from './pages/api/[foo]/echo'
import { NextApiRequestBuilder, ResponseMock } from '@next-testing/api'

it('Mock Request and Response with route parameters', async () => {
  const req = new NextApiRequestBuilder()
          .setMethod('GET')
          .setQuery({
            foo: 'hello'
          })
          .build()
  const res = ResponseMock()

  EchoHandler(req, res)

  expect(res.getStatusCode()).toEqual(200)
  expect(res.getBodyJson()).toStrictEqual({
      foo: 'hello',
  })
})

FAQs

Package last updated on 15 Jul 2023

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