From b72d9fa41dc817427c295a073a94b23259d32851 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Tue, 5 May 2020 15:19:07 -0500 Subject: [PATCH] Update tests to poll for output instead of wait for it (#12534) --- .../jsconfig-baseurl/test/index.test.js | 12 ++--- .../jsconfig-paths/test/index.test.js | 10 +++-- test/lib/next-test-utils.js | 45 ++++++++++--------- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/test/integration/jsconfig-baseurl/test/index.test.js b/test/integration/jsconfig-baseurl/test/index.test.js index 095bbae983..f451622dba 100644 --- a/test/integration/jsconfig-baseurl/test/index.test.js +++ b/test/integration/jsconfig-baseurl/test/index.test.js @@ -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) }) }) }) diff --git a/test/integration/jsconfig-paths/test/index.test.js b/test/integration/jsconfig-paths/test/index.test.js index 9daeb5c2b0..b1393f1a73 100644 --- a/test/integration/jsconfig-paths/test/index.test.js +++ b/test/integration/jsconfig-paths/test/index.test.js @@ -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) }) }) }) diff --git a/test/lib/next-test-utils.js b/test/lib/next-test-utils.js index dc504e7601..b570b83c2e 100644 --- a/test/lib/next-test-utils.js +++ b/test/lib/next-test-utils.js @@ -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 {