1.2.2 • Published 3 years ago

nextjs-ava-fixture v1.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

NextJS Ava Fixture

This package sets up a testing fixture for NextJS applications.

It's great for testing API endpoints.

Installation

npm install --save nextjs-ava-fixture

Usage

import test from "ava"
import fixture from "nextjs-ava-fixture"

test("api endpoint", async (t) => {
  const { serverURL, axios } = await fixture(t)
  // axios is automatically configured with the base URL of the test server
  const { data: res } = await axios.get("/api/hello")
  t.deepEqual(res, { name: "John Doe" })
})

Usage with Middleware

// get-test-server.ts
import { createDb } from "lib/db"
import getFixture from "nextjs-ava-fixture"

export default async (t) => {
  const db = await createDb()

  const sharedDbMw = (next) => (req, res) => {
    req.db = db
    return next(req, res)
  }

  const fixture = await getFixture(t, {
    middlewares: [sharedDbMw],
  })

  return {
    ...fixture,
    db,
  }
}

FAQ / Common Issues

You need to rebuild nsm if you change a frontend page (a non-api route) or introduce a new route

You might see an error like this while running tests:

image

nsm build goes through your files and builds a routes.ts index, if it's up to date the file will be missing.

1.2.2

3 years ago

1.2.0

3 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.2.1

3 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago