From 3c20c8a567d977a6146833311614ccee5539743c Mon Sep 17 00:00:00 2001 From: Jeremy Meng Date: Wed, 31 Jan 2024 00:07:29 -0800 Subject: [PATCH] Fix build failures (#28423) PR #28350 somehow cause many packages to fail to build because they don't have a dev dependency on @types/mocha. Reverting for now. Revert "[test-recorder] Support test context from vitest (#28350)" This reverts commit a2a2d038cfb6990e25eb696ec3ab1e39d60ec4e5. --- sdk/test-utils/recorder/src/index.ts | 1 - sdk/test-utils/recorder/src/recorder.ts | 62 ++----------- sdk/test-utils/recorder/src/testInfo.ts | 91 ------------------- .../recorder/src/utils/sessionFilePath.ts | 20 ++-- sdk/test-utils/recorder/test/recorder.spec.ts | 62 ------------- 5 files changed, 21 insertions(+), 215 deletions(-) delete mode 100644 sdk/test-utils/recorder/src/testInfo.ts delete mode 100644 sdk/test-utils/recorder/test/recorder.spec.ts diff --git a/sdk/test-utils/recorder/src/index.ts b/sdk/test-utils/recorder/src/index.ts index a245ce2fd518..74118cabef02 100644 --- a/sdk/test-utils/recorder/src/index.ts +++ b/sdk/test-utils/recorder/src/index.ts @@ -16,4 +16,3 @@ export { export { env } from "./utils/env"; export { delay } from "./utils/delay"; export { CustomMatcherOptions } from "./matcher"; -export { TestInfo, MochaTest, VitestTestContext } from "./testInfo"; diff --git a/sdk/test-utils/recorder/src/recorder.ts b/sdk/test-utils/recorder/src/recorder.ts index 50f2787d7f2f..96b06cd9e8f1 100644 --- a/sdk/test-utils/recorder/src/recorder.ts +++ b/sdk/test-utils/recorder/src/recorder.ts @@ -20,7 +20,8 @@ import { RecorderStartOptions, RecordingStateManager, } from "./utils/utils"; -import { assetsJsonPath, sessionFilePath, TestContext } from "./utils/sessionFilePath"; +import { Test } from "mocha"; +import { assetsJsonPath, sessionFilePath } from "./utils/sessionFilePath"; import { SanitizerOptions } from "./utils/utils"; import { paths } from "./utils/paths"; import { addSanitizers, transformsInfo } from "./sanitizer"; @@ -34,45 +35,6 @@ import { isNode } from "@azure/core-util"; import { env } from "./utils/env"; import { decodeBase64 } from "./utils/encoding"; import { AdditionalPolicyConfig } from "@azure/core-client"; -import { isMochaTest, isVitestTestContext, TestInfo, VitestSuite } from "./testInfo"; - -/** - * Caculates session file path and JSON assets path from test context - * - * @internal - */ -export function calculatePaths(testContext: TestInfo): TestContext { - if (isMochaTest(testContext)) { - if (!testContext.parent) { - throw new RecorderError( - `The parent of test '${testContext.title}' is undefined, so a file path for its recording could not be generated. Please place the test inside a describe block.`, - ); - } - return { - suiteTitle: testContext.parent.fullTitle(), - testTitle: testContext.title, - }; - } else if (isVitestTestContext(testContext)) { - if (!testContext.task.name || !testContext.task.suite.name) { - throw new RecorderError( - `Unable to determine the recording file path. Unexpected empty Vitest context`, - ); - } - const suites: string[] = []; - let p: VitestSuite | undefined = testContext.task.suite; - while (p?.name) { - suites.push(p.name); - p = p.suite; - } - - return { - suiteTitle: suites.reverse().join("_"), - testTitle: testContext.task.name, - }; - } else { - throw new RecorderError(`Unrecognized test info: ${testContext}`); - } -} /** * This client manages the recorder life cycle and interacts with the proxy-tool to do the recording, @@ -91,23 +53,19 @@ export class Recorder { private assetsJson?: string; private variables: Record; - constructor(private testContext?: TestInfo) { - if (!this.testContext) { - throw new Error( - "Unable to determine the recording file path, testContext provided is not defined.", - ); - } - + constructor(private testContext?: Test | undefined) { logger.info(`[Recorder#constructor] Creating a recorder instance in ${getTestMode()} mode`); if (isRecordMode() || isPlaybackMode()) { - const context = calculatePaths(this.testContext); - - this.sessionFile = sessionFilePath(context); - this.assetsJson = assetsJsonPath(); - if (this.testContext) { + this.sessionFile = sessionFilePath(this.testContext); + this.assetsJson = assetsJsonPath(); + logger.info(`[Recorder#constructor] Using a session file located at ${this.sessionFile}`); this.httpClient = createDefaultHttpClient(); + } else { + throw new Error( + "Unable to determine the recording file path, testContext provided is not defined.", + ); } } this.variables = {}; diff --git a/sdk/test-utils/recorder/src/testInfo.ts b/sdk/test-utils/recorder/src/testInfo.ts deleted file mode 100644 index 876013721343..000000000000 --- a/sdk/test-utils/recorder/src/testInfo.ts +++ /dev/null @@ -1,91 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT license. - -/** - * Represents a Test. - */ -export type TestInfo = MochaTest | VitestTestContext; - -/** - * Represents a Mocha Test. - */ -export interface MochaTest { - /** - * The title of the test. - */ - title: string; - /** - * The parent of the Mocha Test Suite. - */ - parent?: MochaTestSuite; -} - -/** - * Represents a Mocha Test Suite. - */ -export interface MochaTestSuite { - fullTitle(): string; -} - -/** - * Represents a Vitest Test Context - */ -export interface VitestTestContext { - /** - * The Vitest Context Task. - */ - task: VitestTask; -} - -export interface VitestTaskBase { - name: string; - suite?: VitestSuite; -} - -/** - * Represents a Vitest Test Context Task - */ -export interface VitestTask extends VitestTaskBase { - /** - * The Vitest Context Task Name. - */ - name: string; - /** - * The Vitest Context Task Suite. - */ - suite: VitestSuite; -} - -/** - * Represents a Vitest Test Suite. - */ -export interface VitestSuite extends VitestTaskBase { - /** - * The Vitest Context Task Suite Name. - */ - name: string; -} - -/** - * Determines whether the given test is a Mocha Test. - * @param test - The test to check. - * @returns true if the given test is a Mocha Test. - */ -export function isMochaTest(test: unknown): test is MochaTest { - return typeof test === "object" && test != null && "title" in test; -} - -/** - * Determines whether the given test is a Vitest Test. - * @param test - The test to check. - * @returns true if the given test is a Vitest Test. - */ -export function isVitestTestContext(test: unknown): test is VitestTestContext { - return ( - typeof test == "function" && - "task" in test && - typeof test.task === "object" && - test.task != null && - "name" in test.task - ); -} diff --git a/sdk/test-utils/recorder/src/utils/sessionFilePath.ts b/sdk/test-utils/recorder/src/utils/sessionFilePath.ts index be4490070b44..f494814540cb 100644 --- a/sdk/test-utils/recorder/src/utils/sessionFilePath.ts +++ b/sdk/test-utils/recorder/src/utils/sessionFilePath.ts @@ -4,13 +4,9 @@ import { isNode } from "@azure/core-util"; import { generateTestRecordingFilePath } from "./filePathGenerator"; import { relativeRecordingsPath } from "./relativePathCalculator"; +import { RecorderError } from "./utils"; -export interface TestContext { - suiteTitle: string; // describe(suiteTitle, () => {}) - testTitle: string; // it(testTitle, () => {}) -} - -export function sessionFilePath(testContext: TestContext): string { +export function sessionFilePath(testContext: Mocha.Test): string { // sdk/service/project/recordings/{node|browsers}//recording_.json return `${relativeRecordingsPath()}/${recordingFilePath(testContext)}`; } @@ -20,11 +16,17 @@ export function sessionFilePath(testContext: TestContext): string { * * `{node|browsers}//recording_.json` */ -export function recordingFilePath(testContext: TestContext): string { +export function recordingFilePath(testContext: Mocha.Test): string { + if (!testContext.parent) { + throw new RecorderError( + `Test ${testContext.title} is not inside a describe block, so a file path for its recording could not be generated. Please place the test inside a describe block.`, + ); + } + return generateTestRecordingFilePath( isNode ? "node" : "browsers", - testContext.suiteTitle, - testContext.testTitle, + testContext.parent.fullTitle(), + testContext.title, ); } diff --git a/sdk/test-utils/recorder/test/recorder.spec.ts b/sdk/test-utils/recorder/test/recorder.spec.ts deleted file mode 100644 index 853469d65aeb..000000000000 --- a/sdk/test-utils/recorder/test/recorder.spec.ts +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT license. - -import { expect } from "chai"; - -import { calculatePaths } from "../src/recorder"; - -describe("Recorder file paths", () => { - it("calculates paths for a Mocha test", () => { - const mochaTest = { - title: "mocha test title", - parent: { - fullTitle: () => "mocha suite title", - }, - }; - const context = calculatePaths(mochaTest); - - expect(context).to.eql({ - suiteTitle: "mocha suite title", - testTitle: "mocha test title", - }); - }); - - it("calculates paths for a vitest test", () => { - const vitestTest = () => { - /* no-op */ - }; - (vitestTest as any).task = { - name: "vitest test title", - suite: { - name: "vitest suite title", - }, - }; - - const context = calculatePaths(vitestTest as any); - expect(context).to.eql({ - suiteTitle: "vitest suite title", - testTitle: "vitest test title", - }); - }); - - it("calculates paths for a vitest test with nested suites", () => { - const vitestTest = () => { - /* no-op */ - }; - (vitestTest as any).task = { - name: "vitest test title", - suite: { - name: "vitest suite title", - suite: { - name: "toplevel suite", - }, - }, - }; - - const context = calculatePaths(vitestTest as any); - expect(context).to.eql({ - suiteTitle: "toplevel suite_vitest suite title", - testTitle: "vitest test title", - }); - }); -});