Unverified Commit 2ce4e440 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

Expand `server-source-maps` scenarios to cover Edge runtime (#72288)

...and use test patterns more consistently
parent 9b78a23b
import { nextTestSetup } from 'e2e-utils'
import { check } from 'next-test-utils'
describe('app-render-error-log', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should log the correct values on app-render error', async () => {
const outputIndex = next.cliOutput.length
await next.fetch('/')
await check(() => next.cliOutput.slice(outputIndex), /at Page/)
const cliOutput = next.cliOutput.slice(outputIndex)
await check(() => cliOutput, /digest:/)
expect(cliOutput).toInclude('Error: boom')
expect(cliOutput).toInclude('at fn2 (./app/fn.ts')
expect(cliOutput).toMatch(/at (Module\.)?fn1 \(\.\/app\/fn\.ts/)
expect(cliOutput).toInclude('at Page (./app/page.tsx')
expect(cliOutput).not.toInclude('webpack-internal')
})
it('should log the correct values on app-render error with edge runtime', async () => {
const outputIndex = next.cliOutput.length
await next.fetch('/edge')
await check(() => next.cliOutput.slice(outputIndex), /at EdgePage/)
const cliOutput = next.cliOutput.slice(outputIndex)
await check(() => cliOutput, /digest:/)
expect(cliOutput).toInclude('Error: boom')
expect(cliOutput).toInclude('at fn2 (./app/fn.ts')
expect(cliOutput).toMatch(/at (Module\.)?fn1 \(\.\/app\/fn\.ts/)
expect(cliOutput).toInclude('at EdgePage (./app/edge/page.tsx')
expect(cliOutput).not.toInclude('webpack-internal')
})
})
export const runtime = 'edge'
import { fn1 } from '../fn'
export default function EdgePage() {
fn1()
return <p>hello world</p>
}
function fn2() {
throw new Error('boom')
}
export function fn1() {
fn2()
}
import { Suspense } from 'react'
export default function Root({ children }) { export default function Root({ children }) {
return ( return (
<html> <html>
<body>{children}</body> <body>
<Suspense>{children}</Suspense>
</body>
</html> </html>
) )
} }
import { connection } from 'next/server'
function logError(cause) { function logError(cause) {
const error = new Error('Boom', { cause }) const error = new Error('Boom', { cause })
console.error(error) console.error(error)
} }
export default function Page() { export default async function Page() {
await connection()
const error = new Error('Boom') const error = new Error('Boom')
logError(error) logError(error)
return null return null
......
import { connection } from 'next/server'
class UnnamedError extends Error {} class UnnamedError extends Error {}
class NamedError extends Error { class NamedError extends Error {
name = 'MyError' name = 'MyError'
} }
export default function Page() { export default async function Page() {
await connection()
console.error(new UnnamedError('Foo')) console.error(new UnnamedError('Foo'))
console.error(new NamedError('Bar')) console.error(new NamedError('Bar'))
return null return null
......
import { connection } from 'next/server'
import { run } from 'internal-pkg' import { run } from 'internal-pkg'
function logError() { function logError() {
...@@ -5,7 +6,9 @@ function logError() { ...@@ -5,7 +6,9 @@ function logError() {
console.error(error) console.error(error)
} }
export default function Page() { export default async function Page() {
await connection()
run(() => logError()) run(() => logError())
return null return null
} }
import { connection } from 'next/server'
function logError() { function logError() {
const error = new Error('Boom') const error = new Error('Boom')
console.error(error) console.error(error)
} }
export default function Page() { export default async function Page() {
await connection()
logError() logError()
return null return null
} }
'use client'
function throwError() {
throw new Error('Boom')
}
export function Thrower() {
throwError()
}
import { connection } from 'next/server'
import { Thrower } from './Thrower'
export default async function Page() {
await connection()
return <Thrower />
}
export default function Root({ children }: { children: React.ReactNode }) { export const runtime = 'edge'
export default function Root({ children }) {
return ( return (
<html> <html>
<body>{children}</body> <body>{children}</body>
......
'use client'
function logError() { function logError() {
const error = new Error('Boom') console.error(new Error('Boom'))
console.error(error)
} }
export default function Page() { export default function Page() {
......
import { fn1 } from './fn' function throwError() {
throw new Error('Boom')
}
export default function Page() { export default function Page() {
fn1() throwError()
return <p>hello world</p> return null
} }
'use client'
function throwError() {
throw new Error('Boom')
}
export default function Page() {
throwError()
return null
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
serverSourceMaps: true,
},
}
module.exports = nextConfig
/* eslint-disable jest/no-standalone-expect */
import * as path from 'path'
import { nextTestSetup } from 'e2e-utils'
import stripAnsi from 'strip-ansi'
import { retry } from 'next-test-utils'
function normalizeCliOutput(output: string) {
return stripAnsi(output)
}
describe('app-dir - server source maps edge runtime', () => {
const { skipped, next, isNextDev, isTurbopack } = nextTestSetup({
files: path.join(__dirname, 'fixtures/edge'),
// Deploy tests don't have access to runtime logs.
// Manually verify that the runtime logs match.
skipDeployment: true,
})
if (skipped) return
it('logged errors have a sourcemapped stack with a codeframe', async () => {
const outputIndex = next.cliOutput.length
await next.render('/rsc-error-log')
if (isNextDev) {
await retry(() => {
expect(next.cliOutput.slice(outputIndex)).toContain('Error: Boom')
})
expect(normalizeCliOutput(next.cliOutput.slice(outputIndex))).toContain(
isTurbopack
? // TODO(veil): Error stack should be printed
'\n[Error: Boom]\n'
: // TODO(veil): Error stack should be printed
'\n[Error: Boom]\n'
)
} else {
// TODO: Test `next build` with `--enable-source-maps`.
}
})
it('thrown SSR errors', async () => {
const outputIndex = next.cliOutput.length
await next.render('/ssr-throw')
if (isNextDev) {
await retry(() => {
expect(next.cliOutput.slice(outputIndex)).toContain('Error: Boom')
})
const cliOutput = stripAnsi(next.cliOutput.slice(outputIndex))
expect(cliOutput).toContain(
isTurbopack
? '\n ⨯ Error: Boom' +
'\n at throwError (./app/ssr-throw/page.js:4:9)' +
'\n at Page (./app/ssr-throw/page.js:8:3)' +
'\ndigest: "'
: '\n ⨯ Error: Boom' +
'\n at throwError (./app/ssr-throw/page.js:6:11)' +
'\n at Page (./app/ssr-throw/page.js:9:5)' +
'\ndigest: "'
)
expect(cliOutput).toMatch(/digest: "\d+"/)
} else {
// TODO: Test `next build` with `--enable-source-maps`.
}
})
it('should log the correct values on app-render error', async () => {
const outputIndex = next.cliOutput.length
await next.fetch('/rsc-throw')
if (isNextDev) {
await retry(() => {
expect(next.cliOutput.slice(outputIndex)).toMatch(/Error: Boom/)
})
const cliOutput = stripAnsi(next.cliOutput.slice(outputIndex))
// TODO(veil): Hide Node.js internal stackframes
expect(cliOutput).toContain(
isTurbopack
? '\n ⨯ Error: Boom' +
'\n at throwError (./app/rsc-throw/page.js:2:9)' +
'\n at Page (./app/rsc-throw/page.js:6:3)' +
// TODO(veil): Hide Node.js internal stackframes
'\n at AsyncLocalStorage.run (node:async_hooks:346:14)' +
'\ndigest: "'
: '\n ⨯ Error: Boom' +
'\n at throwError (./app/rsc-throw/page.js:6:11)' +
'\n at Page (./app/rsc-throw/page.js:9:5)' +
// TODO(veil): Hide Node.js internal stackframes
'\n at AsyncLocalStorage.run (node:async_hooks:346:14)' +
'\ndigest: "'
)
expect(cliOutput).toMatch(/digest: "\d+"/)
} else {
// TODO: Test `next build` with `--enable-source-maps`.
}
})
})
...@@ -26,61 +26,67 @@ describe('app-dir - server source maps', () => { ...@@ -26,61 +26,67 @@ describe('app-dir - server source maps', () => {
if (skipped) return if (skipped) return
it('logged errors have a sourcemapped stack with a codeframe', async () => { it('logged errors have a sourcemapped stack with a codeframe', async () => {
const outputIndex = next.cliOutput.length
await next.render('/rsc-error-log') await next.render('/rsc-error-log')
if (isNextDev) { if (isNextDev) {
await retry(() => { await retry(() => {
expect(normalizeCliOutput(next.cliOutput)).toContain( expect(next.cliOutput.slice(outputIndex)).toContain('Error: Boom')
})
expect(normalizeCliOutput(next.cliOutput.slice(outputIndex))).toContain(
'\nError: Boom' + '\nError: Boom' +
'\n at logError (app/rsc-error-log/page.js:2:16)' + '\n at logError (app/rsc-error-log/page.js:4:16)' +
(isTurbopack (isTurbopack
? '\n at Page (app/rsc-error-log/page.js:7:2)' ? '\n at Page (app/rsc-error-log/page.js:11:2)'
: // FIXME: Method name should be "Page" : // TODO(veil): Method name should be "Page"
'\n at logError (app/rsc-error-log/page.js:7:2)') + '\n at logError (app/rsc-error-log/page.js:11:2)') +
'\n 1 | function logError() {' + '\n 2 |' +
"\n> 2 | const error = new Error('Boom')" + '\n 3 | function logError() {' +
"\n> 4 | const error = new Error('Boom')" +
'\n | ^' + '\n | ^' +
'\n 3 | console.error(error)' + '\n 5 | console.error(error)' +
'\n 4 | }' + '\n 6 | }' +
'\n 5 |' + '\n 7 |' +
'\n' '\n'
) )
})
} else { } else {
// TODO: Test `next build` with `--enable-source-maps`. // TODO: Test `next build` with `--enable-source-maps`.
} }
}) })
it('logged errors have a sourcemapped `cause`', async () => { it('logged errors have a sourcemapped `cause`', async () => {
const outputIndex = next.cliOutput.length
await next.render('/rsc-error-log-cause') await next.render('/rsc-error-log-cause')
if (isNextDev) { if (isNextDev) {
await retry(() => { await retry(() => {
expect(next.cliOutput.slice(outputIndex)).toContain('Error: Boom')
})
expect(normalizeCliOutput(next.cliOutput)).toContain( expect(normalizeCliOutput(next.cliOutput)).toContain(
'\nError: Boom' + '\nError: Boom' +
'\n at logError (app/rsc-error-log-cause/page.js:2:16)' + '\n at logError (app/rsc-error-log-cause/page.js:4:16)' +
(isTurbopack (isTurbopack
? '\n at Page (app/rsc-error-log-cause/page.js:8:2)' ? '\n at Page (app/rsc-error-log-cause/page.js:12:2)'
: // FIXME: Method name should be "Page" : // FIXME: Method name should be "Page"
'\n at logError (app/rsc-error-log-cause/page.js:8:2)') + '\n at logError (app/rsc-error-log-cause/page.js:12:2)') +
'\n 1 | function logError(cause) {' + '\n 2 |' +
"\n> 2 | const error = new Error('Boom', { cause })" + '\n 3 | function logError(cause) {' +
"\n> 4 | const error = new Error('Boom', { cause })" +
'\n | ^' + '\n | ^' +
'\n 3 | console.error(error)' + '\n 5 | console.error(error)' +
'\n 4 | }' + '\n 6 | }' +
'\n 5 | {' + '\n 7 | {' +
'\n [cause]: Error: Boom' + '\n [cause]: Error: Boom' +
'\n at Page (app/rsc-error-log-cause/page.js:7:16)' + '\n at Page (app/rsc-error-log-cause/page.js:11:16)' +
'\n 5 |' + '\n 9 | await connection()' +
'\n 6 | export default function Page() {' + '\n 10 |' +
"\n > 7 | const error = new Error('Boom')" + "\n > 11 | const error = new Error('Boom')" +
'\n | ^' + '\n | ^' +
'\n 8 | logError(error)' + '\n 12 | logError(error)' +
'\n 9 | return null' + '\n 13 | return null' +
'\n 10 | }' + '\n 14 | }' +
'\n' '\n'
) )
})
} else { } else {
// TODO: Test `next build` with `--enable-source-maps`. // TODO: Test `next build` with `--enable-source-maps`.
} }
...@@ -91,11 +97,14 @@ describe('app-dir - server source maps', () => { ...@@ -91,11 +97,14 @@ describe('app-dir - server source maps', () => {
;(isTurbopack ? it.skip : it)( ;(isTurbopack ? it.skip : it)(
'stack frames are ignore-listed in ssr', 'stack frames are ignore-listed in ssr',
async () => { async () => {
const outputIndex = next.cliOutput.length
await next.render('/ssr-error-log-ignore-listed') await next.render('/ssr-error-log-ignore-listed')
if (isNextDev) { if (isNextDev) {
await retry(() => { await retry(() => {
expect(normalizeCliOutput(next.cliOutput)).toContain( expect(next.cliOutput.slice(outputIndex)).toContain('Error: Boom')
})
expect(normalizeCliOutput(next.cliOutput.slice(outputIndex))).toContain(
isTurbopack isTurbopack
? // FIXME: Turbopack resolver bug ? // FIXME: Turbopack resolver bug
"Module not found: Can't resolve 'internal-pkg'" "Module not found: Can't resolve 'internal-pkg'"
...@@ -106,7 +115,6 @@ describe('app-dir - server source maps', () => { ...@@ -106,7 +115,6 @@ describe('app-dir - server source maps', () => {
'\n at Page (app/ssr-error-log-ignore-listed/page.js:10:6)' + '\n at Page (app/ssr-error-log-ignore-listed/page.js:10:6)' +
'\n 3 |' '\n 3 |'
) )
})
} else { } else {
// TODO: Test `next build` with `--enable-source-maps`. // TODO: Test `next build` with `--enable-source-maps`.
} }
...@@ -118,36 +126,71 @@ describe('app-dir - server source maps', () => { ...@@ -118,36 +126,71 @@ describe('app-dir - server source maps', () => {
;(isTurbopack ? it.skip : it)( ;(isTurbopack ? it.skip : it)(
'stack frames are ignore-listed in rsc', 'stack frames are ignore-listed in rsc',
async () => { async () => {
const outputIndex = next.cliOutput.length
await next.render('/rsc-error-log-ignore-listed') await next.render('/rsc-error-log-ignore-listed')
if (isNextDev) { if (isNextDev) {
await retry(() => { await retry(() => {
expect(normalizeCliOutput(next.cliOutput)).toContain( expect(
normalizeCliOutput(next.cliOutput.slice(outputIndex))
).toContain('Error: Boom')
})
expect(normalizeCliOutput(next.cliOutput.slice(outputIndex))).toContain(
isTurbopack isTurbopack
? // FIXME: Turbopack resolver bug ? // FIXME: Turbopack resolver bug
"Module not found: Can't resolve 'internal-pkg'" "Module not found: Can't resolve 'internal-pkg'"
: '\nError: Boom' + : '\nError: Boom' +
'\n at logError (app/rsc-error-log-ignore-listed/page.js:4:16)' + '\n at logError (app/rsc-error-log-ignore-listed/page.js:5:16)' +
// FIXME: Method name should be "Page" // FIXME: Method name should be "Page"
'\n at logError (app/rsc-error-log-ignore-listed/page.js:9:12)' + '\n at logError (app/rsc-error-log-ignore-listed/page.js:12:12)' +
'\n at Page (app/rsc-error-log-ignore-listed/page.js:9:6)' + '\n at Page (app/rsc-error-log-ignore-listed/page.js:12:6)' +
'\n 2 |' '\n 3 |'
) )
})
} else { } else {
// TODO: Test `next build` with `--enable-source-maps`. // TODO: Test `next build` with `--enable-source-maps`.
} }
} }
) )
it('thrown SSR errors', async () => {
const outputIndex = next.cliOutput.length
await next.render('/ssr-throw')
if (isNextDev) {
await retry(() => {
expect(next.cliOutput.slice(outputIndex)).toContain('Error: Boom')
})
const cliOutput = stripAnsi(next.cliOutput.slice(outputIndex))
expect(cliOutput).toContain(
isTurbopack
? '\n ⨯ Error: Boom' +
'\n at throwError (./app/ssr-throw/Thrower.js:4:9)' +
'\n at Thrower (./app/ssr-throw/Thrower.js:8:3)' +
'\ndigest: "'
: '\n ⨯ Error: Boom' +
'\n at throwError (./app/ssr-throw/Thrower.js:6:11)' +
'\n at Thrower (./app/ssr-throw/Thrower.js:9:5)' +
'\ndigest: "'
)
expect(cliOutput).toMatch(/digest: "\d+"/)
} else {
// TODO: Test `next build` with `--enable-source-maps`.
}
})
it('logged errors preserve their name', async () => { it('logged errors preserve their name', async () => {
const outputIndex = next.cliOutput.length
await next.render('/rsc-error-log-custom-name') await next.render('/rsc-error-log-custom-name')
expect(next.cliOutput).toContain( await retry(() => {
expect(next.cliOutput.slice(outputIndex)).toContain(
// TODO: isNextDev ? 'UnnamedError: Foo' : '[Error]: Foo' // TODO: isNextDev ? 'UnnamedError: Foo' : '[Error]: Foo'
isNextDev ? 'Error: Foo' : 'Error: Foo' isNextDev ? 'Error: Foo' : 'Error: Foo'
) )
expect(next.cliOutput).toContain( })
expect(next.cliOutput.slice(outputIndex)).toContain(
// TODO: isNextDev ? 'NamedError [MyError]: Bar' : '[MyError]: Bar' // TODO: isNextDev ? 'NamedError [MyError]: Bar' : '[MyError]: Bar'
isNextDev ? 'Error [MyError]: Bar' : 'Error [MyError]: Bar' isNextDev ? 'Error [MyError]: Bar' : 'Error [MyError]: Bar'
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment