Unverified Commit b72d9fa4 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update tests to poll for output instead of wait for it (#12534)

parent 8fd7338f
...@@ -8,7 +8,7 @@ import { ...@@ -8,7 +8,7 @@ import {
findPort, findPort,
launchApp, launchApp,
killApp, killApp,
waitFor, check,
} from 'next-test-utils' } from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2 jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
...@@ -54,11 +54,13 @@ describe('TypeScript Features', () => { ...@@ -54,11 +54,13 @@ describe('TypeScript Features', () => {
) )
await renderViaHTTP(appPort, '/hello') await renderViaHTTP(appPort, '/hello')
await waitFor(2 * 1000) const found = await check(
await fs.writeFile(basicPage, contents) () => output,
expect(output).toContain( /Module not found: Can't resolve 'components\/worldd' in/,
`Module not found: Can't resolve 'components/worldd' in` false
) )
await fs.writeFile(basicPage, contents)
expect(found).toBe(true)
}) })
}) })
}) })
...@@ -8,7 +8,7 @@ import { ...@@ -8,7 +8,7 @@ import {
findPort, findPort,
launchApp, launchApp,
killApp, killApp,
waitFor, check,
} from 'next-test-utils' } from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2 jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
...@@ -71,9 +71,13 @@ describe('TypeScript Features', () => { ...@@ -71,9 +71,13 @@ describe('TypeScript Features', () => {
await fs.writeFile(basicPage, contents.replace('@c/world', '@c/worldd')) await fs.writeFile(basicPage, contents.replace('@c/world', '@c/worldd'))
await renderViaHTTP(appPort, '/basic-alias') await renderViaHTTP(appPort, '/basic-alias')
await waitFor(2 * 1000) const found = await check(
() => output,
/Module not found: Can't resolve '@c\/worldd' in/,
false
)
await fs.writeFile(basicPage, contents) await fs.writeFile(basicPage, contents)
expect(output).toContain(`Module not found: Can't resolve '@c/worldd' in`) expect(found).toBe(true)
}) })
}) })
}) })
...@@ -338,32 +338,35 @@ export async function startCleanStaticServer(dir) { ...@@ -338,32 +338,35 @@ export async function startCleanStaticServer(dir) {
return server return server
} }
export async function check(contentFn, regex) { // check for content in 1 second intervals timing out after
let found = false // 30 seconds
const timeout = setTimeout(async () => { export async function check(contentFn, regex, hardError = true) {
if (found) { let content
return
} for (let tries = 0; tries < 30; tries++) {
let content
try { try {
content = await contentFn() content = await contentFn()
} catch (err) { if (regex.test(content)) {
console.error('Error while getting content', { regex }) // found the content
} return true
console.error('TIMED OUT CHECK: ', { regex, content })
throw new Error('TIMED OUT: ' + regex + '\n\n' + content)
}, 1000 * 30)
while (!found) {
try {
const newContent = await contentFn()
if (regex.test(newContent)) {
found = true
clearTimeout(timeout)
break
} }
await waitFor(1000) await waitFor(1000)
} catch (ex) {} } catch (err) {
if (tries === 30) {
console.error('Error while getting content', { regex }, err)
}
}
}
console.error('TIMED OUT CHECK: ', { regex, content })
if (hardError) {
// maintain previous behavior where the error is thrown in a timeout
// and isn't caught when wrapped in a try/catch
setTimeout(() => {
throw new Error('TIMED OUT: ' + regex + '\n\n' + content)
}, 1)
} }
return false
} }
export class File { export class File {
......
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