Comments (1)
Hi there! NTARH has support for all API route types. Testing catch-all routes shouldn't be too different from any other route, you just have to provide the expected params
object.
Using the Next.js docs as an example (given URL /api/post/a/b/c
):
/* file: pages/api/post/[...slug].ts */
import type { NextApiRequest, NextApiResponse } from 'next'
export default function handler(req: NextApiRequest, res: NextApiResponse) {
const { slug } = req.query
res.end(`Post: ${slug.join(', ')}`)
}
/* file: test/main.test.ts */
import { testApiHandler } from 'next-test-api-route-handler';
// Import the handler under test from the pages/api directory
import * as pagesHandler from '../pages/api/[...slug]';
it('does what I want', async () => {
expect.hasAssertions();
await testApiHandler({
pagesHandler,
params: { slug: ['a', 'b', 'c'] },
test: async ({ fetch }) => {
const res = await fetch({ method: 'POST' });
await expect(res.text()).resolves.toBe('Post: a, b, c');
}
});
});
from next-test-api-route-handler.
Related Issues (20)
- ValidationPipe not triggering in unit tests HOT 4
- next-test-api-route-handler (NTARH) failed to import api-utils.js HOT 2
- Verify support for app-based route handlers HOT 1
- Error: Invariant: Method expects to have requestAsyncStorage, none available HOT 3
- Incompatibility with [email protected] HOT 5
- Could not find a declaration file for module 'next-test-api-route-handler'. HOT 4
- Doesn't work in node 20.8.0 to 20.x HOT 7
- Bypassing MSW does not work with MSW 2.X HOT 2
- Vercel lambda governing compatibility badge isn't tracking latest compat version anymore HOT 1
- App Router Examples HOT 2
- Drop node-fetch in favor of native fetch functionality HOT 1
- Jest did not exit one second after the test run has completed. HOT 3
- Recursive redirection occurs when redirecting within the handler HOT 3
- ReferenceError: Request is not defined HOT 6
- Relative URL doesn't work with appHandler: "Invalid URL" error HOT 4
- Insuffient type for `params` object given a well-typed dynamic route handler HOT 1
- Potential NTARH incompatibility with `next/jest.js` HOT 1
- originalGlobalFetch is not a function HOT 2
- Allow this to be used with Vitest HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from next-test-api-route-handler.