Skip to content

Commit 280d8a8

Browse files
committed
test: e2e simple next/after test
1 parent 00e3a4b commit 280d8a8

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

Diff for: tests/e2e/simple-app.test.ts

+33
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,36 @@ test('can require CJS module that is not bundled', async ({ simple }) => {
272272
expect(parsedBody.notBundledCJSModule.isBundled).toEqual(false)
273273
expect(parsedBody.bundledCJSModule.isBundled).toEqual(true)
274274
})
275+
276+
test('next/after callback is executed and finishes', async ({ page, simple }) => {
277+
test.skip(!nextVersionSatisfies('>=15.0.0'), 'This test is only for Next.js 15+')
278+
279+
await page.goto(`${simple.url}/after/check`)
280+
const pageInfoLocator1 = await page.locator('#page-info')
281+
const pageInfo1 = JSON.parse((await pageInfoLocator1.textContent()) ?? '{}')
282+
283+
expect(typeof pageInfo1?.timestamp, 'Check page should have timestamp').toBe('number')
284+
285+
await new Promise((resolve) => setTimeout(resolve, 2000))
286+
287+
await page.goto(`${simple.url}/after/check`)
288+
const pageInfoLocator2 = await page.locator('#page-info')
289+
const pageInfo2 = JSON.parse((await pageInfoLocator2.textContent()) ?? '{}')
290+
291+
expect(typeof pageInfo2?.timestamp, 'Check page should have timestamp').toBe('number')
292+
293+
expect(pageInfo2.timestamp, 'Check page should be cached').toBe(pageInfo1.timestamp)
294+
295+
296+
await page.goto(`${simple.url}/after/trigger`)
297+
298+
// wait for next/after to trigger revalidation of check page
299+
await new Promise((resolve) => setTimeout(resolve, 5000))
300+
301+
await page.goto(`${simple.url}/after/check`)
302+
const pageInfoLocator3 = await page.locator('#page-info')
303+
const pageInfo3 = JSON.parse((await pageInfoLocator3.textContent()) ?? '{}')
304+
305+
expect(typeof pageInfo3?.timestamp, 'Check page should have timestamp').toBe('number')
306+
expect(pageInfo3.timestamp, 'Check page should be invalidated with newer timestamp').toBeGreaterThan(pageInfo1.timestamp)
307+
})

Diff for: tests/fixtures/simple/app/after/check/page.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const revalidate = 3600 // arbitrarily long, just so that it doesn't happen during a test run
2+
3+
4+
export default async function Page() {
5+
const data = {
6+
timestamp: Date.now(),
7+
}
8+
console.log('/timestamp/key/[key] rendered', data)
9+
10+
return (
11+
<div id="page-info">{JSON.stringify(data)}</div>
12+
)
13+
}

Diff for: tests/fixtures/simple/app/after/trigger/page.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { revalidatePath } from 'next/cache'
2+
import { unstable_after as after, connection } from 'next/server'
3+
4+
export default async function Page() {
5+
await connection()
6+
after(async () => {
7+
// this will run after response was sent
8+
console.log('after() triggered')
9+
console.log('after() sleep 1s')
10+
await new Promise((resolve) => setTimeout(resolve, 1000))
11+
12+
console.log('after() revalidatePath /after/check')
13+
revalidatePath('/after/check')
14+
})
15+
16+
return <div>Page with after()</div>
17+
}

Diff for: tests/fixtures/simple/next.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const nextConfig = {
88
outputFileTracingIncludes: {
99
'/': ['public/**'],
1010
},
11+
after: true
1112
},
1213
images: {
1314
remotePatterns: [

0 commit comments

Comments
 (0)