Skip to content

Commit 244f047

Browse files
committed
add test
1 parent e9840d1 commit 244f047

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

test/production/chunk-load-failure/app/dynamic/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import dynamic from 'next/dynamic'
44
import { Suspense } from 'react'
55

6-
const Async = dynamic(() => import('./async'))
6+
const Async = dynamic(() => import('./async'), { ssr: false })
77

88
export default function Page() {
99
return (
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use client'
2+
3+
export default function Page() {
4+
return <>this is other</>
5+
}

test/production/chunk-load-failure/chunk-load-failure.test.ts

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('chunk-load-failure', () => {
99
files: __dirname,
1010
})
1111

12-
it('should report async chunk load failures', async () => {
12+
async function getNextDynamicChunk() {
1313
const chunksPath = path.join(next.testDir, '.next/static/')
1414
const browserChunks = await recursiveReadDir(chunksPath, {
1515
pathnameFilter: (f) => /\.js$/.test(f),
@@ -20,7 +20,12 @@ describe('chunk-load-failure', () => {
2020
.includes('this is a lazy loaded async component')
2121
)
2222
expect(nextDynamicChunks).toHaveLength(1)
23-
let nextDynamicChunk = nextDynamicChunks[0]
23+
24+
return nextDynamicChunks[0]
25+
}
26+
27+
it('should report async chunk load failures', async () => {
28+
let nextDynamicChunk = await getNextDynamicChunk()
2429

2530
let pageError: Error | undefined
2631
const browser = await next.browser('/dynamic', {
@@ -52,4 +57,51 @@ describe('chunk-load-failure', () => {
5257
expect(pageError.message).toContain('/_next/static/' + nextDynamicChunk)
5358
}
5459
})
60+
61+
it.each(['chrome', 'firefox'])(
62+
'should report aborted chunks when navigating away with %s',
63+
async (browserName) => {
64+
let nextDynamicChunk = await getNextDynamicChunk()
65+
66+
let resolve
67+
let oldBrowserName = process.env.BROWSER_NAME
68+
process.env.BROWSER_NAME = browserName
69+
try {
70+
const browser = await next.browser('/dynamic', {
71+
beforePageLoad(page) {
72+
page.route('**/' + nextDynamicChunk, async (route) => {
73+
await new Promise((r) => {
74+
resolve = r
75+
})
76+
})
77+
},
78+
})
79+
80+
await browser.get(next.url + '/other')
81+
82+
let body = await browser.elementByCss('body')
83+
expect(await body.text()).toMatch('this is other')
84+
85+
const browserLogs = (await browser.log()).filter(
86+
(m) => m.source === 'warning' || m.source === 'error'
87+
)
88+
if (browserName === 'firefox') {
89+
expect(browserLogs).toContainEqual(
90+
expect.objectContaining({
91+
message: expect.stringContaining(
92+
'Loading failed for the <script> with source'
93+
),
94+
})
95+
)
96+
} else {
97+
// Chrome doesn't show any errors or warnings here
98+
expect(browserLogs).toBeEmpty()
99+
}
100+
} finally {
101+
process.env.BROWSER_NAME = oldBrowserName
102+
// prevent hanging
103+
resolve?.()
104+
}
105+
}
106+
)
55107
})

0 commit comments

Comments
 (0)