Skip to content

Commit

Permalink
qase-javascript-commons: add fallback reporter
Browse files Browse the repository at this point in the history
--
Add suport the fallback reporter. If the upstream reporter throws any error then the listener will try to use the fallback reporter.

Also fix the format enum.
  • Loading branch information
Dmitrii Gridnev committed Mar 26, 2024
1 parent c5c6269 commit cf5894d
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 23 deletions.
123 changes: 111 additions & 12 deletions qase-javascript-commons/src/qase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,24 @@ export class QaseReporter extends AbstractReporter {
*/
private upstreamReporter?: ReporterInterface;

/**
* @type {ReporterInterface}
* @private
*/
private fallbackReporter?: ReporterInterface;


/**
* @type {boolean}
* @private
*/
private disabled: boolean = false;

Check failure on line 109 in qase-javascript-commons/src/qase.ts

View workflow job for this annotation

GitHub Actions / Project qase-javascript-commons - Node 16

Type boolean trivially inferred from a boolean literal, remove type annotation

/**
* @type {boolean}
* @private
*/
private disabled = false;
private useFallback: boolean = false;

Check failure on line 115 in qase-javascript-commons/src/qase.ts

View workflow job for this annotation

GitHub Actions / Project qase-javascript-commons - Node 16

Type boolean trivially inferred from a boolean literal, remove type annotation

/**
* @param {OptionsType} options
Expand All @@ -112,15 +125,43 @@ export class QaseReporter extends AbstractReporter {
super({ debug: composedOptions.debug }, logger);

try {
this.upstreamReporter = this.createUpstreamReporter(
this.upstreamReporter = this.createReporter(
composedOptions.mode as ModeEnum || ModeEnum.off,

Check warning on line 129 in qase-javascript-commons/src/qase.ts

View workflow job for this annotation

GitHub Actions / Project qase-javascript-commons - Node 16

Unnecessary conditional, value is always truthy
composedOptions,
logger,
);
} catch (error) {
if (error instanceof DisabledException) {
this.disabled = true;
} else {
throw error;
this.logError('Unable to create upstream reporter:', error);

if (composedOptions.fallback != undefined) {
this.disabled = true;
return;
}

this.useFallback = true;
}
}

try {
this.fallbackReporter = this.createReporter(
composedOptions.fallback as ModeEnum || ModeEnum.off,
composedOptions,
logger,
);
} catch (error) {
if (error instanceof DisabledException) {
if (this.useFallback) {
this.disabled = true;
}
} else {
this.logError('Unable to create fallback reporter:', error);

if (this.useFallback && this.upstreamReporter === undefined) {
this.disabled = true;
}
}
}
}
Expand All @@ -130,48 +171,106 @@ export class QaseReporter extends AbstractReporter {
*/
public addTestResult(result: TestResultType) {
if (!this.disabled) {
this.logTestItem(result);

if (this.useFallback) {
this.addTestResultToFallback(result);
return;
}

try {
this.logTestItem(result);
this.upstreamReporter?.addTestResult(result);
} catch (error) {
this.logError('Unable to process result:', error);
}
catch (error) {
this.logError('Unable to add the result to the upstream reporter:', error);

this.disabled = true;
if (this.fallbackReporter == undefined) {
this.disabled = true;
return;
}

if (!this.useFallback) {
this.fallbackReporter?.setTestResults(this.upstreamReporter?.getTestResults() ?? []);
this.useFallback = true;
}

this.addTestResultToFallback(result);
}
}
}

/**
* @param {TestResultType} result
* @private
*/
private addTestResultToFallback(result: TestResultType): void {
try {
this.fallbackReporter?.addTestResult(result);
}
catch (error) {
this.logError('Unable to add the result to the fallback reporter:', error);
this.disabled = true;
}
}

/**
* @returns {Promise<void>}
*/
public async publish() {
public async publish(): Promise<void> {
if (!this.disabled) {
if (this.useFallback) {
await this.publishFallback();
}

try {
await this.upstreamReporter?.publish();
} catch (error) {
this.logError('Unable to publish run results:', error);
this.logError('Unable to publish the run results to the upstream reporter:', error);

this.disabled = true;
if (this.fallbackReporter == undefined) {
this.disabled = true;
return;
}

if (!this.useFallback) {
this.fallbackReporter?.setTestResults(this.upstreamReporter?.getTestResults() ?? []);
this.useFallback = true;
}

await this.publishFallback();
}
}
}

/**
* @returns {Promise<void>}
*/
private async publishFallback(): Promise<void> {
try {
await this.fallbackReporter?.publish();
}
catch (error) {
this.logError('Unable to publish the run results to the fallback reporter:', error);
this.disabled = true;
}
}

/**
* @todo implement mode registry
* @param {OptionsType} options
* @param {LoggerInterface} logger
* @returns {ReporterInterface}
* @private
*/
private createUpstreamReporter(
private createReporter(
mode: ModeEnum,
options: OptionsType,
logger?: LoggerInterface,
): ReporterInterface {
const {
frameworkPackage,
frameworkName,
reporterName,
mode = ModeEnum.off,
environment,
report = {},
testops = {},
Expand Down
18 changes: 18 additions & 0 deletions qase-javascript-commons/src/reporters/abstract-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export type ReporterOptionsType = {
export interface ReporterInterface {
addTestResult(result: TestResultType): void;
publish(): Promise<void>;
getTestResults(): TestResultType[];
setTestResults(results: TestResultType[]): void;
}

/**
Expand All @@ -33,6 +35,8 @@ export abstract class AbstractReporter implements ReporterInterface {
*/
private debug: boolean | undefined;

protected results: TestResultType[] = [];

/**
* @param {TestResultType} result
*/
Expand All @@ -57,6 +61,20 @@ export abstract class AbstractReporter implements ReporterInterface {
this.debug = debug;
}

/**
* @returns {TestResultType[]}
*/
public getTestResults(): TestResultType[] {
return this.results;
}

/**
* @param {TestResultType[]} results
*/
public setTestResults(results: TestResultType[]): void {
this.results = results;
}

/**
* @param {string} message
* @protected
Expand Down
5 changes: 0 additions & 5 deletions qase-javascript-commons/src/reporters/report-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ import { WriterInterface } from '../writer';
* @extends AbstractReporter
*/
export class ReportReporter extends AbstractReporter {
/**
* @type {TestResultType[]}
* @private
*/
private results: TestResultType[] = [];

/**
* @param {ReporterOptionsType} options
Expand Down
5 changes: 0 additions & 5 deletions qase-javascript-commons/src/reporters/testops-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,6 @@ export class TestOpsReporter extends AbstractReporter {
*/
private environment: number | undefined;

/**
* @type {TestResultType[]}
* @private
*/
private results: TestResultType[] = [];
/**
* @type {Record<string, string[]>}
* @private
Expand Down
2 changes: 1 addition & 1 deletion qase-javascript-commons/src/writer/driver-enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export enum DriverEnum {
*/
export enum FormatEnum {
json = 'json',
jsonb = 'jsonb',
jsonb = 'jsonp',
}

0 comments on commit cf5894d

Please sign in to comment.