Files
next.js/test/e2e/app-dir/use-cache-custom-handler/use-cache-custom-handler.test.ts
Arian Tron 61f56f997c
Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled
first commit
2026-03-10 19:37:31 +03:30

134 lines
4.5 KiB
TypeScript

import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
const isoDateRegExp = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/
describe('use-cache-custom-handler', () => {
const { next, skipped, isNextStart } = nextTestSetup({
files: __dirname,
// Skip deployment so we can test the custom cache handlers log output
skipDeployment: true,
})
if (skipped) return
let outputIndex: number
beforeEach(() => {
outputIndex = next.cliOutput.length
})
it('should use a modern custom cache handler if provided', async () => {
const browser = await next.browser(`/`)
const initialData = await browser.elementById('data').text()
expect(initialData).toMatch(isoDateRegExp)
const cliOutput = next.cliOutput.slice(outputIndex)
expect(cliOutput).toContain('ModernCustomCacheHandler::refreshTags')
expect(next.cliOutput.slice(outputIndex)).toMatch(
/ModernCustomCacheHandler::get \["(development|[A-Za-z0-9_-]+)","([0-9a-f]{2})+",\[\]\] \[ '_N_T_\/layout', '_N_T_\/page', '_N_T_\/', '_N_T_\/index' \]/
)
expect(next.cliOutput.slice(outputIndex)).toMatch(
/ModernCustomCacheHandler::set \["(development|[A-Za-z0-9_-]+)","([0-9a-f]{2})+",\[\]\]/
)
// Since no existing cache entry was retrieved, we don't need to call
// getExpiration() to compare the cache entries timestamp with the
// expiration of the implicit tags.
expect(cliOutput).not.toContain(`ModernCustomCacheHandler::getExpiration`)
// The data should be cached initially.
outputIndex = next.cliOutput.length
await browser.refresh()
let data = await browser.elementById('data').text()
expect(data).toMatch(isoDateRegExp)
expect(data).toEqual(initialData)
// Now that a cache entry exists, we expect that getExpiration() is called
// to compare the cache entries timestamp with the expiration of the
// implicit tags.
expect(next.cliOutput.slice(outputIndex)).toContain(
`ModernCustomCacheHandler::getExpiration ["_N_T_/layout","_N_T_/page","_N_T_/","_N_T_/index"]`
)
// Because we use a low `revalidate` value for the "use cache" function, new
// data should be returned eventually.
await retry(
async () => {
await browser.refresh()
data = await browser.elementById('data').text()
expect(data).toMatch(isoDateRegExp)
expect(data).not.toEqual(initialData)
},
10_000,
2_000
)
})
it('calls neither refreshTags nor getExpiration if "use cache" is not used', async () => {
await next.fetch(`/no-cache`)
const cliOutput = next.cliOutput.slice(outputIndex)
expect(cliOutput).not.toContain('ModernCustomCacheHandler::refreshTags')
expect(cliOutput).not.toContain(`ModernCustomCacheHandler::getExpiration`)
})
it('should revalidate after redirect using a modern custom cache handler', async () => {
const browser = await next.browser(`/`)
const initialData = await browser.elementById('data').text()
expect(initialData).toMatch(isoDateRegExp)
await browser.elementById('revalidate-redirect').click()
await retry(async () => {
expect(next.cliOutput.slice(outputIndex)).toContain(
'ModernCustomCacheHandler::updateTags ["modern"]'
)
const data = await browser.elementById('data').text()
expect(data).toMatch(isoDateRegExp)
expect(data).not.toEqual(initialData)
}, 5000)
})
it('should not call updateTags for a normal invocation', async () => {
await next.fetch(`/`)
await retry(async () => {
const cliOutput = next.cliOutput.slice(outputIndex)
expect(cliOutput).toInclude('ModernCustomCacheHandler::refreshTags')
expect(cliOutput).not.toInclude('ModernCustomCacheHandler::updateTags')
})
})
it('should not call getExpiration after an action', async () => {
const browser = await next.browser(`/`)
outputIndex = next.cliOutput.length
await browser.elementById('revalidate-tag').click()
await retry(async () => {
const cliOutput = next.cliOutput.slice(outputIndex)
expect(cliOutput).not.toInclude('ModernCustomCacheHandler::getExpiration')
expect(cliOutput).toIncludeRepeated(
`ModernCustomCacheHandler::updateTags`,
1
)
})
})
if (isNextStart) {
it('should save a short-lived cache during prerendering at buildtime', async () => {
expect(next.cliOutput).toMatch(
/ModernCustomCacheHandler::set \["[A-Za-z0-9_-]+","([0-9a-f]{2})+",\[{"id":"dynamic-cache"}]\]/
)
})
}
})