Unverified Commit 9ab8434d authored by Jude Gao's avatar Jude Gao Committed by GitHub

hmr-test waits for compile signal (#73064)

- 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.
parent 37f26ac2
...@@ -62,7 +62,11 @@ describe(`app-dir-hmr`, () => { ...@@ -62,7 +62,11 @@ describe(`app-dir-hmr`, () => {
await browser.eval('window.__TEST_NO_RELOAD = true') await browser.eval('window.__TEST_NO_RELOAD = true')
expect(await browser.elementByCss('p').text()).toBe('mac') expect(await browser.elementByCss('p').text()).toBe('mac')
const getCliOutput = next.getCliOutputFromHere()
await next.patchFile(envFile, 'MY_DEVICE="ipad"', async () => { await next.patchFile(envFile, 'MY_DEVICE="ipad"', async () => {
await waitFor(() => getCliOutput().includes('Reload env'))
await retry(async () => { await retry(async () => {
expect(await browser.elementByCss('p').text()).toBe('ipad') expect(await browser.elementByCss('p').text()).toBe('ipad')
}) })
......
...@@ -10,6 +10,7 @@ import { ...@@ -10,6 +10,7 @@ import {
launchApp, launchApp,
retry, retry,
getRedboxSource, getRedboxSource,
assertNoRedbox,
} from 'next-test-utils' } from 'next-test-utils'
import stripAnsi from 'strip-ansi' import stripAnsi from 'strip-ansi'
...@@ -93,7 +94,7 @@ describe('server-side dev errors', () => { ...@@ -93,7 +94,7 @@ describe('server-side dev errors', () => {
expect(await getRedboxSource(browser)).toContain('missingVar') expect(await getRedboxSource(browser)).toContain('missingVar')
await fs.writeFile(gspPage, content, { flush: true }) await fs.writeFile(gspPage, content, { flush: true })
await assertHasRedbox(browser) await assertNoRedbox(browser)
} finally { } finally {
await fs.writeFile(gspPage, content) await fs.writeFile(gspPage, content)
} }
...@@ -146,7 +147,7 @@ describe('server-side dev errors', () => { ...@@ -146,7 +147,7 @@ describe('server-side dev errors', () => {
expect(await getRedboxSource(browser)).toContain('missingVar') expect(await getRedboxSource(browser)).toContain('missingVar')
await fs.writeFile(gsspPage, content) await fs.writeFile(gsspPage, content)
await assertHasRedbox(browser) await assertNoRedbox(browser)
} finally { } finally {
await fs.writeFile(gsspPage, content) await fs.writeFile(gsspPage, content)
} }
......
...@@ -613,4 +613,11 @@ export class NextInstance { ...@@ -613,4 +613,11 @@ export class NextInstance {
cb(...args) cb(...args)
}) })
} }
public getCliOutputFromHere() {
const length = this.cliOutput.length
return () => {
return this.cliOutput.slice(length)
}
}
} }
...@@ -647,8 +647,21 @@ export async function stopApp(server: http.Server | undefined) { ...@@ -647,8 +647,21 @@ export async function stopApp(server: http.Server | undefined) {
await promisify(server.close).apply(server) await promisify(server.close).apply(server)
} }
export function waitFor(millis: number) { export async function waitFor(
return new Promise((resolve) => setTimeout(resolve, millis)) 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( export async function startStaticServer(
......
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