Skip to content

Commit

Permalink
hmr-test waits for compile signal (vercel#73064)
Browse files Browse the repository at this point in the history
- The waitFor function now supports a predicate. Unlike retry, waitFor
does not include a timeout. Use it only when the predicate is guaranteed
to evaluate to true before proceeding to the next test. If it runs
indefinitely, Jest enforces a 120-second timeout per test.
- The getCliOutputFromHere utility provides a convenient string getter
that captures CLI output in between.
  • Loading branch information
gaojude authored Nov 21, 2024
1 parent 37f26ac commit 9ab8434
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
4 changes: 4 additions & 0 deletions test/development/app-hmr/hmr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ describe(`app-dir-hmr`, () => {
await browser.eval('window.__TEST_NO_RELOAD = true')

expect(await browser.elementByCss('p').text()).toBe('mac')

const getCliOutput = next.getCliOutputFromHere()
await next.patchFile(envFile, 'MY_DEVICE="ipad"', async () => {
await waitFor(() => getCliOutput().includes('Reload env'))

await retry(async () => {
expect(await browser.elementByCss('p').text()).toBe('ipad')
})
Expand Down
5 changes: 3 additions & 2 deletions test/integration/server-side-dev-errors/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
launchApp,
retry,
getRedboxSource,
assertNoRedbox,
} from 'next-test-utils'
import stripAnsi from 'strip-ansi'

Expand Down Expand Up @@ -93,7 +94,7 @@ describe('server-side dev errors', () => {

expect(await getRedboxSource(browser)).toContain('missingVar')
await fs.writeFile(gspPage, content, { flush: true })
await assertHasRedbox(browser)
await assertNoRedbox(browser)
} finally {
await fs.writeFile(gspPage, content)
}
Expand Down Expand Up @@ -146,7 +147,7 @@ describe('server-side dev errors', () => {

expect(await getRedboxSource(browser)).toContain('missingVar')
await fs.writeFile(gsspPage, content)
await assertHasRedbox(browser)
await assertNoRedbox(browser)
} finally {
await fs.writeFile(gsspPage, content)
}
Expand Down
7 changes: 7 additions & 0 deletions test/lib/next-modes/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,4 +613,11 @@ export class NextInstance {
cb(...args)
})
}

public getCliOutputFromHere() {
const length = this.cliOutput.length
return () => {
return this.cliOutput.slice(length)
}
}
}
17 changes: 15 additions & 2 deletions test/lib/next-test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,21 @@ export async function stopApp(server: http.Server | undefined) {
await promisify(server.close).apply(server)
}

export function waitFor(millis: number) {
return new Promise((resolve) => setTimeout(resolve, millis))
export async function waitFor(
millisOrCondition: number | (() => boolean)
): Promise<void> {
if (typeof millisOrCondition === 'number') {
return new Promise((resolve) => setTimeout(resolve, millisOrCondition))
}

return new Promise((resolve) => {
const interval = setInterval(() => {
if (millisOrCondition()) {
clearInterval(interval)
resolve()
}
}, 100)
})
}

export async function startStaticServer(
Expand Down

0 comments on commit 9ab8434

Please sign in to comment.