Skip to content

Commit

Permalink
Expand middleware error assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Oct 26, 2024
1 parent 9748368 commit c7218cd
Showing 1 changed file with 62 additions and 35 deletions.
97 changes: 62 additions & 35 deletions test/development/middleware-errors/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from 'next-test-utils'
import stripAnsi from 'strip-ansi'
import { nextTestSetup } from 'e2e-utils'
import e from 'next/dist/compiled/cookie'

Check failure on line 10 in test/development/middleware-errors/index.test.ts

View workflow job for this annotation

GitHub Actions / lint / build

'e' is defined but never used. Allowed unused vars must match /^_/u

describe('middleware - development errors', () => {
const { next, isTurbopack } = nextTestSetup({
Expand All @@ -32,23 +33,22 @@ describe('middleware - development errors', () => {

it('logs the error correctly', async () => {
await next.fetch('/')
const output = stripAnsi(next.cliOutput)
await check(() => {
if (isTurbopack) {
expect(stripAnsi(next.cliOutput)).toMatch(
/middleware.js \(\d+:\d+\) @ __TURBOPACK__default__export__/
)
} else {
expect(stripAnsi(next.cliOutput)).toMatch(
/middleware.js \(\d+:\d+\) @ default/
)
}

expect(stripAnsi(next.cliOutput)).toMatch(/boom/)
return 'success'
}, 'success')
expect(output).not.toContain(
'webpack-internal:///(middleware)/./middleware.js'

await retry(() => {
expect(stripAnsi(next.cliOutput)).toContain('boom')
})
// TODO: assert on full, ignore-listed stack
expect(stripAnsi(next.cliOutput)).toContain(
isTurbopack
? '\n ⨯ middleware.js (3:15) @ __TURBOPACK__default__export__' +
'\n ⨯ Error: boom' +
'\n at __TURBOPACK__default__export__ (./middleware.js:3:15)'
: '\n ⨯ middleware.js (3:15) @ default' +
'\n ⨯ boom' +
'\n 1 |' +
'\n 2 | export default function () {' +
"\n> 3 | throw new Error('boom')" +
'\n | ^'
)
})

Expand Down Expand Up @@ -80,13 +80,20 @@ describe('middleware - development errors', () => {

it('logs the error correctly', async () => {
await next.fetch('/')
await check(
() => stripAnsi(next.cliOutput),
new RegExp(`unhandledRejection: Error: async boom!`, 'm')

await retry(() => {
expect(stripAnsi(next.cliOutput)).toContain(
'unhandledRejection: Error: async boom!'
)
})
// TODO: assert on full, ignore-listed stack
expect(stripAnsi(next.cliOutput)).toContain(
isTurbopack
? 'unhandledRejection: Error: async boom!\n at throwError ('
: 'unhandledRejection: Error: async boom!' +
'\n at throwError (webpack-internal:///(middleware)/./middleware.js:8:11)' +
'\n at __WEBPACK_DEFAULT_EXPORT__ (webpack-internal:///(middleware)/./middleware.js:11:5)'
)
// expect(output).not.toContain(
// 'webpack-internal:///(middleware)/./middleware.js'
// )
})

it('does not render the error', async () => {
Expand All @@ -113,17 +120,37 @@ describe('middleware - development errors', () => {

it('logs the error correctly', async () => {
await next.fetch('/')
// const output = stripAnsi(next.cliOutput)
await check(() => {
expect(stripAnsi(next.cliOutput)).toMatch(
/middleware.js \(\d+:\d+\) @ eval/

await retry(() => {
expect(stripAnsi(next.cliOutput)).toContain('Dynamic Code Evaluation')
})
// TODO: assert on full, ignore-listed stack
if (isTurbopack) {
// Locally, prefixes the "test is not defined".
// In CI, it prefixes "Dynamic Code Evaluation".
expect(stripAnsi(next.cliOutput)).toContain(
'\n ⚠ middleware.js (3:22) @ __TURBOPACK__default__export__' +
'\n ⨯ middleware.js (4:9) @ eval'
)
expect(stripAnsi(next.cliOutput)).toMatch(/test is not defined/)
return 'success'
}, 'success')
// expect(output).not.toContain(
// 'webpack-internal:///(middleware)/./middleware.js'
// )
}
expect(stripAnsi(next.cliOutput)).toContain(
isTurbopack
? '\n ⨯ Error: test is not defined' +
'\n at eval (./middleware.js:4:9)' +
'\n at <unknown> (./middleware.js:4:9'
: '\n ⨯ Error [ReferenceError]: test is not defined' +
'\n at eval (file://webpack-internal:///(middleware)/./middleware.js)' +
'\n at eval (webpack://_N_E/middleware.js?3bcb:4:8)'
)
expect(stripAnsi(next.cliOutput)).toContain(
isTurbopack
? "\n ⚠ Error: Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Edge Runtime" +
'\nLearn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation' +
'\n at __TURBOPACK__default__export__ (./middleware.js:3:22)'
: '\n ⚠ middleware.js (4:9) @ eval' +
"\n ⚠ Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Edge Runtime" +
'\nLearn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation'
)
})

it('renders the error correctly and recovers', async () => {
Expand Down Expand Up @@ -153,9 +180,9 @@ describe('middleware - development errors', () => {
await next.fetch('/')

await retry(() => {
expect(next.cliOutput).toContain(`Error: booooom!`)
expect(stripAnsi(next.cliOutput)).toContain(`Error: booooom!`)
})

// TODO: assert on full, ignore-listed stack
expect(stripAnsi(next.cliOutput)).toContain(
isTurbopack
? '\n ⨯ middleware.js (3:13) @ [project]/middleware.js [middleware] (ecmascript)' +
Expand Down

0 comments on commit c7218cd

Please sign in to comment.