forked from Azure/azure-sdk-for-js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[test-recorder] Support test context from vitest (Azure#28350)
Currently we just need suite title and test title to generate recording file names. `vitest` provides the info via `context` of the callback function. This PR adds vitest support to recorder. Call site would look like ```ts //... beforeEach(async (context) => { recorder = new Recorder(context); ```
- Loading branch information
1 parent
aaba9b5
commit 693683c
Showing
5 changed files
with
215 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// 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 | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// 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", | ||
}); | ||
}); | ||
}); |