Skip to content

Commit

Permalink
Update tests to hold _middleware at the project root
Browse files Browse the repository at this point in the history
  • Loading branch information
javivelasco committed May 18, 2022
1 parent 107ae0e commit 828804e
Show file tree
Hide file tree
Showing 23 changed files with 41 additions and 25 deletions.
7 changes: 5 additions & 2 deletions test/e2e/middleware-can-use-wasm-files/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ function baseNextConfig(): Parameters<typeof createNext>[0] {
return exports.add_one(a);
}
`,
'pages/_middleware.js': `
import { increment } from '../src/add.js'
'pages/index.js': `
export default function () { return <div>Hello, world!</div> }
`,
'_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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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') {
Expand Down
4 changes: 2 additions & 2 deletions test/integration/middleware-dynamic-code/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
})
})
Expand Down
2 changes: 1 addition & 1 deletion test/integration/middleware-general/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
1 change: 1 addition & 0 deletions test/integration/middleware-module-errors/_middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => {}

This file was deleted.

3 changes: 1 addition & 2 deletions test/integration/middleware-module-errors/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
}

Expand Down Expand Up @@ -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'
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 3 additions & 5 deletions test/integration/telemetry/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,18 +791,16 @@ 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, [], {
stderr: true,
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <div>Hello, world!</div> }
`,

'_middleware.js': `
import customPackage from 'my-custom-package';
export default function middleware(_req) {
return new Response(JSON.stringify({
Expand Down
14 changes: 10 additions & 4 deletions test/production/generate-middleware-source-maps/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ describe('experimental.middlewareSourceMaps: true', () => {
},
},
files: {
'pages/_middleware.js': `
'pages/index.js': `
export default function () { return <div>Hello, world!</div> }
`,
'_middleware.js': `
export default function middleware() {
return new Response("Hello, world!");
}
Expand All @@ -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)
Expand All @@ -41,7 +44,10 @@ describe('experimental.middlewareSourceMaps: false', () => {
beforeAll(async () => {
next = await createNext({
files: {
'pages/_middleware.js': `
'pages/index.js': `
export default function () { return <div>Hello, world!</div> }
`,
'_middleware.js': `
export default function middleware() {
return new Response("Hello, world!");
}
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <div>Hello, world!</div> }
`,
'_middleware.js': `
export default function middleware() {
return new Response(JSON.stringify({
canBeInferred: process.env.CAN_BE_INFERRED,
Expand Down
2 changes: 1 addition & 1 deletion test/production/middleware-typescript/app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
"isolatedModules": true
},
"exclude": ["node_modules"],
"include": ["next-env.d.ts", "pages"]
"include": ["next-env.d.ts", "pages", "_middleware.ts"]
}
1 change: 1 addition & 0 deletions test/production/middleware-typescript/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion test/production/required-server-files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
),
Expand Down Expand Up @@ -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)
})
Expand Down

0 comments on commit 828804e

Please sign in to comment.