Skip to content

Commit

Permalink
test: fix race condition in serverComponentsHmrCache tests (#70368)
Browse files Browse the repository at this point in the history
fixes a race condition in the `after()`-related test in
`server-components-hmr-cache.test.ts`

i made `getLoggedAfterValue` stricter while investigating an unrelated
issue, and saw it occasionally throw (see [CI
run](https://github.com/vercel/next.js/actions/runs/11001754094/job/30547234764?pr=70362#step:29:708)).
before this change, if `getLoggedAfterValue` was called before any logs
with `"After: "` were written, it would just return `undefined`, which
is obviously not what the test wants. this seems to have been happening
occasionally, but it wasn't visible because even if we got `undefined`
from one of the calls, the
`expect(valueBeforePatch).not.toEqual(valueAfterPatch)` assertion would
still pass -- one of the values is a string and the other is
`undefined`, so obviously they're not equal!
  • Loading branch information
lubieowoce authored Sep 24, 2024
1 parent 3e11b5e commit 1fa8e23
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ describe('server-components-hmr-cache', () => {
const loggedAfterValueRegexp = /After: (\d\.\d+)/
let cliOutputLength: number

const getLoggedAfterValue = () =>
next.cliOutput.slice(cliOutputLength).match(loggedAfterValueRegexp)?.[1]
const getLoggedAfterValue = () => {
const match = next.cliOutput
.slice(cliOutputLength)
.match(loggedAfterValueRegexp)

if (!match) {
throw new Error('No logs from after() found')
}
return match[1]
}

describe.each(['edge', 'node'])('%s runtime', (runtime) => {
afterEach(async () => {
Expand Down Expand Up @@ -124,7 +132,7 @@ describe('server-components-hmr-cache', () => {

it('should not use cached fetch calls for fast refresh requests', async () => {
const browser = await next.browser(`/${runtime}`)
const valueBeforePatch = getLoggedAfterValue()
const valueBeforePatch = await retry(() => getLoggedAfterValue())
cliOutputLength = next.cliOutput.length

await next.patchFile('components/shared-page.tsx', (content) =>
Expand All @@ -136,7 +144,7 @@ describe('server-components-hmr-cache', () => {
expect(updatedContent).toBe('bar')
})

const valueAfterPatch = getLoggedAfterValue()
const valueAfterPatch = await retry(() => getLoggedAfterValue())
expect(valueBeforePatch).not.toEqual(valueAfterPatch)
})
})
Expand Down

0 comments on commit 1fa8e23

Please sign in to comment.