From 339e7dc13f8bb092705566459f47761c87079887 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Mon, 9 Dec 2024 11:06:27 +0100 Subject: [PATCH] add test case for persistent caching --- test/e2e/persistent-caching/app/layout.tsx | 8 ++ test/e2e/persistent-caching/app/page.tsx | 8 ++ test/e2e/persistent-caching/my-loader.js | 5 + test/e2e/persistent-caching/next.config.js | 28 +++++ test/e2e/persistent-caching/pages/pages.tsx | 8 ++ .../persistent-caching.test.ts | 114 ++++++++++++++++++ 6 files changed, 171 insertions(+) create mode 100644 test/e2e/persistent-caching/app/layout.tsx create mode 100644 test/e2e/persistent-caching/app/page.tsx create mode 100644 test/e2e/persistent-caching/my-loader.js create mode 100644 test/e2e/persistent-caching/next.config.js create mode 100644 test/e2e/persistent-caching/pages/pages.tsx create mode 100644 test/e2e/persistent-caching/persistent-caching.test.ts diff --git a/test/e2e/persistent-caching/app/layout.tsx b/test/e2e/persistent-caching/app/layout.tsx new file mode 100644 index 0000000000000..888614deda3ba --- /dev/null +++ b/test/e2e/persistent-caching/app/layout.tsx @@ -0,0 +1,8 @@ +import { ReactNode } from 'react' +export default function Root({ children }: { children: ReactNode }) { + return ( + + {children} + + ) +} diff --git a/test/e2e/persistent-caching/app/page.tsx b/test/e2e/persistent-caching/app/page.tsx new file mode 100644 index 0000000000000..9618e78238f7b --- /dev/null +++ b/test/e2e/persistent-caching/app/page.tsx @@ -0,0 +1,8 @@ +export default function Page() { + return ( + <> +

hello world

+
Timestamp
+ + ) +} diff --git a/test/e2e/persistent-caching/my-loader.js b/test/e2e/persistent-caching/my-loader.js new file mode 100644 index 0000000000000..32179b8c36262 --- /dev/null +++ b/test/e2e/persistent-caching/my-loader.js @@ -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()}`) +} diff --git a/test/e2e/persistent-caching/next.config.js b/test/e2e/persistent-caching/next.config.js new file mode 100644 index 0000000000000..85d7aadbe8482 --- /dev/null +++ b/test/e2e/persistent-caching/next.config.js @@ -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 diff --git a/test/e2e/persistent-caching/pages/pages.tsx b/test/e2e/persistent-caching/pages/pages.tsx new file mode 100644 index 0000000000000..9618e78238f7b --- /dev/null +++ b/test/e2e/persistent-caching/pages/pages.tsx @@ -0,0 +1,8 @@ +export default function Page() { + return ( + <> +

hello world

+
Timestamp
+ + ) +} diff --git a/test/e2e/persistent-caching/persistent-caching.test.ts b/test/e2e/persistent-caching/persistent-caching.test.ts new file mode 100644 index 0000000000000..9e4003d0e92b5 --- /dev/null +++ b/test/e2e/persistent-caching/persistent-caching.test.ts @@ -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() + }) +})