Skip to content

Commit c7218cd

Browse files
committed
Expand middleware error assertions
1 parent 9748368 commit c7218cd

File tree

1 file changed

+62
-35
lines changed

1 file changed

+62
-35
lines changed

test/development/middleware-errors/index.test.ts

Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from 'next-test-utils'
88
import stripAnsi from 'strip-ansi'
99
import { nextTestSetup } from 'e2e-utils'
10+
import e from 'next/dist/compiled/cookie'
1011

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

3334
it('logs the error correctly', async () => {
3435
await next.fetch('/')
35-
const output = stripAnsi(next.cliOutput)
36-
await check(() => {
37-
if (isTurbopack) {
38-
expect(stripAnsi(next.cliOutput)).toMatch(
39-
/middleware.js \(\d+:\d+\) @ __TURBOPACK__default__export__/
40-
)
41-
} else {
42-
expect(stripAnsi(next.cliOutput)).toMatch(
43-
/middleware.js \(\d+:\d+\) @ default/
44-
)
45-
}
46-
47-
expect(stripAnsi(next.cliOutput)).toMatch(/boom/)
48-
return 'success'
49-
}, 'success')
50-
expect(output).not.toContain(
51-
'webpack-internal:///(middleware)/./middleware.js'
36+
37+
await retry(() => {
38+
expect(stripAnsi(next.cliOutput)).toContain('boom')
39+
})
40+
// TODO: assert on full, ignore-listed stack
41+
expect(stripAnsi(next.cliOutput)).toContain(
42+
isTurbopack
43+
? '\n ⨯ middleware.js (3:15) @ __TURBOPACK__default__export__' +
44+
'\n ⨯ Error: boom' +
45+
'\n at __TURBOPACK__default__export__ (./middleware.js:3:15)'
46+
: '\n ⨯ middleware.js (3:15) @ default' +
47+
'\n ⨯ boom' +
48+
'\n 1 |' +
49+
'\n 2 | export default function () {' +
50+
"\n> 3 | throw new Error('boom')" +
51+
'\n | ^'
5252
)
5353
})
5454

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

8181
it('logs the error correctly', async () => {
8282
await next.fetch('/')
83-
await check(
84-
() => stripAnsi(next.cliOutput),
85-
new RegExp(`unhandledRejection: Error: async boom!`, 'm')
83+
84+
await retry(() => {
85+
expect(stripAnsi(next.cliOutput)).toContain(
86+
'unhandledRejection: Error: async boom!'
87+
)
88+
})
89+
// TODO: assert on full, ignore-listed stack
90+
expect(stripAnsi(next.cliOutput)).toContain(
91+
isTurbopack
92+
? 'unhandledRejection: Error: async boom!\n at throwError ('
93+
: 'unhandledRejection: Error: async boom!' +
94+
'\n at throwError (webpack-internal:///(middleware)/./middleware.js:8:11)' +
95+
'\n at __WEBPACK_DEFAULT_EXPORT__ (webpack-internal:///(middleware)/./middleware.js:11:5)'
8696
)
87-
// expect(output).not.toContain(
88-
// 'webpack-internal:///(middleware)/./middleware.js'
89-
// )
9097
})
9198

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

114121
it('logs the error correctly', async () => {
115122
await next.fetch('/')
116-
// const output = stripAnsi(next.cliOutput)
117-
await check(() => {
118-
expect(stripAnsi(next.cliOutput)).toMatch(
119-
/middleware.js \(\d+:\d+\) @ eval/
123+
124+
await retry(() => {
125+
expect(stripAnsi(next.cliOutput)).toContain('Dynamic Code Evaluation')
126+
})
127+
// TODO: assert on full, ignore-listed stack
128+
if (isTurbopack) {
129+
// Locally, prefixes the "test is not defined".
130+
// In CI, it prefixes "Dynamic Code Evaluation".
131+
expect(stripAnsi(next.cliOutput)).toContain(
132+
'\n ⚠ middleware.js (3:22) @ __TURBOPACK__default__export__' +
133+
'\n ⨯ middleware.js (4:9) @ eval'
120134
)
121-
expect(stripAnsi(next.cliOutput)).toMatch(/test is not defined/)
122-
return 'success'
123-
}, 'success')
124-
// expect(output).not.toContain(
125-
// 'webpack-internal:///(middleware)/./middleware.js'
126-
// )
135+
}
136+
expect(stripAnsi(next.cliOutput)).toContain(
137+
isTurbopack
138+
? '\n ⨯ Error: test is not defined' +
139+
'\n at eval (./middleware.js:4:9)' +
140+
'\n at <unknown> (./middleware.js:4:9'
141+
: '\n ⨯ Error [ReferenceError]: test is not defined' +
142+
'\n at eval (file://webpack-internal:///(middleware)/./middleware.js)' +
143+
'\n at eval (webpack://_N_E/middleware.js?3bcb:4:8)'
144+
)
145+
expect(stripAnsi(next.cliOutput)).toContain(
146+
isTurbopack
147+
? "\n ⚠ Error: Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Edge Runtime" +
148+
'\nLearn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation' +
149+
'\n at __TURBOPACK__default__export__ (./middleware.js:3:22)'
150+
: '\n ⚠ middleware.js (4:9) @ eval' +
151+
"\n ⚠ Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Edge Runtime" +
152+
'\nLearn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation'
153+
)
127154
})
128155

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

155182
await retry(() => {
156-
expect(next.cliOutput).toContain(`Error: booooom!`)
183+
expect(stripAnsi(next.cliOutput)).toContain(`Error: booooom!`)
157184
})
158-
185+
// TODO: assert on full, ignore-listed stack
159186
expect(stripAnsi(next.cliOutput)).toContain(
160187
isTurbopack
161188
? '\n ⨯ middleware.js (3:13) @ [project]/middleware.js [middleware] (ecmascript)' +

0 commit comments

Comments
 (0)