Skip to content

Next API Test Suite πŸš€ Simplifying the mocking and asserting of Next API Request & Responses

License

Notifications You must be signed in to change notification settings

alexandros-megas/next-testing

Β 
Β 

Repository files navigation

✨ NextJS Testing Framework ✨

npm (tag) NPM npm

Maintainability Rating Reliability Rating Coverage

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

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

About

Next API Test Suite πŸš€ Simplifying the mocking and asserting of Next API Request & Responses

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 99.2%
  • JavaScript 0.8%