mirror of
https://github.com/hanzoai/next.js.git
synced 2026-09-24 17:00:51 +00:00
You'll probably want to disable whitespace in the diff ## Description This allows for better editor support by using `describe` or functions called `describe` with the same syntax instead of custom names. Changes: - `nextTestSetup` can be used instead of `createNextDescribe` keeping the same behaviour but being called inside a `describe` "block" (not applied everywhere) - `getSnapshotTestDescribe` replaced with a custom `describe.each` - `sandbox` helper function for `acceptance`/`acceptance-app` merged into a single shared one - `outdent` to remove the indent from inline files in tests which helps with consistent snapshots
100 lines
2.7 KiB
TypeScript
100 lines
2.7 KiB
TypeScript
import { FileRef, nextTestSetup } from 'e2e-utils'
|
|
import { sandbox } from 'development-sandbox'
|
|
import path from 'path'
|
|
import { outdent } from 'outdent'
|
|
|
|
const middlewarePath = 'middleware.js'
|
|
const middlewareWarning = `A middleware can not alter response's body`
|
|
|
|
describe('middlewares', () => {
|
|
const { next } = nextTestSetup({
|
|
files: new FileRef(
|
|
path.join(__dirname, '..', 'acceptance', 'fixtures', 'default-template')
|
|
),
|
|
skipStart: true,
|
|
})
|
|
|
|
let cleanup
|
|
afterEach(() => cleanup?.())
|
|
|
|
it.each([
|
|
{
|
|
title: 'returning response with literal string',
|
|
code: outdent`
|
|
export default function middleware() {
|
|
return new Response('this is not allowed');
|
|
}
|
|
`,
|
|
},
|
|
{
|
|
title: 'returning response with literal number',
|
|
code: outdent`
|
|
export default function middleware() {
|
|
return new Response(10);
|
|
}
|
|
`,
|
|
},
|
|
{
|
|
title: 'returning response with JSON.stringify',
|
|
code: outdent`
|
|
export default function middleware() {
|
|
return new Response(JSON.stringify({ foo: 'this is not allowed' }));
|
|
}
|
|
`,
|
|
},
|
|
{
|
|
title: 'populating response with a value',
|
|
code: outdent`
|
|
export default function middleware(request) {
|
|
const body = JSON.stringify({ foo: 'this should not be allowed, but hard to detect with AST' })
|
|
return new Response(body);
|
|
}
|
|
`,
|
|
},
|
|
{
|
|
title: 'populating response with a function call',
|
|
code: outdent`
|
|
function buildBody() {
|
|
return 'this should not be allowed, but hard to detect with AST'
|
|
}
|
|
export default function middleware(request) {
|
|
return new Response(buildBody());
|
|
}
|
|
`,
|
|
},
|
|
{
|
|
title: 'populating response with an async function call',
|
|
code: outdent`
|
|
export default async function middleware(request) {
|
|
return new Response(await fetch('https://example.vercel.sh'));
|
|
}
|
|
`,
|
|
},
|
|
])('does not warn when $title', async ({ code }) => {
|
|
;({ cleanup } = await sandbox(next, new Map([[middlewarePath, code]])))
|
|
expect(next.cliOutput).not.toMatch(middlewareWarning)
|
|
})
|
|
|
|
it.each([
|
|
{
|
|
title: 'returning null reponse body',
|
|
code: outdent`
|
|
export default function middleware() {
|
|
return new Response(null);
|
|
}
|
|
`,
|
|
},
|
|
{
|
|
title: 'returning undefined response body',
|
|
code: outdent`
|
|
export default function middleware() {
|
|
return new Response(undefined);
|
|
}
|
|
`,
|
|
},
|
|
])('does not warn when $title', async ({ code }) => {
|
|
;({ cleanup } = await sandbox(next, new Map([[middlewarePath, code]])))
|
|
expect(next.cliOutput).not.toMatch(middlewareWarning)
|
|
})
|
|
})
|