Skip to content

Commit

Permalink
test: e2e simple next/after test
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Dec 6, 2024
1 parent 00e3a4b commit 280d8a8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tests/e2e/simple-app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,36 @@ test('can require CJS module that is not bundled', async ({ simple }) => {
expect(parsedBody.notBundledCJSModule.isBundled).toEqual(false)
expect(parsedBody.bundledCJSModule.isBundled).toEqual(true)
})

test('next/after callback is executed and finishes', async ({ page, simple }) => {
test.skip(!nextVersionSatisfies('>=15.0.0'), 'This test is only for Next.js 15+')

await page.goto(`${simple.url}/after/check`)
const pageInfoLocator1 = await page.locator('#page-info')
const pageInfo1 = JSON.parse((await pageInfoLocator1.textContent()) ?? '{}')

expect(typeof pageInfo1?.timestamp, 'Check page should have timestamp').toBe('number')

await new Promise((resolve) => setTimeout(resolve, 2000))

await page.goto(`${simple.url}/after/check`)
const pageInfoLocator2 = await page.locator('#page-info')
const pageInfo2 = JSON.parse((await pageInfoLocator2.textContent()) ?? '{}')

expect(typeof pageInfo2?.timestamp, 'Check page should have timestamp').toBe('number')

expect(pageInfo2.timestamp, 'Check page should be cached').toBe(pageInfo1.timestamp)


await page.goto(`${simple.url}/after/trigger`)

// wait for next/after to trigger revalidation of check page
await new Promise((resolve) => setTimeout(resolve, 5000))

await page.goto(`${simple.url}/after/check`)
const pageInfoLocator3 = await page.locator('#page-info')
const pageInfo3 = JSON.parse((await pageInfoLocator3.textContent()) ?? '{}')

expect(typeof pageInfo3?.timestamp, 'Check page should have timestamp').toBe('number')
expect(pageInfo3.timestamp, 'Check page should be invalidated with newer timestamp').toBeGreaterThan(pageInfo1.timestamp)
})
13 changes: 13 additions & 0 deletions tests/fixtures/simple/app/after/check/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const revalidate = 3600 // arbitrarily long, just so that it doesn't happen during a test run


export default async function Page() {
const data = {
timestamp: Date.now(),
}
console.log('/timestamp/key/[key] rendered', data)

return (
<div id="page-info">{JSON.stringify(data)}</div>
)
}
17 changes: 17 additions & 0 deletions tests/fixtures/simple/app/after/trigger/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { revalidatePath } from 'next/cache'
import { unstable_after as after, connection } from 'next/server'

export default async function Page() {
await connection()
after(async () => {
// this will run after response was sent
console.log('after() triggered')
console.log('after() sleep 1s')
await new Promise((resolve) => setTimeout(resolve, 1000))

console.log('after() revalidatePath /after/check')
revalidatePath('/after/check')
})

return <div>Page with after()</div>
}
1 change: 1 addition & 0 deletions tests/fixtures/simple/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const nextConfig = {
outputFileTracingIncludes: {
'/': ['public/**'],
},
after: true
},
images: {
remotePatterns: [
Expand Down

0 comments on commit 280d8a8

Please sign in to comment.