From 9c1191abccb08b984369e1b488d1ed99f4f9f7f6 Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 4 Oct 2024 18:40:36 -0400 Subject: [PATCH] fix missing helpers in `next-test-utils.ts` --- test/lib/next-test-utils.ts | 59 +++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/test/lib/next-test-utils.ts b/test/lib/next-test-utils.ts index f36b8fe978ed1..acd904533b831 100644 --- a/test/lib/next-test-utils.ts +++ b/test/lib/next-test-utils.ts @@ -782,6 +782,65 @@ export async function hasRedbox(browser: BrowserInterface): Promise { return result } +export async function assertHasRedbox(browser: BrowserInterface) { + try { + await retry( + async () => { + const hasRedbox = await evaluate(browser, () => { + return Boolean( + [].slice + .call(document.querySelectorAll('nextjs-portal')) + .find((p) => + p.shadowRoot.querySelector( + '#nextjs__container_errors_label, #nextjs__container_errors_label' + ) + ) + ) + }) + expect(hasRedbox).toBe(true) + }, + 5000, + 200 + ) + } catch (errorCause) { + const error = new Error('Expected Redbox but found none') + Error.captureStackTrace(error, assertHasRedbox) + throw error + } +} + +export async function assertNoRedbox(browser: BrowserInterface) { + await waitFor(5000) + const hasRedbox = await evaluate(browser, () => { + return Boolean( + [].slice + .call(document.querySelectorAll('nextjs-portal')) + .find((p) => + p.shadowRoot.querySelector( + '#nextjs__container_errors_label, #nextjs__container_errors_label' + ) + ) + ) + }) + + if (hasRedbox) { + const [redboxHeader, redboxDescription, redboxSource] = await Promise.all([ + getRedboxHeader(browser).catch(() => ''), + getRedboxDescription(browser).catch(() => ''), + getRedboxSource(browser).catch(() => ''), + ]) + + const error = new Error( + 'Expected no Redbox but found one\n' + + `header: ${redboxHeader}\n` + + `description: ${redboxDescription}\n` + + `source: ${redboxSource}` + ) + Error.captureStackTrace(error, assertNoRedbox) + throw error + } +} + export async function hasErrorToast( browser: BrowserInterface ): Promise {