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

This commit is contained in:
JJ Kasper 2020-05-05 15:19:07 -05:00 committed by GitHub
parent 8fd7338f77
commit b72d9fa41d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 29 deletions

View file

@ -8,7 +8,7 @@ import {
findPort,
launchApp,
killApp,
waitFor,
check,
} from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
@ -54,11 +54,13 @@ describe('TypeScript Features', () => {
)
await renderViaHTTP(appPort, '/hello')
await waitFor(2 * 1000)
await fs.writeFile(basicPage, contents)
expect(output).toContain(
`Module not found: Can't resolve 'components/worldd' in`
const found = await check(
() => output,
/Module not found: Can't resolve 'components\/worldd' in/,
false
)
await fs.writeFile(basicPage, contents)
expect(found).toBe(true)
})
})
})

View file

@ -8,7 +8,7 @@ import {
findPort,
launchApp,
killApp,
waitFor,
check,
} from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
@ -71,9 +71,13 @@ describe('TypeScript Features', () => {
await fs.writeFile(basicPage, contents.replace('@c/world', '@c/worldd'))
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)
expect(output).toContain(`Module not found: Can't resolve '@c/worldd' in`)
expect(found).toBe(true)
})
})
})

View file

@ -338,32 +338,35 @@ export async function startCleanStaticServer(dir) {
return server
}
export async function check(contentFn, regex) {
let found = false
const timeout = setTimeout(async () => {
if (found) {
return
}
let content
// check for content in 1 second intervals timing out after
// 30 seconds
export async function check(contentFn, regex, hardError = true) {
let content
for (let tries = 0; tries < 30; tries++) {
try {
content = await contentFn()
} catch (err) {
console.error('Error while getting content', { regex })
}
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
if (regex.test(content)) {
// found the content
return true
}
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 {