suite-metrics
Version:
Easily keep track of metrics for many nested test suites
144 lines (141 loc) • 5.8 kB
TypeScript
type Test = {
readonly name: string;
startTimestamp: number;
endTimestamp: number;
duration: number;
completed: boolean;
readonly testNumber: number;
readonly suiteTestNumber: number;
};
type Suite = {
readonly name: string;
tests: Map<string, Test> | null;
numSubTests: number;
subSuites: Map<string, Suite> | null;
};
type SuiteData = {
readonly name: string;
readonly parentSuites: string[];
readonly childSuites: string[] | null;
readonly testMetrics: {
readonly numTests: number;
readonly totalTime: number;
readonly averageTime: number | null;
};
};
type RecursiveSuiteData = {
readonly name: string;
readonly parentSuites: string[] | null;
readonly childSuites: string[] | null;
readonly directTestMetrics: {
readonly numTests: number;
readonly totalTime: number;
readonly averageTime: number | null;
};
readonly subTestMetrics: {
readonly numTests: number;
readonly totalTime: number;
readonly averageTime: number | null;
};
readonly totalTestMetrics: {
readonly numTests: number;
readonly totalTime: number;
readonly averageTime: number | null;
};
};
interface ISuiteMetrics {
startTest(testPath: string[]): void;
stopTest(): void;
suiteExists(suitePath: string[]): boolean;
testExists(testPath: string[]): boolean;
getSuiteMetrics(suitePath: string[]): SuiteData;
getSuiteMetricsRecursive(suitePath: string[]): RecursiveSuiteData;
printAllSuiteMetrics(): string;
}
declare class SuiteMetrics implements ISuiteMetrics {
private static _instance;
private readonly _suite;
private readonly _topLevelSuite;
private _currentSuite;
private _currentTest;
private _currentTime;
private _numTests;
private _validateName;
private _createSuite;
private _getSuite;
private _exists;
private _addTest;
/**
* Gets an instance on this class. Simplifies having one accessible metrics instance for many classes
*/
static getInstance(): SuiteMetrics;
/**
* Resets the singleton instance of this class, wiping all data on it to start fresh
*/
static resetInstance(): void;
/**
* Starts a new test. Call directly before the test for maximum accuracy
*
* @param name Suites the test is part of, then the test name (in order). E.g. ['suite1', 'suite2', 'test1'] means
* there is a top-level suite named 'suite1', which has a suite inside it named 'suite2', which has a test inside it
* named 'test1' which we want to measure
*/
startTest(name: string[]): void;
/**
* Stops the current test (the last time startTest() was called). Call directly after the test for maximum accuracy
*/
stopTest(): void;
/**
* Returns true if a suite currently exists, false otherwise
*
* @param name Name of the suite to check for. E.g. ['suite1', 'suite2'] means there is a top-level suite named
* 'suite1', which has a suite inside it named 'suite2' which we want to check if it exists
*/
suiteExists(name: string[]): boolean;
/**
* Returns true if a test currently exists, false otherwise
* @param name Name of the test to check for. E.g. ['suite1', 'suite2', 'test1'] means there is a top-level suite
* named 'suite1', which has a suite inside it named 'suite2', which has a test inside it named 'test1' which we want
* to check if it exists
*/
testExists(name: string[]): boolean;
/**
* Gets the metrics for a test (name, start/stop, duration, completion, order). Throws an error if the test does not
* exist
*
* @param name Name of the test to get metrics for. E.g. ['suite1', 'suite2', 'test1'] means there is a top-level
* suite named 'suite1', which has a suite inside it named 'suite2', which has a test inside it named 'test1' which
* we want to get metrics for
*/
getTestMetrics(name: string[]): Test;
/**
* Gets the metrics for a suite - suite metadata (name, parents, children) and test metrics (number of tests,
* total time, average time)
*
* This method only calculates test metrics for tests directly in this suite. To include tests that are in
* sub-suites of this suite, use getSuiteMetricsRecursive() instead
*
* @param name Name of the suite to get metrics for. E.g. ['suite1', 'suite2'] means there is a top-level suite
* named 'suite1', which has a suite inside it named 'suite2' which we want to get metrics for. Can also pass a
* Mocha context ('this') to get the name from it, or an empty array to get metrics for the top-level suite
*/
getSuiteMetrics(name: string[]): SuiteData;
private _subSuiteMetrics;
/**
* Gets the metrics for a suite (metadata - name, parents, children) and metrics for all its sub-suite's tests
* (number of tests, total time, average time)
*
* @param name Name of the suite to get metrics for. E.g. ['suite1', 'suite2'] means there is a top-level suite
* named 'suite1', which has a suite inside it named 'suite2' which we want to get metrics for. Can also pass an
* empty array to get metrics for the top-level suite
*/
getSuiteMetricsRecursive(name: string[]): RecursiveSuiteData;
private _printSuiteHelper;
/**
* Returns a formatted string of all the test suite metrics
*
* @param topLevelSuite (Defaults to true) Include the default top-level suite, providing a summary of all suites
*/
printAllSuiteMetrics(topLevelSuite?: boolean): string;
}
export { type ISuiteMetrics, type RecursiveSuiteData, type Suite, type SuiteData, type Test, SuiteMetrics as default };