Skip to content

Commit

Permalink
release: qase-cucumberjs 2.0.0-beta.2
Browse files Browse the repository at this point in the history
Added support steps.
  • Loading branch information
gibiw committed May 23, 2024
1 parent c6feac3 commit 517f773
Showing 1 changed file with 105 additions and 16 deletions.
121 changes: 105 additions & 16 deletions qase-cucumberjs/src/reporter.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
import { EventEmitter } from 'events';

import { Formatter, IFormatterOptions, Status } from '@cucumber/cucumber';
import { Envelope, PickleTag, TestCaseStarted } from '@cucumber/messages';
import {
Envelope,
PickleStep,
PickleTag,
TestCaseStarted,
TestStepFinished,
} from '@cucumber/messages';

import {
ConfigType,
composeOptions,
ConfigLoader,
ConfigType,
QaseReporter,
Relation,
ReporterInterface,
StepStatusEnum,
StepType,
TestStatusEnum,
composeOptions,
TestStepType,
} from 'qase-javascript-commons';
import { TestCase } from '@cucumber/messages/dist/esm/src/messages';

type PickleInfoType = {
interface PickleInfoType {
caseIds: number[];
name: string;
lastAstNodeId: string | undefined;
};
steps: readonly PickleStep[];
}

type TestStepResultStatus = (typeof Status)[keyof typeof Status];

Expand All @@ -42,6 +54,19 @@ export class CucumberQaseReporter extends Formatter {
[Status.UNKNOWN]: null,
};

/**
* @type {Record<TestStepResultStatus, StepStatusEnum>}
*/
static stepStatusMap: Record<TestStepResultStatus, StepStatusEnum> = {
[Status.PASSED]: StepStatusEnum.passed,
[Status.FAILED]: StepStatusEnum.failed,
[Status.SKIPPED]: StepStatusEnum.blocked,
[Status.AMBIGUOUS]: StepStatusEnum.blocked,
[Status.PENDING]: StepStatusEnum.blocked,
[Status.UNDEFINED]: StepStatusEnum.blocked,
[Status.UNKNOWN]: StepStatusEnum.blocked,
};

/**
* @type {RegExp}
*/
Expand All @@ -52,7 +77,7 @@ export class CucumberQaseReporter extends Formatter {
* @returns {number[]}
* @private
*/
private static getCaseIds(tagsList: readonly PickleTag[]) {
private static getCaseIds(tagsList: readonly PickleTag[]): number[] {
return tagsList.reduce<number[]>((acc, tagInfo) => {
const ids = Array.from(tagInfo.name.matchAll(CucumberQaseReporter.qaseIdRegExp))
.map(([, id]) => Number(id))
Expand All @@ -78,6 +103,13 @@ export class CucumberQaseReporter extends Formatter {
* @type {Record<string, >}
* @private
*/

/**
* @type {Record<string, TestStepFinished>}
* @private
*/
private testCaseStepsFinished: Record<string, TestStepFinished> = {};

private testCaseStartedResult: Record<string, TestStatusEnum> = {};
/**
* @type {Record<string, string[]>}
Expand All @@ -88,7 +120,7 @@ export class CucumberQaseReporter extends Formatter {
* @type {Record<string, string>}
* @private
*/
private testCaseScenarioId: Record<string, string> = {};
private testCaseScenarioId: Record<string, TestCase> = {};
/**
* @type {Record<string, string>}
* @private
Expand Down Expand Up @@ -157,6 +189,7 @@ export class CucumberQaseReporter extends Formatter {
name: envelope.pickle.name,
lastAstNodeId:
envelope.pickle.astNodeIds[envelope.pickle.astNodeIds.length - 1],
steps: envelope.pickle.steps,
};
} else if (envelope.attachment) {
if (
Expand All @@ -172,7 +205,7 @@ export class CucumberQaseReporter extends Formatter {
void this.publishResults();
} else if (envelope.testCase) {
this.testCaseScenarioId[envelope.testCase.id] =
envelope.testCase.pickleId;
envelope.testCase;
} else if (envelope.testCaseStarted) {
this.testCaseStarts[envelope.testCaseStarted.id] =
envelope.testCaseStarted;
Expand All @@ -188,6 +221,8 @@ export class CucumberQaseReporter extends Formatter {
return;
}

this.testCaseStepsFinished[stepFin.testStepId] = stepFin;

if (newStatus !== TestStatusEnum.passed) {
if (stepFin.testStepResult.message) {
const errors =
Expand Down Expand Up @@ -216,13 +251,13 @@ export class CucumberQaseReporter extends Formatter {
return;
}

const pickleId = this.testCaseScenarioId[tcs.testCaseId];
const testCase = this.testCaseScenarioId[tcs.testCaseId];

if (!pickleId) {
if (!testCase) {
return;
}

const info = this.pickleInfo[pickleId];
const info = this.pickleInfo[testCase.pickleId];

if (!info) {
return;
Expand All @@ -233,6 +268,19 @@ export class CucumberQaseReporter extends Formatter {
if (this.testCaseStartedErrors[tcs.id]?.length) {
error = new Error(this.testCaseStartedErrors[tcs.id]?.join('\n\n'));
}
let relations: Relation | null = null;
if (info.lastAstNodeId != undefined && this.scenarios[info.lastAstNodeId] != undefined) {
relations = {
suite: {
data: [
{
title: this.scenarios[info.lastAstNodeId] ?? '',
public_id: null,
},
],
},
};
}

void this.reporter.addTestResult({
attachments: [],
Expand All @@ -241,8 +289,8 @@ export class CucumberQaseReporter extends Formatter {
status: this.testCaseStartedResult[
envelope.testCaseFinished.testCaseStartedId
] ?? TestStatusEnum.passed,
start_time: tcs.timestamp.seconds,
end_time: envelope.testCaseFinished.timestamp.seconds,
start_time: null,
end_time: null,
duration: Math.abs(
envelope.testCaseFinished.timestamp.seconds - tcs.timestamp.seconds,
),
Expand All @@ -253,10 +301,10 @@ export class CucumberQaseReporter extends Formatter {
message: null,
muted: false,
params: {},
relations: {},
relations: relations,
run_id: null,
signature: '',
steps: [],
steps: this.convertSteps(info.steps, testCase),
testops_id: info.caseIds.length > 0 ? info.caseIds : null,
id: tcs.id,
title: info.name,
Expand All @@ -266,6 +314,47 @@ export class CucumberQaseReporter extends Formatter {
});
}

private convertSteps(steps: readonly PickleStep[], testCase: TestCase): TestStepType[] {
const results: TestStepType[] = [];

for (const s of testCase.testSteps) {
const finished = this.testCaseStepsFinished[s.id];
if (!finished) {
continue;
}

const step = steps.find((step) => step.id === s.pickleStepId);

if (!step) {
continue;
}

const result: TestStepType = {
id: s.id,
step_type: StepType.GHERKIN,
data: {
keyword: step.text,
name: step.text,
line: 0,
},
execution: {
status: CucumberQaseReporter.stepStatusMap[finished.testStepResult.status],
start_time: null,
end_time: null,
duration: finished.testStepResult.duration.seconds,
},
attachments: [],
steps: [],
parent_id: null,
}
;

results.push(result);
}

return results;
}

/**
* @returns {Promise<void>}
* @private
Expand All @@ -279,6 +368,6 @@ export class CucumberQaseReporter extends Formatter {
* @private
*/
private startTestRun(): void {
void this.reporter.startTestRun();
this.reporter.startTestRun();
}
}

0 comments on commit 517f773

Please sign in to comment.