Files
4588a73542 Convert tests using createNext -> nextTestSetup (#93767)
## What?

Converts existing `createNext()` usage into `nextTestSetup()`. 

`createNext()` was the setup step we had before `nextTestSetup()` was
added.

This PR focused on the simple conversion cases. There will be a
follow-up to complete the last few.

---------

Co-authored-by: Cursor <[email protected]>
2026-05-12 13:16:31 +02:00

86 lines
2.3 KiB
TypeScript

import { nextTestSetup } from 'e2e-utils'
import { fetchViaHTTP } from 'next-test-utils'
describe('Middleware fetches with any HTTP method', () => {
const { next } = nextTestSetup({
files: {
'pages/api/ping.js': `
export default (req, res) => {
res.send(JSON.stringify({
method: req.method,
headers: {...req.headers},
}))
}
`,
'middleware.js': `
import { NextResponse } from 'next/server';
const HTTP_ECHO_URL = 'https://next-data-api-endpoint.vercel.app/api/echo-headers';
export default async (req) => {
const kind = req.nextUrl.searchParams.get('kind')
const handler = handlers[kind] ?? handlers['normal-fetch'];
const response = await handler({url: HTTP_ECHO_URL, method: req.method});
const json = await response.text()
const res = NextResponse.next();
res.headers.set('x-resolved', json ?? '{}');
return res
}
const handlers = {
'new-request': ({url, method}) =>
fetch(new Request(url, { method, headers: { 'x-kind': 'new-request' } })),
'normal-fetch': ({url, method}) =>
fetch(url, { method, headers: { 'x-kind': 'normal-fetch' } })
}
`,
},
dependencies: {},
})
it('passes the method on a direct fetch request', async () => {
const response = await fetchViaHTTP(
next.url,
'/api/ping',
{},
{ method: 'POST' }
)
const json = await response.json()
expect(json).toMatchObject({
method: 'POST',
})
const headerJson = JSON.parse(response.headers.get('x-resolved'))
expect(headerJson).toMatchObject({
method: 'POST',
headers: {
'x-kind': 'normal-fetch',
},
})
})
it('passes the method when providing a Request object', async () => {
const response = await fetchViaHTTP(
next.url,
'/api/ping',
{ kind: 'new-request' },
{ method: 'POST' }
)
const json = await response.json()
expect(json).toMatchObject({
method: 'POST',
})
const headerJson = JSON.parse(response.headers.get('x-resolved'))
expect(headerJson).toMatchObject({
method: 'POST',
headers: {
'x-kind': 'new-request',
},
})
})
})