From a77ec12b2ea3e65c02c117fb461feec14e3850f4 Mon Sep 17 00:00:00 2001 From: Hweinstock <42325418+Hweinstock@users.noreply.github.com> Date: Mon, 11 Nov 2024 17:24:33 -0500 Subject: [PATCH] test(vscode): fix failure to close editors on `beforeEach` hook. (#5975) ## Problem - See https://github.com/aws/aws-toolkit-vscode/issues/5325 - the following `waitUntil` is timing out: https://github.com/aws/aws-toolkit-vscode/blob/43299f368308b6720ba22bbd910c6714bc47bdcb/packages/core/src/test/testUtil.ts#L495-L518 - All occurrences of error (AFAIK) fail to list any editors open. This implies `waitUntil` is timing out even though all editors (that aren't ignored by pattern) are closed. ## Solution A potential explanation is that the `executeCommand` is taking a long time to close the editors ignored by the patterns, and causing a timeout in `waitUntil`. In this case, we don't want to error. - refactor to also check if activeEditors is cleared. If there aren't any, we shouldn't be throwing an error. - Also, decrease timeout to avoid wasting time here during tests (if we can't close ignored editors, don't waste time trying). This code is run between every test so 5 second timeout per test is huge. --- License: I confirm that my contribution is made under the terms of the Apache 2.0 license. --- packages/core/src/test/testUtil.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/core/src/test/testUtil.ts b/packages/core/src/test/testUtil.ts index d45a0b1ad20..eac5ffc2a2b 100644 --- a/packages/core/src/test/testUtil.ts +++ b/packages/core/src/test/testUtil.ts @@ -490,30 +490,30 @@ export async function closeAllEditors(): Promise { /amazonwebservices\.[a-z\-]+-vscode\./, /nullExtensionDescription./, // Sometimes exists instead of the prior line, see https://github.com/aws/aws-toolkit-vscode/issues/4658 ] - const editors: vscode.TextEditor[] = [] + const editorsToClose: vscode.TextEditor[] = [] const noVisibleEditor: boolean | undefined = await waitUntil( async () => { // Race: documents could appear after the call to closeAllEditors(), so retry. await vscode.commands.executeCommand(closeAllCmd) - editors.length = 0 - editors.push( + editorsToClose.length = 0 + editorsToClose.push( ...vscode.window.visibleTextEditors.filter( (editor) => !ignorePatterns.some((p) => p.test(editor.document.fileName)) ) ) - return editors.length === 0 + return editorsToClose.length === 0 }, { - timeout: 5000, // Arbitrary values. Should succeed except when VS Code is lagging heavily. + timeout: 1000, // Arbitrary values. Should succeed except when VS Code is lagging heavily. interval: 250, truthy: true, } ) - if (!noVisibleEditor) { - const editorNames = editors.map((editor) => `\t${editor.document.fileName}`) + if (!noVisibleEditor && editorsToClose.length > 0) { + const editorNames = editorsToClose.map((editor) => `\t${editor.document.fileName}`) throw new Error(`Editors were still open after closeAllEditors():\n${editorNames.join('\n')}`) } }