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

[test] Improve coverage for fetch errors in Edge runtime (#78257)

parent 605126ec
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { import { check } from 'next-test-utils'
assertHasRedbox,
getRedboxSource,
getRedboxDescription,
check,
} from 'next-test-utils'
import stripAnsi from 'strip-ansi' import stripAnsi from 'strip-ansi'
describe('fetch failures have good stack traces in edge runtime', () => { describe('fetch failures have good stack traces in edge runtime', () => {
const { next, isNextStart, isNextDev, skipped } = nextTestSetup({ const { isTurbopack, next, isNextStart, isNextDev, skipped } = nextTestSetup({
files: __dirname, files: __dirname,
// don't have access to runtime logs on deploy // don't have access to runtime logs on deploy
skipDeployment: true, skipDeployment: true,
...@@ -20,22 +15,46 @@ describe('fetch failures have good stack traces in edge runtime', () => { ...@@ -20,22 +15,46 @@ describe('fetch failures have good stack traces in edge runtime', () => {
} }
it('when awaiting `fetch` using an unknown domain, stack traces are preserved', async () => { it('when awaiting `fetch` using an unknown domain, stack traces are preserved', async () => {
const outputIndex = next.cliOutput.length
const browser = await webdriver(next.url, '/api/unknown-domain') const browser = await webdriver(next.url, '/api/unknown-domain')
if (isNextStart) { if (isNextStart) {
expect(next.cliOutput).toMatch(/at.+\/pages\/api\/unknown-domain.js/) expect(next.cliOutput.slice(outputIndex)).toMatch(
/at.+\/pages\/api\/unknown-domain.js/
)
} else if (isNextDev) { } else if (isNextDev) {
// TODO(veil): Apply sourcemap expect(stripAnsi(next.cliOutput.slice(outputIndex))).toContain(
expect(next.cliOutput).toContain('\n at anotherFetcher (') '' +
'\n ⨯ Error [TypeError]: fetch failed' +
'\n at anotherFetcher (src/fetcher.js:6:15)' +
'\n at fetcher (src/fetcher.js:2:15)' +
'\n at UnknownDomainEndpoint (pages/api/unknown-domain.js:6:16)' +
'\n 4 |' +
'\n 5 | async function anotherFetcher(...args) {' +
'\n> 6 | return await fetch(...args)' +
'\n | ^' +
'\n 7 | }' +
'\n 8 |' +
// TODO(veil): Why double error?
'\n ⨯ Error [TypeError]: fetch failed'
)
await assertHasRedbox(browser) // TODO(veil): Why column off by one?
const source = await getRedboxSource(browser) await expect(browser).toDisplayRedbox(`
{
expect(source).toContain('async function anotherFetcher(...args)') "description": "TypeError: fetch failed",
expect(source).toContain(`fetch(...args)`) "environmentLabel": null,
"label": "Runtime Error",
const description = await getRedboxDescription(browser) "source": "src/fetcher.js (6:16) @ anotherFetcher
expect(description).toEqual('TypeError: fetch failed') > 6 | return await fetch(...args)
| ^",
"stack": [
"anotherFetcher src/fetcher.js (6:16)",
"fetcher src/fetcher.js (2:16)",
"UnknownDomainEndpoint pages/api/unknown-domain.js (6:${isTurbopack ? 15 : 16})",
],
}
`)
} }
}) })
......
export const config = { runtime: 'edge' } export const config = { runtime: 'edge' }
export default async function UnknownDomainEndpoint() { export default async function UnknownDomainEndpoint() {
fetch('http://an.unknown.domain.nextjs').catch((err) => { fetch('http://an.unknown.domain.nextjs').catch((reason) => {
console.error(`stack is:`, err.stack) console.error(reason)
}) })
return new Response('okay.') return new Response('okay.')
......
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