File size: 11,253 Bytes
b91e262 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | /* eslint-disable jest/no-standalone-expect */
import { nextTestSetup, isNextDev } from 'e2e-utils'
import { waitFor } from 'next-test-utils'
for (const cacheEnabled of [false, true]) {
describe(`filesystem-caching with cache ${cacheEnabled ? 'enabled' : 'disabled'}`, () => {
beforeAll(() => {
process.env.NEXT_PUBLIC_ENV_VAR = 'hello world'
})
afterAll(() => {
delete process.env.NEXT_PUBLIC_ENV_VAR
})
let envVars = [
`ENABLE_CACHING=${cacheEnabled ? '1' : ''}`,
// Make it easier to run in development, test directories are cleared between runs already so this is safe.
`TURBO_ENGINE_IGNORE_DIRTY=1`,
// decrease the idle timeout to make the test more reliable
`TURBO_ENGINE_SNAPSHOT_IDLE_TIMEOUT_MILLIS=1000`,
].join(' ')
const { skipped, next, isTurbopack } = nextTestSetup({
files: __dirname,
skipDeployment: true,
packageJson: {
scripts: {
build: `${envVars} next build`,
dev: `${envVars} next dev`,
start: 'next start',
},
},
// We need to use npm here as pnpms symlinks trigger a weird bug (kernel bug?)
installCommand: 'npm i',
// Next is always started with caching, but this can disable it for the followup restarts
buildCommand: `npm run build`,
startCommand: isNextDev ? 'npm run dev' : 'npm run start',
})
if (skipped) {
return
}
beforeAll(() => {
// We can skip the dev watch delay since this is not an HMR test
;(next as any).handleDevWatchDelayBeforeChange = () => {}
;(next as any).handleDevWatchDelayAfterChange = () => {}
})
async function restartCycle() {
await stop()
await start()
}
async function stop() {
if (isNextDev) {
// Give FileSystem Cache time to write to disk
// Turbopack is configured to wait 1s above.
// Webpack has an idle timeout (after large changes) of 1s
// and we give time a bit more to allow writing to disk
await waitFor(3000)
}
await next.stop()
}
async function start() {
await next.start()
}
// Very flakey with Webpack enabled
;(process.env.IS_TURBOPACK_TEST ? it : it.skip)(
'should cache or not cache loaders',
async () => {
let appTimestamp, unchangedTimestamp, appClientTimestamp, pagesTimestamp
{
const browser = await next.browser('/')
appTimestamp = await browser.elementByCss('main').text()
expect(appTimestamp).toMatch(/Timestamp = \d+$/)
await browser.close()
}
{
const browser = await next.browser('/unchanged')
unchangedTimestamp = await browser.elementByCss('main').text()
expect(unchangedTimestamp).toMatch(/Timestamp = \d+$/)
await browser.close()
}
{
const browser = await next.browser('/client')
appClientTimestamp = await browser.elementByCss('main').text()
expect(appClientTimestamp).toMatch(/Timestamp = \d+$/)
await browser.close()
}
{
const browser = await next.browser('/pages')
pagesTimestamp = await browser.elementByCss('main').text()
expect(pagesTimestamp).toMatch(/Timestamp = \d+$/)
await browser.close()
}
await restartCycle()
{
const browser = await next.browser('/')
const newTimestamp = await browser.elementByCss('main').text()
expect(newTimestamp).toMatch(/Timestamp = \d+$/)
if (cacheEnabled) {
expect(newTimestamp).toBe(appTimestamp)
} else {
expect(newTimestamp).not.toBe(appTimestamp)
}
await browser.close()
}
{
const browser = await next.browser('/unchanged')
const newTimestamp = await browser.elementByCss('main').text()
expect(newTimestamp).toMatch(/Timestamp = \d+$/)
if (cacheEnabled) {
expect(newTimestamp).toBe(unchangedTimestamp)
} else {
expect(newTimestamp).not.toBe(unchangedTimestamp)
}
await browser.close()
}
{
const browser = await next.browser('/client')
const newTimestamp = await browser.elementByCss('main').text()
expect(newTimestamp).toMatch(/Timestamp = \d+$/)
if (cacheEnabled) {
expect(newTimestamp).toBe(appClientTimestamp)
} else {
expect(newTimestamp).not.toBe(appClientTimestamp)
}
await browser.close()
}
{
const browser = await next.browser('/pages')
const newTimestamp = await browser.elementByCss('main').text()
expect(newTimestamp).toMatch(/Timestamp = \d+$/)
if (cacheEnabled) {
expect(newTimestamp).toBe(pagesTimestamp)
} else {
expect(newTimestamp).not.toBe(pagesTimestamp)
}
await browser.close()
}
}
)
function makeTextCheck(url: string, text: string) {
return textCheck.bind(null, url, text)
}
async function textCheck(url: string, text: string) {
const browser = await next.browser(url)
expect(await browser.elementByCss('p').text()).toBe(text)
await browser.close()
}
function makeFileEdit(file: string) {
return async (inner: () => Promise<void>) => {
await next.patchFile(
file,
(content) => {
return content.replace('hello world', 'hello filesystem cache')
},
inner
)
}
}
interface Change {
checkInitial(): Promise<void>
withChange(previous: () => Promise<void>): Promise<void>
checkChanged(): Promise<void>
fullInvalidation?: boolean
}
const POTENTIAL_CHANGES: Record<string, Change> = {
'RSC change': {
checkInitial: makeTextCheck('/', 'hello world'),
withChange: makeFileEdit('app/page.tsx'),
checkChanged: makeTextCheck('/', 'hello filesystem cache'),
},
'RCC change': {
checkInitial: makeTextCheck('/client', 'hello world'),
withChange: makeFileEdit('app/client/page.tsx'),
checkChanged: makeTextCheck('/client', 'hello filesystem cache'),
},
'Pages change': {
checkInitial: makeTextCheck('/pages', 'hello world'),
withChange: makeFileEdit('pages/pages.tsx'),
checkChanged: makeTextCheck('/pages', 'hello filesystem cache'),
},
'rename app page': {
checkInitial: makeTextCheck('/remove-me', 'hello world'),
async withChange(inner) {
await next.renameFolder('app/remove-me', 'app/add-me')
try {
await inner()
} finally {
await next.renameFolder('app/add-me', 'app/remove-me')
}
},
checkChanged: makeTextCheck('/add-me', 'hello world'),
},
// TODO fix this case with Turbopack
...(isTurbopack
? {}
: {
'loader change': {
async checkInitial() {
await textCheck('/loader', 'hello world')
await textCheck('/loader/client', 'hello world')
},
withChange: makeFileEdit('my-loader.js'),
async checkChanged() {
await textCheck('/loader', 'hello filesystem cache')
await textCheck('/loader/client', 'hello filesystem cache')
},
fullInvalidation: !isTurbopack,
},
}),
'next config change': {
async checkInitial() {
await textCheck('/next-config', 'hello world')
await textCheck('/next-config/client', 'hello world')
},
withChange: makeFileEdit('next.config.js'),
async checkChanged() {
await textCheck('/next-config', 'hello filesystem cache')
await textCheck('/next-config/client', 'hello filesystem cache')
},
fullInvalidation: !isTurbopack,
},
'env var change': {
async checkInitial() {
await textCheck('/env', 'hello world')
await textCheck('/env/client', 'hello world')
},
async withChange(inner) {
process.env.NEXT_PUBLIC_ENV_VAR = 'hello filesystem cache'
try {
await inner()
} finally {
process.env.NEXT_PUBLIC_ENV_VAR = 'hello world'
}
},
async checkChanged() {
await textCheck('/env', 'hello filesystem cache')
await textCheck('/env/client', 'hello filesystem cache')
},
},
} as const
// Checking only single change and all combined for performance reasons.
const combinations = Object.entries(POTENTIAL_CHANGES).map(([k, v]) => [
k,
[v],
]) as Array<[string, Array<Change>]>
combinations.push([
Object.keys(POTENTIAL_CHANGES).join(', '),
Object.values(POTENTIAL_CHANGES),
])
for (const [name, changes] of combinations) {
// Very flakey with Webpack enabled
;(process.env.IS_TURBOPACK_TEST ? it : it.skip)(
`should allow to change files while stopped (${name})`,
async () => {
let fullInvalidation = !cacheEnabled
for (const change of changes) {
await change.checkInitial()
if (change.fullInvalidation) {
fullInvalidation = true
}
}
let unchangedTimestamp: string
if (!fullInvalidation) {
const browser = await next.browser('/unchanged')
unchangedTimestamp = await browser.elementByCss('main').text()
expect(unchangedTimestamp).toMatch(/Timestamp = \d+$/)
await browser.close()
}
async function checkChanged() {
for (const change of changes) {
await change.checkChanged()
}
if (!fullInvalidation) {
const browser = await next.browser('/unchanged')
const timestamp = await browser.elementByCss('main').text()
expect(unchangedTimestamp).toEqual(timestamp)
await browser.close()
}
}
await stop()
async function inner() {
await start()
await checkChanged()
// Some no-op change builds
for (let i = 0; i < 2; i++) {
await restartCycle()
await checkChanged()
}
await stop()
}
let current = inner
for (const change of changes) {
const prev = current
current = () => change.withChange(prev)
}
await current()
await start()
for (const change of changes) {
await change.checkInitial()
}
if (!fullInvalidation) {
const browser = await next.browser('/unchanged')
const timestamp = await browser.elementByCss('main').text()
expect(unchangedTimestamp).toEqual(timestamp)
await browser.close()
}
},
200000
)
}
})
}
|