Unverified Commit 6beec2f6 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

Reduce flakiness of app-fetch-logging test (#53612)

Updates `app-fetch-logging` tests to not start/stop the server each run and instead slice the relevant log output. Also moves log parsing inside of `check` since logs get appended asynchronously.

ref: https://github.com/vercel/next.js/actions/runs/5767647854/job/15637691191#step:28:662
parent 081151fe
import path from 'path' import path from 'path'
import { createNextDescribe, FileRef } from '../../../lib/e2e-utils'
import stripAnsi from 'strip-ansi' import stripAnsi from 'strip-ansi'
import { check } from 'next-test-utils'
import { createNextDescribe, FileRef } from 'e2e-utils'
function parseLogsFromCli(cliOutput: string) { function parseLogsFromCli(cliOutput: string) {
return stripAnsi(cliOutput) return stripAnsi(cliOutput)
.split('\n') .split('\n')
.filter((log) => log.includes('cache:')) .filter((log) => log.includes('ms (cache:'))
.map((log) => { .map((log) => {
const trimmedLog = log.replace(/^[^a-zA-Z]+/, '') const trimmedLog = log.replace(/^[^a-zA-Z]+/, '')
const parts = trimmedLog.split(' ') const parts = trimmedLog.split(' ')
...@@ -40,61 +41,76 @@ createNextDescribe( ...@@ -40,61 +41,76 @@ createNextDescribe(
({ next, isNextDev }) => { ({ next, isNextDev }) => {
describe('with verbose logging', () => { describe('with verbose logging', () => {
it('should only log requests in dev mode', async () => { it('should only log requests in dev mode', async () => {
const outputIndex = next.cliOutput.length
await next.fetch('/default-cache') await next.fetch('/default-cache')
const logs = stripAnsi(next.cliOutput)
if (isNextDev) { await check(() => {
expect(logs).toContain('GET /default-cache 200') const logs = stripAnsi(next.cliOutput.slice(outputIndex))
} else { const hasLogs = logs.includes('GET /default-cache 200')
expect(logs).not.toContain('GET /default-cache 200')
}
await next.stop() if (isNextDev && hasLogs) {
return 'success'
}
if (!isNextDev && !hasLogs) {
return 'success'
}
}, 'success')
}) })
if (isNextDev) { if (isNextDev) {
it("should log 'skip' cache status with a reason when cache: 'no-cache' is used", async () => { it("should log 'skip' cache status with a reason when cache: 'no-cache' is used", async () => {
await next.start() const outputIndex = next.cliOutput.length
await next.fetch('/default-cache') await next.fetch('/default-cache')
const logs = parseLogsFromCli(next.cliOutput)
const logEntry = logs.find((log) => await check(() => {
log.url.includes('api/random?no-cache') const logs = parseLogsFromCli(next.cliOutput.slice(outputIndex))
)
const logEntry = logs.find((log) =>
expect(logEntry?.cache).toMatchInlineSnapshot( log.url.includes('api/random?no-cache')
`"(cache: SKIP, reason: cache: no-cache)"` )
)
await next.stop() if (logEntry?.cache === '(cache: SKIP, reason: cache: no-cache)') {
return 'success'
}
}, 'success')
}) })
it("should log 'skip' cache status with a reason when revalidate: 0 is used", async () => { it("should log 'skip' cache status with a reason when revalidate: 0 is used", async () => {
await next.start() const outputIndex = next.cliOutput.length
await next.fetch('/default-cache') await next.fetch('/default-cache')
const logs = parseLogsFromCli(next.cliOutput) await check(() => {
const logEntry = logs.find((log) => const logs = parseLogsFromCli(next.cliOutput.slice(outputIndex))
log.url.includes('api/random?revalidate-0')
) const logEntry = logs.find((log) =>
log.url.includes('api/random?revalidate-0')
expect(logEntry?.cache).toMatchInlineSnapshot( )
`"(cache: SKIP, reason: revalidate: 0)"`
) if (logEntry?.cache === '(cache: SKIP, reason: revalidate: 0)') {
await next.stop() return 'success'
}
}, 'success')
}) })
it("should log 'skip' cache status with a reason when the browser indicates caching should be ignored", async () => { it("should log 'skip' cache status with a reason when the browser indicates caching should be ignored", async () => {
await next.start() const outputIndex = next.cliOutput.length
await next.fetch('/default-cache', { await next.fetch('/default-cache', {
headers: { 'Cache-Control': 'no-cache' }, headers: { 'Cache-Control': 'no-cache' },
}) })
const logs = parseLogsFromCli(next.cliOutput) await check(() => {
const logEntry = logs.find((log) => const logs = parseLogsFromCli(next.cliOutput.slice(outputIndex))
log.url.includes('api/random?auto-cache')
) const logEntry = logs.find((log) =>
log.url.includes('api/random?auto-cache')
expect(logEntry?.cache).toMatchInlineSnapshot( )
`"(cache: SKIP, reason: cache-control: no-cache (hard refresh))"`
) if (
await next.stop() logEntry?.cache ===
'(cache: SKIP, reason: cache-control: no-cache (hard refresh))'
) {
return 'success'
}
}, 'success')
}) })
} }
}) })
...@@ -107,10 +123,17 @@ createNextDescribe( ...@@ -107,10 +123,17 @@ createNextDescribe(
}) })
it('should not log fetch requests at all', async () => { it('should not log fetch requests at all', async () => {
const outputIndex = next.cliOutput.length
await next.fetch('/default-cache') await next.fetch('/default-cache')
const logs = stripAnsi(next.cliOutput)
expect(logs).not.toContain('GET /default-cache 200') await check(() => {
const logs = stripAnsi(next.cliOutput.slice(outputIndex))
if (logs.includes('GET /default-cache 200')) {
return 'fail'
}
return 'success'
}, 'success')
}) })
}) })
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment