From 40da1aeb9253402cf24ddfd7ca680de0e0a5c96b Mon Sep 17 00:00:00 2001 From: OJ Kwon <1210596+kwonoj@users.noreply.github.com> Date: Mon, 11 Sep 2023 08:29:49 -0700 Subject: [PATCH] test(nexttestsetup): teardown nextinstance gracefully (#55144) ### What? https://vercel.slack.com/archives/C04KC8A53T7/p1694190462958199 I realized we throws an error attempt to destory against undefined instance when `beforeAll` fails in any reason. This is quite redundant error since if next instance doesn't exist, likely there's an upstream test failures already and destory against undefined error is not useful to debug anyway. PR replaces `nextTestSetup`'s teardown first, for the remaining places calling `next.destory` explicitly will be amended later. --- test/lib/e2e-utils.ts | 7 ++++++- test/lib/next-test-utils.ts | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/test/lib/e2e-utils.ts b/test/lib/e2e-utils.ts index 1d739f6de28c4..5de183b8b29c5 100644 --- a/test/lib/e2e-utils.ts +++ b/test/lib/e2e-utils.ts @@ -233,7 +233,12 @@ export function nextTestSetup( next = await createNext(options) }) afterAll(async () => { - await next.destroy() + // Gracefully destroy the instance if `createNext` success. + // If next instnace is not available, it's likely beforeAll hook failed and unnecessariliy throws another error + // by attempting to destroy on undefined. + if (next) { + await next.destroy() + } }) } diff --git a/test/lib/next-test-utils.ts b/test/lib/next-test-utils.ts index 09c9736d8c552..5e28fd7c602a1 100644 --- a/test/lib/next-test-utils.ts +++ b/test/lib/next-test-utils.ts @@ -536,7 +536,9 @@ export async function killProcess(pid: number): Promise { // Kill a launched app export async function killApp(instance: ChildProcess) { - await killProcess(instance.pid) + if (instance && instance.pid) { + await killProcess(instance.pid) + } } export async function startApp(app: NextServer) {