Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 30 additions & 32 deletions tests/e2e/after.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,44 @@ import { expect } from '@playwright/test'
import { nextVersionSatisfies } from '../utils/next-version-helpers.mjs'
import { test } from '../utils/playwright-helpers.js'

test('next/after callback is executed and finishes', async ({ page, after }) => {
test.skip(
!nextVersionSatisfies('>=15.1.0'),
'This test is only for Next.js >=15.1.0 that has stable after() support',
)
// This test is only for Next.js >=15.1.0 that has stable after() support
if (nextVersionSatisfies('>=15.1.0')) {
test('next/after callback is executed and finishes', async ({ page, after }) => {
// trigger initial request to check page which might be stale and allow regenerating in background
await page.goto(`${after.url}/after/check`)

// trigger initial request to check page which might be stale and allow regenerating in background
await page.goto(`${after.url}/after/check`)
await new Promise((resolve) => setTimeout(resolve, 5000))

await new Promise((resolve) => setTimeout(resolve, 5000))
// after it was possibly regenerated we can start checking actual content of the page
await page.goto(`${after.url}/after/check`)
const pageInfoLocator1 = await page.locator('#page-info')
const pageInfo1 = JSON.parse((await pageInfoLocator1.textContent()) ?? '{}')

// after it was possibly regenerated we can start checking actual content of the page
await page.goto(`${after.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')

expect(typeof pageInfo1?.timestamp, 'Check page should have timestamp').toBe('number')
await page.goto(`${after.url}/after/check`)
const pageInfoLocator2 = await page.locator('#page-info')
const pageInfo2 = JSON.parse((await pageInfoLocator2.textContent()) ?? '{}')

await page.goto(`${after.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(typeof pageInfo2?.timestamp, 'Check page should have timestamp').toBe('number')
expect(pageInfo2.timestamp, 'Check page should be cached').toBe(pageInfo1.timestamp)

expect(pageInfo2.timestamp, 'Check page should be cached').toBe(pageInfo1.timestamp)
const response = await page.goto(`${after.url}/after/trigger`)

const response = await page.goto(`${after.url}/after/trigger`)
expect(response?.status(), 'Trigger should return 200').toBe(200)

expect(response?.status(), 'Trigger should return 200').toBe(200)
// wait for next/after to trigger revalidation of check page
await new Promise((resolve) => setTimeout(resolve, 5000))

// wait for next/after to trigger revalidation of check page
await new Promise((resolve) => setTimeout(resolve, 5000))
await page.goto(`${after.url}/after/check`)
const pageInfoLocator3 = await page.locator('#page-info')
const pageInfo3 = JSON.parse((await pageInfoLocator3.textContent()) ?? '{}')

await page.goto(`${after.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)
})
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)
})
}
3 changes: 1 addition & 2 deletions tests/e2e/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,12 @@ for (const { expectedRuntime, isNodeMiddleware, label, testWithSwitchableMiddlew
expect(await res?.headerValue('x-runtime')).toEqual(expectedRuntime)
})

if (expectedRuntime !== 'node') {
if (expectedRuntime !== 'node' && nextVersionSatisfies('>=14.0.0')) {
// adaptation of https://github.com/vercel/next.js/blob/8aa9a52c36f338320d55bd2ec292ffb0b8c7cb35/test/e2e/app-dir/metadata-edge/index.test.ts#L24C5-L31C7
test('it should render OpenGraph image meta tag correctly', async ({
page,
middlewareOg,
}) => {
test.skip(!nextVersionSatisfies('>=14.0.0'), 'This test is only for Next.js 14+')
await page.goto(`${middlewareOg.url}/`)
const ogURL = await page.locator('meta[property="og:image"]').getAttribute('content')
expect(ogURL).toBeTruthy()
Expand Down
16 changes: 10 additions & 6 deletions tests/utils/next-version-helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export async function setNextVersionInFixture(

const isSemverVersion = valid(resolvedVersion)

const areNextVersionConstraintsSatisfied = await Promise.all(
const packageJsonsNeedUpdates = await Promise.all(
packageJsons.map(async (packageJsonPath) => {
const packageJson = JSON.parse(await readFile(packageJsonPath, 'utf8'))
if (packageJson.dependencies?.next) {
Expand All @@ -130,15 +130,19 @@ export async function setNextVersionInFixture(
`${logPrefix}⏩ Skipping '${packageJson.name}' because it requires next@${versionConstraint}`,
)
}
return false
return { packageJsonPath, needUpdate: false }
}
}
return true
return { packageJsonPath, needUpdate: true }
}),
)

if (areNextVersionConstraintsSatisfied.some((isSatisfied) => !isSatisfied)) {
// at least one next version constraint is not satisfied so we skip this fixture
const packageJsonsToUpdate = packageJsonsNeedUpdates
.filter(({ needUpdate }) => needUpdate)
.map(({ packageJsonPath }) => packageJsonPath)

if (packageJsonsToUpdate.length === 0) {
// all next version constraints are not satisfied so we skip this fixture
return false
}

Expand All @@ -154,7 +158,7 @@ export async function setNextVersionInFixture(
}

await Promise.all(
packageJsons.map(async (packageJsonPath) => {
packageJsonsToUpdate.map(async (packageJsonPath) => {
const packageJson = JSON.parse(await readFile(packageJsonPath, 'utf8'))
if (packageJson.dependencies?.next) {
packageJson.dependencies.next = version
Expand Down
Loading