Skip to content

Commit

Permalink
[test] Use new Redbox matchers in server-navigation-error (#76787)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored Mar 4, 2025
1 parent ab00c87 commit df77469
Showing 1 changed file with 109 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import { nextTestSetup } from 'e2e-utils'
import {
assertHasRedbox,
getRedboxDescription,
getRedboxSource,
} from 'next-test-utils'

describe('server-navigation-error', () => {
const { isTurbopack, next } = nextTestSetup({
Expand All @@ -13,64 +8,74 @@ describe('server-navigation-error', () => {
describe('pages router', () => {
it('should error on navigation API redirect', async () => {
const browser = await next.browser('/pages/redirect')
await assertHasRedbox(browser)
expect(await getRedboxDescription(browser)).toMatch(
`Next.js navigation API is not allowed to be used in Pages Router.`
)
const source = await getRedboxSource(browser)
if (isTurbopack) {
expect(source).toMatchInlineSnapshot(`
"pages/pages/redirect.tsx (4:10) @ Page

2 |
3 | export default function Page() {
> 4 | redirect('/')
| ^
5 | }
6 |"
// TODO(veil): investigate the column number is off by 1 between turbo and webpack
if (isTurbopack) {
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "Error: Next.js navigation API is not allowed to be used in Pages Router.",
"environmentLabel": null,
"label": "Runtime Error",
"source": "pages/pages/redirect.tsx (4:10) @ Page
> 4 | redirect('/')
| ^",
"stack": [
"Page pages/pages/redirect.tsx (4:10)",
],
}
`)
} else {
expect(source).toMatchInlineSnapshot(`
"pages/pages/redirect.tsx (4:11) @ Page
2 |
3 | export default function Page() {
> 4 | redirect('/')
| ^
5 | }
6 |"
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "Error: Next.js navigation API is not allowed to be used in Pages Router.",
"environmentLabel": null,
"label": "Runtime Error",
"source": "pages/pages/redirect.tsx (4:11) @ Page
> 4 | redirect('/')
| ^",
"stack": [
"Page pages/pages/redirect.tsx (4:11)",
],
}
`)
}
})

it('should error on navigation API notFound', async () => {
const browser = await next.browser('/pages/not-found')
await assertHasRedbox(browser)
expect(await getRedboxDescription(browser)).toMatch(
`Next.js navigation API is not allowed to be used in Pages Router.`
)
const source = await getRedboxSource(browser)
if (isTurbopack) {
expect(source).toMatchInlineSnapshot(`
"pages/pages/not-found.tsx (4:10) @ Page

2 |
3 | export default function Page() {
> 4 | notFound()
| ^
5 | }
6 |"
// TODO(veil): investigate the column number is off by 1 between turbo and webpack
if (isTurbopack) {
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "Error: Next.js navigation API is not allowed to be used in Pages Router.",
"environmentLabel": null,
"label": "Runtime Error",
"source": "pages/pages/not-found.tsx (4:10) @ Page
> 4 | notFound()
| ^",
"stack": [
"Page pages/pages/not-found.tsx (4:10)",
],
}
`)
} else {
expect(source).toMatchInlineSnapshot(`
"pages/pages/not-found.tsx (4:11) @ Page
2 |
3 | export default function Page() {
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "Error: Next.js navigation API is not allowed to be used in Pages Router.",
"environmentLabel": null,
"label": "Runtime Error",
"source": "pages/pages/not-found.tsx (4:11) @ Page
> 4 | notFound()
| ^
5 | }
6 |"
| ^",
"stack": [
"Page pages/pages/not-found.tsx (4:11)",
],
}
`)
}
})
Expand All @@ -81,69 +86,74 @@ describe('server-navigation-error', () => {
const browser = await next.browser('/middleware/redirect')
// FIXME: the first request to middleware error load didn't show the redbox, need one more reload
await browser.refresh()
await assertHasRedbox(browser)
expect(await getRedboxDescription(browser)).toMatch(
`Next.js navigation API is not allowed to be used in Middleware.`
)
const source = await getRedboxSource(browser)
if (isTurbopack) {
expect(source).toMatchInlineSnapshot(`
"middleware.ts (8:12) @ middleware

6 | notFound()
7 | } else if (req.nextUrl.pathname === '/middleware/redirect') {
> 8 | redirect('/')
| ^
9 | }
10 | }
11 |"
// TODO(veil): investigate the column number is off by 1 between turbo and webpack
if (isTurbopack) {
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "Error: Next.js navigation API is not allowed to be used in Middleware.",
"environmentLabel": null,
"label": "Runtime Error",
"source": "middleware.ts (8:12) @ middleware
> 8 | redirect('/')
| ^",
"stack": [
"middleware middleware.ts (8:12)",
],
}
`)
} else {
expect(source).toMatchInlineSnapshot(`
"middleware.ts (8:13) @ middleware
6 | notFound()
7 | } else if (req.nextUrl.pathname === '/middleware/redirect') {
> 8 | redirect('/')
| ^
9 | }
10 | }
11 |"
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "Error: Next.js navigation API is not allowed to be used in Middleware.",
"environmentLabel": null,
"label": "Runtime Error",
"source": "middleware.ts (8:13) @ middleware
> 8 | redirect('/')
| ^",
"stack": [
"middleware middleware.ts (8:13)",
],
}
`)
}
})

it('should error on navigation API not-found', async () => {
const browser = await next.browser('/middleware/not-found')
await assertHasRedbox(browser)
expect(await getRedboxDescription(browser)).toMatch(
`Next.js navigation API is not allowed to be used in Middleware.`
)
const source = await getRedboxSource(browser)

// TODO(veil): investigate the column number is off by 1 between turbo and webpack
if (isTurbopack) {
expect(source).toMatchInlineSnapshot(`
"middleware.ts (6:12) @ middleware
4 | export default function middleware(req: NextRequest) {
5 | if (req.nextUrl.pathname === '/middleware/not-found') {
> 6 | notFound()
| ^
7 | } else if (req.nextUrl.pathname === '/middleware/redirect') {
8 | redirect('/')
9 | }"
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "Error: Next.js navigation API is not allowed to be used in Middleware.",
"environmentLabel": null,
"label": "Runtime Error",
"source": "middleware.ts (6:12) @ middleware
> 6 | notFound()
| ^",
"stack": [
"middleware middleware.ts (6:12)",
],
}
`)
} else {
expect(source).toMatchInlineSnapshot(`
"middleware.ts (6:13) @ middleware
4 | export default function middleware(req: NextRequest) {
5 | if (req.nextUrl.pathname === '/middleware/not-found') {
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "Error: Next.js navigation API is not allowed to be used in Middleware.",
"environmentLabel": null,
"label": "Runtime Error",
"source": "middleware.ts (6:13) @ middleware
> 6 | notFound()
| ^
7 | } else if (req.nextUrl.pathname === '/middleware/redirect') {
8 | redirect('/')
9 | }"
| ^",
"stack": [
"middleware middleware.ts (6:13)",
],
}
`)
}
})
Expand Down

0 comments on commit df77469

Please sign in to comment.