From 828804e1dcb476dd247fcbd4c49f3424e7c4874d Mon Sep 17 00:00:00 2001 From: Javi Velasco Date: Wed, 18 May 2022 15:30:00 +0200 Subject: [PATCH] Update tests to hold `_middleware` at the project root --- .../middleware-can-use-wasm-files/index.test.ts | 7 +++++-- .../{pages => }/_middleware.js | 0 .../{pages => }/_middleware.js | 2 +- .../middleware-dynamic-code/test/index.test.js | 4 ++-- .../middleware-general/{pages => }/_middleware.js | 0 .../middleware-general/test/index.test.js | 2 +- .../middleware-module-errors/_middleware.js | 1 + .../middleware-module-errors/pages/_middleware.js | 1 - .../middleware-module-errors/test/index.test.js | 3 +-- .../{pages => }/_middleware.js | 0 .../{pages => }/_middleware.js | 0 .../{pages => }/_middleware.js | 2 +- .../middleware-rewrites/{pages => }/_middleware.js | 0 test/integration/telemetry/test/index.test.js | 8 +++----- .../index.test.ts | 7 +++++-- .../generate-middleware-source-maps/index.test.ts | 14 ++++++++++---- .../index.test.ts | 5 ++++- .../app/{pages => }/_middleware.ts | 0 .../middleware-typescript/app/tsconfig.json | 2 +- .../middleware-typescript/test/index.test.ts | 1 + .../index.test.ts | 2 +- test/production/required-server-files.test.ts | 5 ++++- .../{pages => }/_middleware.js | 0 23 files changed, 41 insertions(+), 25 deletions(-) rename test/integration/middleware-base-path/{pages => }/_middleware.js (100%) rename test/integration/middleware-dynamic-code/{pages => }/_middleware.js (88%) rename test/integration/middleware-general/{pages => }/_middleware.js (100%) create mode 100644 test/integration/middleware-module-errors/_middleware.js delete mode 100644 test/integration/middleware-module-errors/pages/_middleware.js rename test/integration/middleware-preflight/{pages => }/_middleware.js (100%) rename test/integration/middleware-redirects/{pages => }/_middleware.js (100%) rename test/integration/middleware-responses/{pages => }/_middleware.js (98%) rename test/integration/middleware-rewrites/{pages => }/_middleware.js (100%) rename test/production/middleware-typescript/app/{pages => }/_middleware.ts (100%) rename test/production/required-server-files/{pages => }/_middleware.js (100%) diff --git a/test/e2e/middleware-can-use-wasm-files/index.test.ts b/test/e2e/middleware-can-use-wasm-files/index.test.ts index 808bff344490f2..87881ad2345536 100644 --- a/test/e2e/middleware-can-use-wasm-files/index.test.ts +++ b/test/e2e/middleware-can-use-wasm-files/index.test.ts @@ -17,8 +17,11 @@ function baseNextConfig(): Parameters[0] { return exports.add_one(a); } `, - 'pages/_middleware.js': ` - import { increment } from '../src/add.js' + 'pages/index.js': ` + export default function () { return
Hello, world!
} + `, + '_middleware.js': ` + import { increment } from './src/add.js' export default async function middleware(request) { const input = Number(request.nextUrl.searchParams.get('input')) || 1; const value = await increment(input); diff --git a/test/integration/middleware-base-path/pages/_middleware.js b/test/integration/middleware-base-path/_middleware.js similarity index 100% rename from test/integration/middleware-base-path/pages/_middleware.js rename to test/integration/middleware-base-path/_middleware.js diff --git a/test/integration/middleware-dynamic-code/pages/_middleware.js b/test/integration/middleware-dynamic-code/_middleware.js similarity index 88% rename from test/integration/middleware-dynamic-code/pages/_middleware.js rename to test/integration/middleware-dynamic-code/_middleware.js index 0ea96d0bb80239..60a8860854161a 100644 --- a/test/integration/middleware-dynamic-code/pages/_middleware.js +++ b/test/integration/middleware-dynamic-code/_middleware.js @@ -1,4 +1,4 @@ -import { notUsingEval, usingEval } from '../lib/utils' +import { notUsingEval, usingEval } from './lib/utils' export async function middleware(request) { if (request.nextUrl.pathname === '/using-eval') { diff --git a/test/integration/middleware-dynamic-code/test/index.test.js b/test/integration/middleware-dynamic-code/test/index.test.js index ce2239fd02a77a..aff1ef9d757645 100644 --- a/test/integration/middleware-dynamic-code/test/index.test.js +++ b/test/integration/middleware-dynamic-code/test/index.test.js @@ -45,7 +45,7 @@ describe('Middleware usage of dynamic code evaluation', () => { expect(json.value).toEqual(100) expect(output).toContain(DYNAMIC_CODE_ERROR) expect(output).toContain('DynamicCodeEvaluationWarning') - expect(output).toContain('pages/_middleware') + expect(output).toContain('./_middleware') // TODO check why that has a backslash on windows expect(output).toMatch(/lib[\\/]utils\.js/) expect(output).toContain('usingEval') @@ -81,7 +81,7 @@ describe('Middleware usage of dynamic code evaluation', () => { it('should have middleware warning during build', () => { expect(buildResult.stderr).toContain(`Failed to compile`) expect(buildResult.stderr).toContain(`Used by usingEval`) - expect(buildResult.stderr).toContain(`./pages/_middleware.js`) + expect(buildResult.stderr).toContain(`./_middleware.js`) expect(buildResult.stderr).toContain(DYNAMIC_CODE_ERROR) }) }) diff --git a/test/integration/middleware-general/pages/_middleware.js b/test/integration/middleware-general/_middleware.js similarity index 100% rename from test/integration/middleware-general/pages/_middleware.js rename to test/integration/middleware-general/_middleware.js diff --git a/test/integration/middleware-general/test/index.test.js b/test/integration/middleware-general/test/index.test.js index 87d245d009dd84..8c9904f8d93f65 100644 --- a/test/integration/middleware-general/test/index.test.js +++ b/test/integration/middleware-general/test/index.test.js @@ -57,7 +57,7 @@ describe('Middleware Runtime', () => { const text = await browser.elementByCss('h1').text() expect(text).toEqual('AboutA') - const middlewarePath = join(context.appDir, '/pages/_middleware.js') + const middlewarePath = join(context.appDir, '/_middleware.js') const originalContent = fs.readFileSync(middlewarePath, 'utf-8') const editedContent = originalContent.replace('/about/a', '/about/b') diff --git a/test/integration/middleware-module-errors/_middleware.js b/test/integration/middleware-module-errors/_middleware.js new file mode 100644 index 00000000000000..ead516c976e9b3 --- /dev/null +++ b/test/integration/middleware-module-errors/_middleware.js @@ -0,0 +1 @@ +export default () => {} diff --git a/test/integration/middleware-module-errors/pages/_middleware.js b/test/integration/middleware-module-errors/pages/_middleware.js deleted file mode 100644 index ea9b101e1c2259..00000000000000 --- a/test/integration/middleware-module-errors/pages/_middleware.js +++ /dev/null @@ -1 +0,0 @@ -export default function () {} diff --git a/test/integration/middleware-module-errors/test/index.test.js b/test/integration/middleware-module-errors/test/index.test.js index 8cd69dd897b34a..091ed64e5a49ea 100644 --- a/test/integration/middleware-module-errors/test/index.test.js +++ b/test/integration/middleware-module-errors/test/index.test.js @@ -20,7 +20,7 @@ const context = { appDir: join(__dirname, '../'), buildLogs: { output: '', stdout: '', stderr: '' }, logs: { output: '', stdout: '', stderr: '' }, - middleware: new File(join(__dirname, '../pages/_middleware.js')), + middleware: new File(join(__dirname, '../_middleware.js')), page: new File(join(__dirname, '../pages/index.js')), } @@ -161,7 +161,6 @@ describe('Middleware importing Node.js modules', () => { file.write(`export function middleware() {}`) try { const res = await fetchViaHTTP(context.appPort, '/about') - console.log(context.logs.stderr) expect(context.logs.stderr).toContain( 'nested Middleware is deprecated (found pages/about/_middleware) - https://nextjs.org/docs/messages/nested-middleware' ) diff --git a/test/integration/middleware-preflight/pages/_middleware.js b/test/integration/middleware-preflight/_middleware.js similarity index 100% rename from test/integration/middleware-preflight/pages/_middleware.js rename to test/integration/middleware-preflight/_middleware.js diff --git a/test/integration/middleware-redirects/pages/_middleware.js b/test/integration/middleware-redirects/_middleware.js similarity index 100% rename from test/integration/middleware-redirects/pages/_middleware.js rename to test/integration/middleware-redirects/_middleware.js diff --git a/test/integration/middleware-responses/pages/_middleware.js b/test/integration/middleware-responses/_middleware.js similarity index 98% rename from test/integration/middleware-responses/pages/_middleware.js rename to test/integration/middleware-responses/_middleware.js index ef54722b35bbd7..2e91aec519d865 100644 --- a/test/integration/middleware-responses/pages/_middleware.js +++ b/test/integration/middleware-responses/_middleware.js @@ -1,7 +1,7 @@ import { NextResponse } from 'next/server' import { createElement } from 'react' import { renderToString } from 'react-dom/server.browser' -import { getText } from '../lib/utils' +import { getText } from './lib/utils' export async function middleware(request, ev) { // eslint-disable-next-line no-undef diff --git a/test/integration/middleware-rewrites/pages/_middleware.js b/test/integration/middleware-rewrites/_middleware.js similarity index 100% rename from test/integration/middleware-rewrites/pages/_middleware.js rename to test/integration/middleware-rewrites/_middleware.js diff --git a/test/integration/telemetry/test/index.test.js b/test/integration/telemetry/test/index.test.js index fa4c9ac2b5bdcd..f792e72f0400f3 100644 --- a/test/integration/telemetry/test/index.test.js +++ b/test/integration/telemetry/test/index.test.js @@ -791,10 +791,8 @@ describe('Telemetry CLI', () => { it('emits telemetry for usage of _middleware', async () => { await fs.writeFile( - path.join(appDir, 'pages/_middleware.js'), - `export function middleware (evt) { - evt.respondWith(new Response(null)) - }` + path.join(appDir, '_middleware.js'), + `export function middleware () { }` ) const { stderr } = await nextBuild(appDir, [], { @@ -802,7 +800,7 @@ describe('Telemetry CLI', () => { env: { NEXT_TELEMETRY_DEBUG: 1 }, }) - await fs.remove(path.join(appDir, 'pages/_middleware.js')) + await fs.remove(path.join(appDir, '_middleware.js')) const regex = /NEXT_BUILD_OPTIMIZED[\s\S]+?{([\s\S]+?)}/ const optimizedEvt = regex.exec(stderr).pop() diff --git a/test/production/dependencies-can-use-env-vars-in-middlewares/index.test.ts b/test/production/dependencies-can-use-env-vars-in-middlewares/index.test.ts index 515b68fc23610e..de32b4605dfa7c 100644 --- a/test/production/dependencies-can-use-env-vars-in-middlewares/index.test.ts +++ b/test/production/dependencies-can-use-env-vars-in-middlewares/index.test.ts @@ -25,8 +25,11 @@ describe('dependencies can use env vars in middlewares', () => { module.exports = () => process.env.MY_CUSTOM_PACKAGE_ENV_VAR; `, - // The actual middleware code - 'pages/_middleware.js': ` + 'pages/index.js': ` + export default function () { return
Hello, world!
} + `, + + '_middleware.js': ` import customPackage from 'my-custom-package'; export default function middleware(_req) { return new Response(JSON.stringify({ diff --git a/test/production/generate-middleware-source-maps/index.test.ts b/test/production/generate-middleware-source-maps/index.test.ts index c4c3cf5f87dc12..3f408eb759e997 100644 --- a/test/production/generate-middleware-source-maps/index.test.ts +++ b/test/production/generate-middleware-source-maps/index.test.ts @@ -14,7 +14,10 @@ describe('experimental.middlewareSourceMaps: true', () => { }, }, files: { - 'pages/_middleware.js': ` + 'pages/index.js': ` + export default function () { return
Hello, world!
} + `, + '_middleware.js': ` export default function middleware() { return new Response("Hello, world!"); } @@ -28,7 +31,7 @@ describe('experimental.middlewareSourceMaps: true', () => { it('generates a source map', async () => { const middlewarePath = path.resolve( next.testDir, - '.next/server/pages/_middleware.js' + '.next/server/_middleware.js' ) expect(await fs.pathExists(middlewarePath)).toEqual(true) expect(await fs.pathExists(`${middlewarePath}.map`)).toEqual(true) @@ -41,7 +44,10 @@ describe('experimental.middlewareSourceMaps: false', () => { beforeAll(async () => { next = await createNext({ files: { - 'pages/_middleware.js': ` + 'pages/index.js': ` + export default function () { return
Hello, world!
} + `, + '_middleware.js': ` export default function middleware() { return new Response("Hello, world!"); } @@ -55,7 +61,7 @@ describe('experimental.middlewareSourceMaps: false', () => { it('does not generate a source map', async () => { const middlewarePath = path.resolve( next.testDir, - '.next/server/pages/_middleware.js' + '.next/server/_middleware.js' ) expect(await fs.pathExists(middlewarePath)).toEqual(true) expect(await fs.pathExists(`${middlewarePath}.map`)).toEqual(false) diff --git a/test/production/middleware-environment-variables-in-node-server-reflect-the-usage-inference/index.test.ts b/test/production/middleware-environment-variables-in-node-server-reflect-the-usage-inference/index.test.ts index 7aa6ddb7577b9c..4ba90386bee240 100644 --- a/test/production/middleware-environment-variables-in-node-server-reflect-the-usage-inference/index.test.ts +++ b/test/production/middleware-environment-variables-in-node-server-reflect-the-usage-inference/index.test.ts @@ -14,7 +14,10 @@ describe('middleware environment variables in node server reflect the usage infe beforeAll(async () => { next = await createNext({ files: { - 'pages/_middleware.js': ` + 'pages/index.js': ` + export default function () { return
Hello, world!
} + `, + '_middleware.js': ` export default function middleware() { return new Response(JSON.stringify({ canBeInferred: process.env.CAN_BE_INFERRED, diff --git a/test/production/middleware-typescript/app/pages/_middleware.ts b/test/production/middleware-typescript/app/_middleware.ts similarity index 100% rename from test/production/middleware-typescript/app/pages/_middleware.ts rename to test/production/middleware-typescript/app/_middleware.ts diff --git a/test/production/middleware-typescript/app/tsconfig.json b/test/production/middleware-typescript/app/tsconfig.json index e327470b183414..f906a5d7f6873e 100644 --- a/test/production/middleware-typescript/app/tsconfig.json +++ b/test/production/middleware-typescript/app/tsconfig.json @@ -16,5 +16,5 @@ "isolatedModules": true }, "exclude": ["node_modules"], - "include": ["next-env.d.ts", "pages"] + "include": ["next-env.d.ts", "pages", "_middleware.ts"] } diff --git a/test/production/middleware-typescript/test/index.test.ts b/test/production/middleware-typescript/test/index.test.ts index 0521983d61a123..933c3a25a33700 100644 --- a/test/production/middleware-typescript/test/index.test.ts +++ b/test/production/middleware-typescript/test/index.test.ts @@ -14,6 +14,7 @@ describe('should set-up next', () => { next = await createNext({ files: { pages: new FileRef(join(appDir, 'pages')), + '_middleware.ts': new FileRef(join(appDir, '_middleware.ts')), 'tsconfig.json': new FileRef(join(appDir, 'tsconfig.json')), 'next.config.js': new FileRef(join(appDir, 'next.config.js')), }, diff --git a/test/production/reading-request-body-in-middleware/index.test.ts b/test/production/reading-request-body-in-middleware/index.test.ts index 5deff6b2affa82..7fdc2c75218456 100644 --- a/test/production/reading-request-body-in-middleware/index.test.ts +++ b/test/production/reading-request-body-in-middleware/index.test.ts @@ -8,7 +8,7 @@ describe('reading request body in middleware', () => { beforeAll(async () => { next = await createNext({ files: { - 'pages/_middleware.js': ` + '_middleware.js': ` const { NextResponse } = require('next/server'); export default async function middleware(request) { diff --git a/test/production/required-server-files.test.ts b/test/production/required-server-files.test.ts index fa04db44a57017..bf8fdb846f5431 100644 --- a/test/production/required-server-files.test.ts +++ b/test/production/required-server-files.test.ts @@ -30,6 +30,9 @@ describe('should set-up next', () => { files: { pages: new FileRef(join(__dirname, 'required-server-files/pages')), lib: new FileRef(join(__dirname, 'required-server-files/lib')), + '_middleware.js': new FileRef( + join(__dirname, 'required-server-files/_middleware.js') + ), 'data.txt': new FileRef( join(__dirname, 'required-server-files/data.txt') ), @@ -159,7 +162,7 @@ describe('should set-up next', () => { ).toBe(true) expect( await fs.pathExists( - join(next.testDir, 'standalone/.next/server/pages/_middleware.js') + join(next.testDir, 'standalone/.next/server/_middleware.js') ) ).toBe(true) }) diff --git a/test/production/required-server-files/pages/_middleware.js b/test/production/required-server-files/_middleware.js similarity index 100% rename from test/production/required-server-files/pages/_middleware.js rename to test/production/required-server-files/_middleware.js