https://github.com/seamapi/nextjs-ava-fixture
NextJS AVA testing fixture. Test API routes and pages with AVA
https://github.com/seamapi/nextjs-ava-fixture
Last synced: 3 months ago
JSON representation
NextJS AVA testing fixture. Test API routes and pages with AVA
- Host: GitHub
- URL: https://github.com/seamapi/nextjs-ava-fixture
- Owner: seamapi
- Created: 2021-11-02T04:24:11.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-12-17T18:46:08.000Z (6 months ago)
- Last Synced: 2025-03-19T09:42:56.917Z (3 months ago)
- Language: JavaScript
- Size: 420 KB
- Stars: 1
- Watchers: 4
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# NextJS Ava Fixture
This package sets up a testing fixture for NextJS applications.
It's great for testing API endpoints.
For more speed, you might want to use [nextjs-server-modules](https://github.com/seamapi/nextjs-server-modules) which
have a faster compilation process.## Installation
```sh
npm install --save nextjs-ava-fixture
```## Usage
```ts
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
```ts
// 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,
}
}
```