Skip to content

Commit

Permalink
add test case for persistent caching
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Dec 9, 2024
1 parent 928c217 commit 339e7dc
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 0 deletions.
8 changes: 8 additions & 0 deletions test/e2e/persistent-caching/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
8 changes: 8 additions & 0 deletions test/e2e/persistent-caching/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function Page() {
return (
<>
<p>hello world</p>
<main>Timestamp</main>
</>
)
}
5 changes: 5 additions & 0 deletions test/e2e/persistent-caching/my-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = async function myLoader(source) {
// Make webpack consider the build as large change which makes it persistent cache it sooner
await new Promise((resolve) => setTimeout(resolve, 2000))
return source.replace(/Timestamp/g, `Timestamp = ${Date.now()}`)
}
28 changes: 28 additions & 0 deletions test/e2e/persistent-caching/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
turbo: {
unstablePersistentCaching: true,
rules: {
'app/page.tsx': {
loaders: ['./my-loader.js'],
},
'pages/pages.tsx': {
loaders: ['./my-loader.js'],
},
},
},
},
webpack(config) {
config.module.rules.push({
test: /app\/page\.tsx|pages\/pages.tsx/,
use: ['./my-loader.js'],
})

return config
},
}

module.exports = nextConfig
8 changes: 8 additions & 0 deletions test/e2e/persistent-caching/pages/pages.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function Page() {
return (
<>
<p>hello world</p>
<main>Timestamp</main>
</>
)
}
114 changes: 114 additions & 0 deletions test/e2e/persistent-caching/persistent-caching.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { nextTestSetup } from 'e2e-utils'
import { waitFor } from 'next-test-utils'

describe('persistent-caching', () => {
const { skipped, next, isNextDev, isTurbopack } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})

if (skipped) {
return
}

async function restartCycle() {
await stop()
await start()
}

async function stop() {
if (isNextDev) {
// Give Persistent Cache time to write to disk
await waitFor(10000)
}
await next.stop()
}

async function start() {
if (!isNextDev) {
await next.build()
}
await next.start()
}

it('should persistent cache loaders', async () => {
let appTimestamp, pagesTimestamp
{
const browser = await next.browser('/')
appTimestamp = await browser.elementByCss('main').text()
browser.close()
}
{
const browser = await next.browser('/pages')
pagesTimestamp = await browser.elementByCss('main').text()
browser.close()
}
await restartCycle()

{
const browser = await next.browser('/')
// TODO Persistent Caching for webpack dev server is broken
if (!isNextDev || isTurbopack) {
expect(await browser.elementByCss('main').text()).toBe(appTimestamp)
}
browser.close()
}
{
const browser = await next.browser('/pages')
// TODO Persistent Caching for webpack dev server is broken
if (!isNextDev || isTurbopack) {
expect(await browser.elementByCss('main').text()).toBe(pagesTimestamp)
}
browser.close()
}
})

it('should allow to change files while stopped', async () => {
{
const browser = await next.browser('/')
expect(await browser.elementByCss('p').text()).toBe('hello world')
browser.close()
}
{
const browser = await next.browser('/pages')
expect(await browser.elementByCss('p').text()).toBe('hello world')
browser.close()
}

await stop()

await next.patchFile(
'pages/pages.tsx',
(content) => {
return content.replace('hello world', 'hello persistent caching')
},
async () => {
await next.patchFile(
'app/page.tsx',
(content) => {
return content.replace('hello world', 'hello persistent caching')
},
async () => {
await start()
{
const browser = await next.browser('/')
expect(await browser.elementByCss('p').text()).toBe(
'hello persistent caching'
)
browser.close()
}
{
const browser = await next.browser('/pages')
expect(await browser.elementByCss('p').text()).toBe(
'hello persistent caching'
)
browser.close()
}
await stop()
}
)
}
)
await start()
})
})

0 comments on commit 339e7dc

Please sign in to comment.