Socket
Socket
Sign inDemoInstall

@next-testing/api

Package Overview
Dependencies
1
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @next-testing/api

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


Version published
Weekly downloads
126
increased by8.62%
Maintainers
1
Created
Weekly downloads
 

Changelog

Source

2.0.0 (2023-07-15)

Bug Fixes

  • apply latest semver version (6c5f47f)

chore

  • deps: drop node 14 support (>= 16) (7372db3)

BREAKING CHANGES

  • deps: Drops support for node@14, lowest version is now node@16

Readme

Source
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',
  })
})

Keywords

FAQs

Last updated on 15 Jul 2023

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc