Skip to content

Commit 3cb89a5

Browse files
committed
[test] Improve coverage for fetch errors in Edge runtime
1 parent bb9556a commit 3cb89a5

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

test/e2e/fetch-failures-have-good-stack-traces-in-edge-runtime/fetch-failures-have-good-stack-traces-in-edge-runtime.test.ts

+37-18
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import { nextTestSetup } from 'e2e-utils'
22
import webdriver from 'next-webdriver'
3-
import {
4-
assertHasRedbox,
5-
getRedboxSource,
6-
getRedboxDescription,
7-
check,
8-
} from 'next-test-utils'
3+
import { check } from 'next-test-utils'
94
import stripAnsi from 'strip-ansi'
105

116
describe('fetch failures have good stack traces in edge runtime', () => {
12-
const { next, isNextStart, isNextDev, skipped } = nextTestSetup({
7+
const { isTurbopack, next, isNextStart, isNextDev, skipped } = nextTestSetup({
138
files: __dirname,
149
// don't have access to runtime logs on deploy
1510
skipDeployment: true,
@@ -20,22 +15,46 @@ describe('fetch failures have good stack traces in edge runtime', () => {
2015
}
2116

2217
it('when awaiting `fetch` using an unknown domain, stack traces are preserved', async () => {
18+
const outputIndex = next.cliOutput.length
2319
const browser = await webdriver(next.url, '/api/unknown-domain')
2420

2521
if (isNextStart) {
26-
expect(next.cliOutput).toMatch(/at.+\/pages\/api\/unknown-domain.js/)
22+
expect(next.cliOutput.slice(outputIndex)).toMatch(
23+
/at.+\/pages\/api\/unknown-domain.js/
24+
)
2725
} else if (isNextDev) {
28-
// TODO(veil): Apply sourcemap
29-
expect(next.cliOutput).toContain('\n at anotherFetcher (')
26+
expect(stripAnsi(next.cliOutput.slice(outputIndex))).toContain(
27+
'' +
28+
'\n ⨯ Error [TypeError]: fetch failed' +
29+
'\n at anotherFetcher (src/fetcher.js:6:15)' +
30+
'\n at fetcher (src/fetcher.js:2:15)' +
31+
'\n at UnknownDomainEndpoint (pages/api/unknown-domain.js:6:16)' +
32+
'\n 4 |' +
33+
'\n 5 | async function anotherFetcher(...args) {' +
34+
'\n> 6 | return await fetch(...args)' +
35+
'\n | ^' +
36+
'\n 7 | }' +
37+
'\n 8 |' +
38+
// TODO(veil): Why double error?
39+
'\n ⨯ Error [TypeError]: fetch failed'
40+
)
3041

31-
await assertHasRedbox(browser)
32-
const source = await getRedboxSource(browser)
33-
34-
expect(source).toContain('async function anotherFetcher(...args)')
35-
expect(source).toContain(`fetch(...args)`)
36-
37-
const description = await getRedboxDescription(browser)
38-
expect(description).toEqual('TypeError: fetch failed')
42+
// TODO(veil): Why column off by one?
43+
await expect(browser).toDisplayRedbox(`
44+
{
45+
"description": "TypeError: fetch failed",
46+
"environmentLabel": null,
47+
"label": "Runtime Error",
48+
"source": "src/fetcher.js (6:16) @ anotherFetcher
49+
> 6 | return await fetch(...args)
50+
| ^",
51+
"stack": [
52+
"anotherFetcher src/fetcher.js (6:16)",
53+
"fetcher src/fetcher.js (2:16)",
54+
"UnknownDomainEndpoint pages/api/unknown-domain.js (6:${isTurbopack ? 15 : 16})",
55+
],
56+
}
57+
`)
3958
}
4059
})
4160

test/e2e/fetch-failures-have-good-stack-traces-in-edge-runtime/pages/api/unknown-domain-no-await.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export const config = { runtime: 'edge' }
22

33
export default async function UnknownDomainEndpoint() {
4-
fetch('http://an.unknown.domain.nextjs').catch((err) => {
5-
console.error(`stack is:`, err.stack)
4+
fetch('http://an.unknown.domain.nextjs').catch((reason) => {
5+
console.error(reason)
66
})
77

88
return new Response('okay.')

0 commit comments

Comments
 (0)