From cfb312c7062a23d4e20d42a6b7c39ece16929af7 Mon Sep 17 00:00:00 2001 From: feugy Date: Mon, 9 May 2022 14:41:15 +0200 Subject: [PATCH] feat!: removes NextResponse.json() function --- .../index.test.ts | 9 --- test/unit/web-runtime/next-cookies.test.ts | 4 +- test/unit/web-runtime/next-response.test.ts | 73 ------------------- 3 files changed, 3 insertions(+), 83 deletions(-) delete mode 100644 test/unit/web-runtime/next-response.test.ts diff --git a/test/e2e/middleware-cannot-return-body/index.test.ts b/test/e2e/middleware-cannot-return-body/index.test.ts index 54b79d29c65381..7cf7c856320992 100644 --- a/test/e2e/middleware-cannot-return-body/index.test.ts +++ b/test/e2e/middleware-cannot-return-body/index.test.ts @@ -17,15 +17,6 @@ describe.each([ export default function middleware(request) { return new Response(JSON.stringify({ foo: 'bar' }), { headers: {'content-type': 'application/json'} }); } -`, - }, - { - title: 'use NextResponse.json()', - middlewareCode: ` - import { NextResponse } from 'next/server'; - export default function middleware(request) { - return NextResponse.json({ foo: 'bar' }); - } `, }, ])('middleware cannot $title', ({ middlewareCode }) => { diff --git a/test/unit/web-runtime/next-cookies.test.ts b/test/unit/web-runtime/next-cookies.test.ts index 604819ecd0ce37..7dd69a4839f009 100644 --- a/test/unit/web-runtime/next-cookies.test.ts +++ b/test/unit/web-runtime/next-cookies.test.ts @@ -128,7 +128,9 @@ it('response.cookie does not modify options', async () => { ) const options = { maxAge: 10000 } - const response = NextResponse.json(null) + const response = new NextResponse(null, { + headers: { 'content-type': 'application/json' }, + }) response.cookies.set('cookieName', 'cookieValue', options) expect(options).toEqual({ maxAge: 10000 }) }) diff --git a/test/unit/web-runtime/next-response.test.ts b/test/unit/web-runtime/next-response.test.ts deleted file mode 100644 index 85351b6e221229..00000000000000 --- a/test/unit/web-runtime/next-response.test.ts +++ /dev/null @@ -1,73 +0,0 @@ -/* eslint-env jest */ - -import { Blob, File, FormData } from 'next/dist/compiled/formdata-node' -import { Crypto } from 'next/dist/server/web/sandbox/polyfills' -import { Response } from 'next/dist/server/web/spec-compliant/response' -import { Headers } from 'next/dist/server/web/spec-compliant/headers' -import * as streams from 'web-streams-polyfill/ponyfill' - -beforeAll(() => { - global['Blob'] = Blob - global['crypto'] = new Crypto() - global['File'] = File - global['FormData'] = FormData - global['Headers'] = Headers - global['ReadableStream'] = streams.ReadableStream - global['TransformStream'] = streams.TransformStream - global['Response'] = Response -}) - -afterAll(() => { - delete global['Blob'] - delete global['crypto'] - delete global['File'] - delete global['Headers'] - delete global['FormData'] - delete global['ReadableStream'] - delete global['TransformStream'] -}) - -const toJSON = async (response) => ({ - body: await response.json(), - contentType: response.headers.get('content-type'), - status: response.status, -}) - -it('automatically parses and formats JSON', async () => { - const { NextResponse } = await import( - 'next/dist/server/web/spec-extension/response' - ) - - expect(await toJSON(NextResponse.json({ message: 'hello!' }))).toMatchObject({ - contentType: 'application/json', - body: { message: 'hello!' }, - }) - - expect( - await toJSON(NextResponse.json({ status: 'success' }, { status: 201 })) - ).toMatchObject({ - contentType: 'application/json', - body: { status: 'success' }, - status: 201, - }) - - expect( - await toJSON( - NextResponse.json({ error: { code: 'bad_request' } }, { status: 400 }) - ) - ).toMatchObject({ - contentType: 'application/json', - body: { error: { code: 'bad_request' } }, - status: 400, - }) - - expect(await toJSON(NextResponse.json(null))).toMatchObject({ - contentType: 'application/json', - body: null, - }) - - expect(await toJSON(NextResponse.json(''))).toMatchObject({ - contentType: 'application/json', - body: '', - }) -})