testit-adapter-cucumber 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintignore ADDED
@@ -0,0 +1,2 @@
1
+ dist/*
2
+ demo/*
package/.eslintrc.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "env": {
3
+ "es2021": true,
4
+ "node": true
5
+ },
6
+ "extends": [
7
+ "eslint:recommended",
8
+ "plugin:@typescript-eslint/recommended",
9
+ "plugin:prettier/recommended",
10
+ "prettier"
11
+ ],
12
+ "parser": "@typescript-eslint/parser",
13
+ "parserOptions": {
14
+ "ecmaVersion": 12,
15
+ "sourceType": "module"
16
+ },
17
+ "plugins": ["@typescript-eslint", "prettier"],
18
+ "rules": {
19
+ "no-unused-vars": "off",
20
+ "@typescript-eslint/no-unused-vars": [
21
+ "error",
22
+ { "argsIgnorePattern": "^_" }
23
+ ],
24
+ "prettier/prettier": "warn",
25
+ "@typescript-eslint/explicit-function-return-type": "error"
26
+ }
27
+ }
package/.prettierrc ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "endOfLine": "lf",
3
+ "printWidth": 80,
4
+ "semi": true,
5
+ "singleQuote": true,
6
+ "useTabs": false,
7
+ "trailingComma": "es5",
8
+ "tabWidth": 2
9
+ }
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "testit-adapter-cucumber",
3
+ "version": "1.0.0",
4
+ "description": "Adapter provides formatter for cucumber output and some additional methods to Cucumber world.",
5
+ "main": "dist/formatter.js",
6
+ "scripts": {
7
+ "prebuild": "rimraf dist",
8
+ "build": "tsc",
9
+ "start": "npm run build && cd demo && npm run test",
10
+ "lint": "eslint . --ext=.ts"
11
+ },
12
+ "keywords": [],
13
+ "author": "Andrey Kiselev",
14
+ "license": "Apache-2.0",
15
+ "peerDependencies": {
16
+ "axios": "^0.22.0",
17
+ "minimist": "^1.2.5"
18
+ },
19
+ "dependencies": {
20
+ "@cucumber/cucumber": "^7.3.1",
21
+ "@cucumber/messages": "^17.1.1",
22
+ "testit-api-client": "file:testit-api-client-1.0.0.tgz"
23
+ },
24
+ "devDependencies": {
25
+ "@babel/preset-env": "^7.15.8",
26
+ "@babel/preset-typescript": "^7.15.0",
27
+ "@types/node": "^16.10.3",
28
+ "@typescript-eslint/eslint-plugin": "^5.0.0",
29
+ "@typescript-eslint/parser": "^5.0.0",
30
+ "eslint": "^7.32.0",
31
+ "eslint-config-airbnb-base": "^14.2.1",
32
+ "eslint-config-prettier": "^8.3.0",
33
+ "eslint-plugin-import": "^2.25.1",
34
+ "eslint-plugin-prettier": "^4.0.0",
35
+ "prettier": "^2.4.1",
36
+ "rimraf": "^3.0.2",
37
+ "ts-node": "^10.3.0",
38
+ "typescript": "^4.4.3"
39
+ },
40
+ "bugs": {
41
+ "url": "https://github.com/testit-tms/adapters-js/issues"
42
+ },
43
+ "homepage": "https://github.com/testit-tms/adapters-js"
44
+ }
package/readme.md ADDED
@@ -0,0 +1,78 @@
1
+ # TestIt Cucumber Adapter
2
+
3
+ Adapter provides formatter for cucumber output and some additional methods to Cucumber world.
4
+
5
+ # Installation
6
+
7
+ ```
8
+ npm i <TBD>
9
+ ```
10
+
11
+ or
12
+
13
+ ```
14
+ yarn add <TBD>
15
+ ```
16
+
17
+ # Usage
18
+
19
+ Create `formatter.js` file with
20
+
21
+ ```js
22
+ const { TestItFormatter } = require('<TBD>');
23
+
24
+ module.exports = class CustomFormatter extends TestItFormatter {
25
+ constructor(options) {
26
+ super(options, {
27
+ url: '<url>',
28
+ privateToken: '<token>',
29
+ projectId: '<id>',
30
+ configurationId: '<id>',
31
+ testRunId: '<optional id>',
32
+ });
33
+ }
34
+ };
35
+ ```
36
+
37
+ And fill object with your configuration. Formatter sends results to TestIt.
38
+
39
+ > TestRunId is optional. If it's not provided than it create automatically.
40
+
41
+ Formatter provides additional methods to World:
42
+
43
+ - addMessage - adds message to autotest
44
+ - addLinks - adds links to autotest
45
+ - addAttachments - uploads specified to TestIt and links to test run
46
+
47
+ ```js
48
+ When('Something happens', function () {
49
+ this.addMessage('💔');
50
+ this.addLinks([
51
+ {
52
+ url: 'http://github.com',
53
+ },
54
+ {
55
+ url: 'https://wikipedia.org',
56
+ title: 'Wikipedia',
57
+ description: 'The free encyclopedia',
58
+ type: 'Related',
59
+ hasInfo: true,
60
+ },
61
+ ]);
62
+ this.addAttachments(['path/to/file.txt']);
63
+ });
64
+ ```
65
+
66
+ # Tags
67
+
68
+ Cucumber tags can be used to specify information about autotest.
69
+
70
+ > Only those specified above the `Scenario` are taken into account
71
+
72
+ - `@ExternalId` - Unique identifier of autotest (Required)
73
+ - `@Title` - Title that is displayed on autotest page
74
+ - `@DisplayName` - Name that is displayed in autotests table
75
+ - `@Description` - Autotest description
76
+ - `@Link` - can be specified either in JSON (`@Link={"url":"http://google.com","hasInfo":true,"description":"GoogleDescription","title":"Google","type":"Defect"}`) or in text (`@Link=http://google.com`)
77
+ - `@Label` - Label that is going to be linked to autotest
78
+ - `@WorkItemID` - Work item's ID to which autotest is going to be linked
@@ -0,0 +1,285 @@
1
+ import { Formatter, IFormatterOptions } from '@cucumber/cucumber';
2
+ import {
3
+ Envelope,
4
+ GherkinDocument,
5
+ Pickle,
6
+ TestCase,
7
+ TestStepFinished,
8
+ TestRunFinished,
9
+ TestCaseStarted,
10
+ TestCaseFinished,
11
+ TestStepStarted,
12
+ Meta,
13
+ TestRunStarted,
14
+ } from '@cucumber/messages';
15
+ import {
16
+ Client,
17
+ ClientConfigWithFile,
18
+ IClient,
19
+ LinkPost,
20
+ } from 'testit-api-client';
21
+ import { IStorage } from './types/storage';
22
+ import { Storage } from './storage';
23
+ import { IFormatter } from './types/formatter';
24
+ import { AutotestPostWithWorkItemId } from './mappers';
25
+ import { AxiosError } from 'axios';
26
+
27
+ export class TestItFormatter extends Formatter implements IFormatter {
28
+ client: IClient;
29
+ storage: IStorage = new Storage();
30
+ currentTestCaseId: string | undefined;
31
+
32
+ constructor(
33
+ options: IFormatterOptions,
34
+ config: Partial<ClientConfigWithFile>
35
+ ) {
36
+ super(options);
37
+ this.client = new Client(config);
38
+ options.eventBroadcaster.on('envelope', (envelope: Envelope) => {
39
+ if (envelope.meta) {
40
+ return this.onMeta(envelope.meta);
41
+ }
42
+ if (envelope.gherkinDocument) {
43
+ return this.onGherkinDocument(envelope.gherkinDocument);
44
+ }
45
+ if (envelope.pickle) {
46
+ return this.onPickle(envelope.pickle);
47
+ }
48
+ if (envelope.testCase) {
49
+ return this.onTestCase(envelope.testCase);
50
+ }
51
+ if (envelope.testRunStarted) {
52
+ return this.onTestRunStarted(envelope.testRunStarted);
53
+ }
54
+ if (envelope.testCaseStarted) {
55
+ return this.onTestCaseStarted(envelope.testCaseStarted);
56
+ }
57
+ if (envelope.testStepStarted) {
58
+ return this.testStepStarted(envelope.testStepStarted);
59
+ }
60
+ if (envelope.testStepFinished) {
61
+ return this.onTestStepFinished(envelope.testStepFinished);
62
+ }
63
+ if (envelope.testCaseFinished) {
64
+ return this.testCaseFinished(envelope.testCaseFinished);
65
+ }
66
+ if (envelope.testRunFinished) {
67
+ return this.onTestRunFinished(envelope.testRunFinished);
68
+ }
69
+ });
70
+ options.supportCodeLibrary.World.prototype.addMessage =
71
+ this.addMessage.bind(this);
72
+ options.supportCodeLibrary.World.prototype.addLinks =
73
+ this.addLinks.bind(this);
74
+ options.supportCodeLibrary.World.prototype.addAttachments =
75
+ this.addAttachments.bind(this);
76
+ }
77
+
78
+ private testRunId: Promise<string> | undefined;
79
+ private testRunStarted: Promise<void> | undefined;
80
+ private attachmentsQueue: Promise<void>[] = [];
81
+
82
+ onMeta(_meta: Meta): void {
83
+ const { projectId, testRunId } = this.client.getConfig();
84
+ if (testRunId === undefined) {
85
+ this.testRunId = this.client
86
+ .createTestRun({
87
+ projectId,
88
+ })
89
+ .then((testRun) => testRun.id);
90
+ } else {
91
+ this.testRunId = Promise.resolve(testRunId);
92
+ }
93
+ }
94
+
95
+ onGherkinDocument(document: GherkinDocument): void {
96
+ this.storage.saveGherkinDocument(document);
97
+ }
98
+
99
+ onPickle(pickle: Pickle): void {
100
+ this.storage.savePickle(pickle);
101
+ }
102
+
103
+ onTestRunStarted(_testRunStarted: TestRunStarted): void {
104
+ if (this.testRunId === undefined) {
105
+ throw new Error('TestRunId is not yet specified');
106
+ }
107
+ this.testRunStarted = this.testRunId.then((id) =>
108
+ this.client.startTestRun(id)
109
+ );
110
+ }
111
+
112
+ onTestCase(testCase: TestCase): void {
113
+ this.storage.saveTestCase(testCase);
114
+ }
115
+
116
+ onTestCaseStarted(testCaseStarted: TestCaseStarted): void {
117
+ this.currentTestCaseId = testCaseStarted.testCaseId;
118
+ this.storage.saveTestCaseStarted(testCaseStarted);
119
+ }
120
+
121
+ testStepStarted(testStepStarted: TestStepStarted): void {
122
+ this.storage.saveTestStepStarted(testStepStarted);
123
+ }
124
+
125
+ onTestStepFinished(testStepFinished: TestStepFinished): void {
126
+ this.storage.saveTestStepFinished(testStepFinished);
127
+ }
128
+
129
+ testCaseFinished(testCaseFinished: TestCaseFinished): void {
130
+ this.currentTestCaseId = undefined;
131
+ this.storage.saveTestCaseFinished(testCaseFinished);
132
+ }
133
+
134
+ onTestRunFinished(_testRunFinished: TestRunFinished): void {
135
+ if (this.testRunId === undefined) {
136
+ throw new Error('TestRunId is not yet specified');
137
+ }
138
+ if (this.testRunStarted === undefined) {
139
+ throw new Error('Test run is not started yet');
140
+ }
141
+
142
+ Promise.all([
143
+ this.testRunId,
144
+ this.testRunStarted,
145
+ Promise.all(this.attachmentsQueue),
146
+ ])
147
+ .then(async ([id]) => {
148
+ const { configurationId } = this.client.getConfig();
149
+ const autotests = this.storage.getAutotests(
150
+ this.client.getConfig().projectId
151
+ );
152
+ const results = this.storage.getTestRunResults(configurationId);
153
+ await Promise.all(
154
+ autotests.map((autotestPost) => {
155
+ const result = results.find(
156
+ (result) => result.autotestExternalId === autotestPost.externalId
157
+ );
158
+ if (result === undefined) {
159
+ throw new Error(
160
+ `Cannot find result for ${autotestPost.externalId} autotest`
161
+ );
162
+ }
163
+ if (result.outcome !== 'Passed') {
164
+ return this.loadAutotest(autotestPost);
165
+ }
166
+ return this.loadPassedAutotest(autotestPost);
167
+ })
168
+ );
169
+ return this.client.loadTestRunResults(id, results);
170
+ })
171
+ .catch((err) => {
172
+ console.error(err);
173
+ this.testRunId?.then((id) => this.client.completeTestRun(id));
174
+ });
175
+ }
176
+
177
+ async loadAutotest(autotestPost: AutotestPostWithWorkItemId): Promise<void> {
178
+ try {
179
+ await this.createNewAutotest(autotestPost);
180
+ } catch (err) {
181
+ const axiosError = err as AxiosError;
182
+
183
+ if (axiosError.response?.status === 409) {
184
+ const [autotest] = await this.client.getAutotest({
185
+ projectId: this.client.getConfig().projectId,
186
+ externalId: autotestPost.externalId,
187
+ });
188
+ await this.updateAutotest({
189
+ ...autotest,
190
+ links: autotestPost.links,
191
+ });
192
+ } else {
193
+ this.logError(axiosError);
194
+ }
195
+ }
196
+ }
197
+
198
+ async loadPassedAutotest(
199
+ autotestPost: AutotestPostWithWorkItemId
200
+ ): Promise<void> {
201
+ try {
202
+ await this.createNewAutotest(autotestPost);
203
+ } catch (err) {
204
+ const axiosError = err as AxiosError;
205
+ if (axiosError.response?.status === 409) {
206
+ await this.updateAutotest(autotestPost);
207
+ } else {
208
+ this.logError(axiosError);
209
+ }
210
+ }
211
+
212
+ if (autotestPost.workItemId !== undefined) {
213
+ this.linkWorkItem(autotestPost.externalId, autotestPost.workItemId);
214
+ }
215
+ }
216
+
217
+ async createNewAutotest(
218
+ autotestPost: AutotestPostWithWorkItemId
219
+ ): Promise<void> {
220
+ await this.client.createAutotest(autotestPost);
221
+ }
222
+
223
+ async updateAutotest(
224
+ autotestPost: AutotestPostWithWorkItemId
225
+ ): Promise<void> {
226
+ await this.client.updateAutotest(autotestPost).catch(this.logError);
227
+ }
228
+
229
+ async linkWorkItem(externalId: string, workItemId: string): Promise<void> {
230
+ const [autotest] = await this.client
231
+ .getAutotest({
232
+ projectId: this.client.getConfig().projectId,
233
+ externalId: externalId,
234
+ })
235
+ .catch(() => []);
236
+ if (autotest?.id !== undefined) {
237
+ await this.client.linkToWorkItem(autotest.id, {
238
+ id: workItemId,
239
+ });
240
+ }
241
+ }
242
+
243
+ addMessage(message: string): void {
244
+ if (this.currentTestCaseId === undefined) {
245
+ throw new Error('CurrentTestCaseId is not set');
246
+ }
247
+ this.storage.addMessage(this.currentTestCaseId, message);
248
+ }
249
+
250
+ addLinks(links: LinkPost[]): void {
251
+ if (this.currentTestCaseId === undefined) {
252
+ throw new Error('CurrentTestCaseId is not set');
253
+ }
254
+ this.storage.addLinks(this.currentTestCaseId, links);
255
+ }
256
+
257
+ addAttachments(attachments: string[]): void {
258
+ if (this.currentTestCaseId === undefined) {
259
+ throw new Error('CurrentTestCaseId is not set');
260
+ }
261
+ const currentTestCaseId = this.currentTestCaseId;
262
+ this.attachmentsQueue.push(
263
+ ...attachments.map(async (attachment) => {
264
+ const { id } = await this.client.loadAttachment(attachment);
265
+ if (id === undefined) {
266
+ // NOTE: Why?
267
+ console.warn('Attachment id is not returned');
268
+ return;
269
+ }
270
+ this.storage.addAttachment(currentTestCaseId, id);
271
+ })
272
+ );
273
+ }
274
+
275
+ private logError(err: AxiosError): void {
276
+ console.error(
277
+ err.response?.status,
278
+ err.config.method,
279
+ err.config.url,
280
+ err.response?.data
281
+ );
282
+ }
283
+
284
+ // TODO: add stopping run on error
285
+ }
package/src/mappers.ts ADDED
@@ -0,0 +1,174 @@
1
+ import {
2
+ Background,
3
+ DataTable,
4
+ Examples,
5
+ GherkinDocument,
6
+ Rule,
7
+ Scenario,
8
+ Step,
9
+ TestStepResultStatus,
10
+ } from '@cucumber/messages';
11
+ import { AutotestPost, AutotestStep, OutcomeType } from 'testit-api-client';
12
+ import { parseTags } from './utils';
13
+
14
+ export interface AutotestPostWithWorkItemId extends AutotestPost {
15
+ workItemId?: string;
16
+ }
17
+
18
+ export function mapStatus(status: TestStepResultStatus): OutcomeType {
19
+ switch (status) {
20
+ case TestStepResultStatus.PASSED:
21
+ return 'Passed';
22
+ case TestStepResultStatus.FAILED:
23
+ return 'Failed';
24
+ case TestStepResultStatus.PENDING:
25
+ return 'Pending';
26
+ case TestStepResultStatus.SKIPPED:
27
+ return 'Skipped';
28
+ case TestStepResultStatus.UNKNOWN:
29
+ case TestStepResultStatus.UNDEFINED:
30
+ case TestStepResultStatus.AMBIGUOUS:
31
+ return 'Blocked';
32
+ default:
33
+ throw new Error('Unknown status');
34
+ }
35
+ }
36
+
37
+ export function mapDate(date: number): string {
38
+ return new Date(date * 1000).toISOString();
39
+ }
40
+
41
+ export function mapDocument(
42
+ document: GherkinDocument,
43
+ projectId: string
44
+ ): AutotestPostWithWorkItemId[] {
45
+ if (document.feature === undefined) {
46
+ return [];
47
+ }
48
+
49
+ const setup = document.feature.children
50
+ .filter((child) => child.background !== undefined)
51
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
52
+ .map((child) => mapBackground(child.background!));
53
+ const scenarioAutotests = document.feature.children
54
+ .filter((child) => child.scenario !== undefined)
55
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
56
+ .map((child) => mapScenario(child.scenario!, projectId, setup));
57
+ const ruleAutotests = document.feature.children
58
+ .filter((child) => child.rule !== undefined)
59
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
60
+ .flatMap((child) => mapRule(child.rule!, projectId, setup));
61
+
62
+ return scenarioAutotests.concat(...ruleAutotests);
63
+ }
64
+
65
+ export function mapBackground(background: Background): AutotestStep {
66
+ return {
67
+ title: background.name,
68
+ description: background.description,
69
+ steps: background.steps.map(mapStep),
70
+ };
71
+ }
72
+
73
+ export function mapRule(
74
+ rule: Rule,
75
+ projectId: string,
76
+ setup: AutotestStep[]
77
+ ): AutotestPostWithWorkItemId[] {
78
+ const ruleSetup = setup.concat(
79
+ rule.children
80
+ .filter((child) => child.background !== undefined)
81
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
82
+ .map((child) => mapBackground(child.background!))
83
+ );
84
+ return (
85
+ rule.children
86
+ .filter((child) => child.scenario !== undefined)
87
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
88
+ .map((child) => mapScenario(child.scenario!, projectId, ruleSetup))
89
+ );
90
+ }
91
+
92
+ export function mapScenario(
93
+ scenario: Scenario,
94
+ projectId: string,
95
+ setup: AutotestStep[]
96
+ ): AutotestPostWithWorkItemId {
97
+ const tags = parseTags(scenario.tags);
98
+ if (tags.externalId === undefined) {
99
+ throw new Error(`External id is not provided in ${scenario.name}`);
100
+ }
101
+ const exampleSteps = scenario.examples.map(mapExamples);
102
+ return {
103
+ setup: exampleSteps.concat(setup),
104
+ externalId: tags.externalId,
105
+ links: tags.links,
106
+ name: tags.name ?? scenario.name,
107
+ title: tags.title,
108
+ description: tags.description ?? scenario.description,
109
+ projectId,
110
+ steps: scenario.steps.map(mapStep),
111
+ workItemId: tags.workItemId,
112
+ // Disable for now (BUG??)
113
+ // labels:
114
+ // tags.labels.length > 0
115
+ // ? tags.labels.map((label) => ({ name: label }))
116
+ // : undefined,
117
+ };
118
+ }
119
+
120
+ export function mapExamples(examples: Examples): AutotestStep {
121
+ let table: string[][] | Record<string, string>[] = [];
122
+ const body = examples.tableBody.map((row) =>
123
+ row.cells.map((cell) => cell.value)
124
+ );
125
+ if (examples.tableHeader !== undefined) {
126
+ const header = examples.tableHeader?.cells.map((cell) => cell.value);
127
+ table = body.map((row) =>
128
+ header.reduce((acc, key, i) => {
129
+ acc[key] = row[i];
130
+ return acc;
131
+ }, {} as Record<string, string>)
132
+ );
133
+ } else {
134
+ table = body;
135
+ }
136
+ const description = [examples.description];
137
+
138
+ const tags = parseTags(examples.tags);
139
+
140
+ if (table.length > 0) {
141
+ description.push(JSON.stringify(table));
142
+ }
143
+
144
+ return {
145
+ title: tags.title ?? tags.name ?? examples.name,
146
+ description: tags.description ?? description.join('\n\n'),
147
+ };
148
+ }
149
+
150
+ export function mapStep(step: Step): AutotestStep {
151
+ return {
152
+ title: `${step.keyword} ${step.text}`,
153
+ description: step.docString?.content ?? mapDataTable(step.dataTable),
154
+ };
155
+ }
156
+
157
+ export function mapDataTable(
158
+ dataTable: DataTable | undefined
159
+ ): string | undefined {
160
+ if (dataTable === undefined) {
161
+ return undefined;
162
+ }
163
+ const keys = dataTable.rows[0].cells.map((cell) => cell.value);
164
+ const rows = dataTable.rows
165
+ .slice(1)
166
+ .map((row) => row.cells.map((cell) => cell.value));
167
+ const objects = rows.map((value) =>
168
+ keys.reduce((acc, key, i) => {
169
+ acc[key] = value[i];
170
+ return acc;
171
+ }, {} as Record<string, string>)
172
+ );
173
+ return JSON.stringify(objects);
174
+ }
package/src/storage.ts ADDED
@@ -0,0 +1,212 @@
1
+ import {
2
+ AttachmentPut,
3
+ AttachmentPutModelAutotestStepResults,
4
+ AutotestResultsForTestRun,
5
+ LinkPost,
6
+ } from 'testit-api-client';
7
+ import {
8
+ GherkinDocument,
9
+ Pickle,
10
+ PickleStep,
11
+ TestCase,
12
+ TestCaseFinished,
13
+ TestCaseStarted,
14
+ TestStepFinished,
15
+ TestStepStarted,
16
+ } from '@cucumber/messages';
17
+ import { IStorage } from './types/storage';
18
+ import {
19
+ AutotestPostWithWorkItemId,
20
+ mapDate,
21
+ mapDocument,
22
+ mapStatus,
23
+ } from './mappers';
24
+ import { calculateResultOutcome, parseTags } from './utils';
25
+
26
+ export class Storage implements IStorage {
27
+ private gherkinDocuments: GherkinDocument[] = [];
28
+ private pickles: Pickle[] = [];
29
+ private testCases: TestCase[] = [];
30
+ private testCasesStarted: TestCaseStarted[] = [];
31
+ private testCasesFinished: TestCaseFinished[] = [];
32
+ private testStepsStarted: TestStepStarted[] = [];
33
+ private testStepsFinished: TestStepFinished[] = [];
34
+ private messages: Record<string, string[]> = {};
35
+ private links: Record<string, LinkPost[]> = {};
36
+ private attachments: Record<string, string[]> = {};
37
+
38
+ saveGherkinDocument(document: GherkinDocument): void {
39
+ this.gherkinDocuments.push(document);
40
+ }
41
+ getAutotests(projectId: string): AutotestPostWithWorkItemId[] {
42
+ return this.gherkinDocuments.flatMap((document) =>
43
+ mapDocument(document, projectId)
44
+ );
45
+ }
46
+ savePickle(pickle: Pickle): void {
47
+ this.pickles.push(pickle);
48
+ }
49
+ saveTestCase(testCase: TestCase): void {
50
+ this.testCases.push(testCase);
51
+ }
52
+ saveTestCaseStarted(testCaseStarted: TestCaseStarted): void {
53
+ this.testCasesStarted.push(testCaseStarted);
54
+ }
55
+ saveTestCaseFinished(testCaseFinished: TestCaseFinished): void {
56
+ this.testCasesFinished.push(testCaseFinished);
57
+ }
58
+ saveTestStepStarted(testStepStarted: TestStepStarted): void {
59
+ this.testStepsStarted.push(testStepStarted);
60
+ }
61
+ saveTestStepFinished(testStepFinished: TestStepFinished): void {
62
+ this.testStepsFinished.push(testStepFinished);
63
+ }
64
+ getTestRunResults(configurationId: string): AutotestResultsForTestRun[] {
65
+ const results: AutotestResultsForTestRun[] = [];
66
+ for (const pickle of this.pickles) {
67
+ const tags = parseTags(pickle.tags);
68
+ if (tags.externalId === undefined) {
69
+ throw new Error('External id is not provided');
70
+ }
71
+ const testCase = this.testCases.find(
72
+ (testCase) => testCase.pickleId === pickle.id
73
+ );
74
+ if (testCase === undefined) {
75
+ throw new Error('TestCase not found');
76
+ }
77
+ const testCaseStarted = this.testCasesStarted.find(
78
+ (testCase) => testCase.id === testCase.id
79
+ );
80
+ if (testCaseStarted === undefined) {
81
+ throw new Error('TestCaseStarted not found');
82
+ }
83
+ const testCaseFinished = this.testCasesFinished.find(
84
+ (testCase) => testCase.testCaseStartedId === testCaseStarted.id
85
+ );
86
+ if (testCaseFinished === undefined) {
87
+ throw new Error('TestCaseFinished not found');
88
+ }
89
+ const steps = pickle.steps
90
+ .map((step) => this.getStepResult(step, testCase))
91
+ .filter((item, i, arr) => {
92
+ const prevOutcome = arr[i - 1]?.outcome;
93
+ if (
94
+ item.outcome === 'Skipped' &&
95
+ prevOutcome !== undefined &&
96
+ ['Failed', 'Skipped'].includes(prevOutcome)
97
+ ) {
98
+ return false;
99
+ }
100
+ return true;
101
+ });
102
+ const messages: string[] = [];
103
+ for (const step of pickle.steps) {
104
+ const message = this.getStepMessage(step, testCase);
105
+ if (message !== undefined) {
106
+ messages.push(message);
107
+ }
108
+ }
109
+ const links = this.links[testCase.id] ?? [];
110
+ links.push(...tags.links);
111
+ const result: AutotestResultsForTestRun = {
112
+ autotestExternalId: tags.externalId,
113
+ configurationId,
114
+ links,
115
+ stepResults: steps,
116
+ outcome: calculateResultOutcome(steps.map((step) => step.outcome)),
117
+ startedOn: mapDate(testCaseStarted.timestamp.seconds),
118
+ completeOn: mapDate(testCaseFinished.timestamp.seconds),
119
+ duration:
120
+ testCaseFinished.timestamp.seconds -
121
+ testCaseStarted.timestamp.seconds,
122
+ message: this.messages[testCase.id]?.join('\n\n') ?? undefined,
123
+ traces: messages.join('\n\n'),
124
+ attachments: this.getAttachments(testCase.id),
125
+ };
126
+ results.push(result);
127
+ }
128
+
129
+ return results;
130
+ }
131
+ getStepResult(
132
+ pickleStep: PickleStep,
133
+ testCase: TestCase
134
+ ): AttachmentPutModelAutotestStepResults {
135
+ const testStep = testCase.testSteps.find(
136
+ (step) => step.pickleStepId === pickleStep.id
137
+ );
138
+ if (testStep === undefined) {
139
+ throw new Error('TestCase step not found');
140
+ }
141
+ const testStepStarted = this.testStepsStarted.find(
142
+ (step) => step.testStepId === testStep.id
143
+ );
144
+ if (testStepStarted === undefined) {
145
+ throw new Error('TestStepStarted not found');
146
+ }
147
+ const testStepFinished = this.testStepsFinished.find(
148
+ (step) => step.testStepId === testStepStarted.testStepId
149
+ );
150
+ if (testStepFinished === undefined) {
151
+ throw new Error('TestStepFinished not found');
152
+ }
153
+ return {
154
+ title: pickleStep.text,
155
+ startedOn: mapDate(testStepStarted.timestamp.seconds),
156
+ duration: testStepFinished.testStepResult.duration.seconds,
157
+ completedOn: mapDate(testStepFinished.timestamp.seconds),
158
+ outcome: mapStatus(testStepFinished.testStepResult.status),
159
+ };
160
+ }
161
+ getStepMessage(
162
+ pickleStep: PickleStep,
163
+ testCase: TestCase
164
+ ): string | undefined {
165
+ const testStep = testCase.testSteps.find(
166
+ (step) => step.pickleStepId === pickleStep.id
167
+ );
168
+ if (testStep === undefined) {
169
+ throw new Error('TestCase step not found');
170
+ }
171
+ const testStepStarted = this.testStepsStarted.find(
172
+ (step) => step.testStepId === testStep.id
173
+ );
174
+ if (testStepStarted === undefined) {
175
+ throw new Error('TestStepStarted not found');
176
+ }
177
+ const testStepFinished = this.testStepsFinished.find(
178
+ (step) => step.testStepId === testStepStarted.testStepId
179
+ );
180
+ if (testStepFinished === undefined) {
181
+ throw new Error('TestStepFinished not found');
182
+ }
183
+ return testStepFinished.testStepResult.message;
184
+ }
185
+ getAttachments(testCaseId: string): AttachmentPut[] | undefined {
186
+ if (this.attachments[testCaseId] === undefined) {
187
+ return undefined;
188
+ }
189
+ return this.attachments[testCaseId].map((id) => ({ id }));
190
+ }
191
+ addMessage(testCaseId: string, message: string): void {
192
+ if (this.messages[testCaseId] === undefined) {
193
+ this.messages[testCaseId] = [message];
194
+ } else {
195
+ this.messages[testCaseId].push(message);
196
+ }
197
+ }
198
+ addLinks(testCaseId: string, links: LinkPost[]): void {
199
+ if (this.links[testCaseId] === undefined) {
200
+ this.links[testCaseId] = links;
201
+ } else {
202
+ this.links[testCaseId].push(...links);
203
+ }
204
+ }
205
+ addAttachment(testCaseId: string, attachmentId: string): void {
206
+ if (this.attachments[testCaseId] === undefined) {
207
+ this.attachments[testCaseId] = [attachmentId];
208
+ } else {
209
+ this.attachments[testCaseId].concat(attachmentId);
210
+ }
211
+ }
212
+ }
@@ -0,0 +1,29 @@
1
+ import {
2
+ GherkinDocument,
3
+ Meta,
4
+ Pickle,
5
+ TestCase,
6
+ TestCaseFinished,
7
+ TestCaseStarted,
8
+ TestRunFinished,
9
+ TestRunStarted,
10
+ TestStepFinished,
11
+ TestStepStarted,
12
+ } from '@cucumber/messages';
13
+ import { IClient } from 'testit-api-client';
14
+ import { IStorage } from './storage';
15
+
16
+ export interface IFormatter {
17
+ client: IClient;
18
+ storage: IStorage;
19
+ onMeta(meta: Meta): void;
20
+ onGherkinDocument(document: GherkinDocument): void;
21
+ onPickle(pickle: Pickle): void;
22
+ onTestRunStarted(testRunStarted: TestRunStarted): void;
23
+ onTestCase(testCase: TestCase): void;
24
+ onTestCaseStarted(testCaseStarted: TestCaseStarted): void;
25
+ testStepStarted(testStepStarted: TestStepStarted): void;
26
+ onTestStepFinished(testStepFinished: TestStepFinished): void;
27
+ testCaseFinished(testCaseFinished: TestCaseFinished): void;
28
+ onTestRunFinished(_testRunFinished: TestRunFinished): void;
29
+ }
@@ -0,0 +1,26 @@
1
+ import {
2
+ GherkinDocument,
3
+ Pickle,
4
+ TestCase,
5
+ TestCaseFinished,
6
+ TestCaseStarted,
7
+ TestStepFinished,
8
+ TestStepStarted,
9
+ } from '@cucumber/messages';
10
+ import { AutotestResultsForTestRun, LinkPost } from 'testit-api-client';
11
+ import { AutotestPostWithWorkItemId } from '../mappers';
12
+
13
+ export interface IStorage {
14
+ saveGherkinDocument(document: GherkinDocument): void;
15
+ getAutotests(projectId: string): AutotestPostWithWorkItemId[];
16
+ savePickle(pickle: Pickle): void;
17
+ saveTestCase(testCase: TestCase): void;
18
+ saveTestCaseStarted(testCaseStarted: TestCaseStarted): void;
19
+ saveTestCaseFinished(testCaseFinished: TestCaseFinished): void;
20
+ saveTestStepStarted(testStepStarted: TestStepStarted): void;
21
+ saveTestStepFinished(testStepFinished: TestStepFinished): void;
22
+ getTestRunResults(configurationId: string): AutotestResultsForTestRun[];
23
+ addMessage(testCaseId: string, message: string): void;
24
+ addLinks(testCaseId: string, links: LinkPost[]): void;
25
+ addAttachment(testCaseId: string, attachmentId: string): void;
26
+ }
@@ -0,0 +1,33 @@
1
+ import { Link } from 'testit-api-client';
2
+
3
+ export type ParsedTags = {
4
+ externalId?: string;
5
+ links: Omit<Link, 'id'>[];
6
+ title?: string;
7
+ workItemId?: string;
8
+ name?: string;
9
+ description?: string;
10
+ labels: string[];
11
+ };
12
+
13
+ export enum TagType {
14
+ ExternalId,
15
+ LinkUrl,
16
+ Link,
17
+ Title,
18
+ WorkItemId,
19
+ Name,
20
+ Description,
21
+ Label,
22
+ Unknown,
23
+ }
24
+
25
+ export const tags: Record<keyof ParsedTags, string> = {
26
+ externalId: 'ExternalId',
27
+ links: 'Link',
28
+ title: 'Title',
29
+ name: 'DisplayName',
30
+ workItemId: 'WorkItemId',
31
+ description: 'Description',
32
+ labels: 'Label',
33
+ };
@@ -0,0 +1,8 @@
1
+ import { World } from '@cucumber/cucumber';
2
+ import { LinkPost } from 'testit-api-client';
3
+
4
+ export interface TestItWorld extends World {
5
+ addMessage(message: string): void;
6
+ addLinks(links: LinkPost[]): void;
7
+ addAttachments(attachments: string[]): void;
8
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,127 @@
1
+ import { Tag } from '@cucumber/messages';
2
+ import { Link, OutcomeType } from 'testit-api-client';
3
+ import { ParsedTags, tags, TagType } from './types/tags';
4
+
5
+ export function getTagType(tag: string): TagType {
6
+ if (new RegExp(`^@${tags.externalId}=.+$`).test(tag)) {
7
+ return TagType.ExternalId;
8
+ }
9
+ if (new RegExp(`^@${tags.links}=.+$`).test(tag)) {
10
+ // Check if it is JSON
11
+ if (tag.endsWith('}')) {
12
+ return TagType.Link;
13
+ }
14
+ return TagType.LinkUrl;
15
+ }
16
+ if (new RegExp(`^@${tags.title}=.+$`).test(tag)) {
17
+ return TagType.Title;
18
+ }
19
+ if (new RegExp(`^@${tags.workItemId}=.+$`).test(tag)) {
20
+ return TagType.WorkItemId;
21
+ }
22
+ if (new RegExp(`^@${tags.name}=.+$`).test(tag)) {
23
+ return TagType.Name;
24
+ }
25
+ if (new RegExp(`^@${tags.description}=.+$`).test(tag)) {
26
+ return TagType.Description;
27
+ }
28
+ if (new RegExp(`^@${tags.labels}=.+$`).test(tag)) {
29
+ return TagType.Label;
30
+ }
31
+ return TagType.Unknown;
32
+ }
33
+
34
+ export function getExternalId(tag: string): string {
35
+ return tag.replace(new RegExp(`^@${tags.externalId}=`), '');
36
+ }
37
+
38
+ export function getLinkUrl(tag: string): string {
39
+ return tag.replace(new RegExp(`^@${tags.links}=`), '');
40
+ }
41
+
42
+ export function getLink(tag: string): Omit<Link, 'id'> {
43
+ const linkData: Omit<Link, 'id'> = JSON.parse(getLinkUrl(tag));
44
+ return linkData;
45
+ }
46
+
47
+ export function getTitle(tag: string): string {
48
+ return tag.replace(new RegExp(`^@${tags.title}=`), '');
49
+ }
50
+
51
+ export function getWorkItemId(tag: string): string {
52
+ return tag.replace(new RegExp(`^@${tags.workItemId}=`), '');
53
+ }
54
+
55
+ export function getName(tag: string): string {
56
+ return tag.replace(new RegExp(`^@${tags.name}=`), '');
57
+ }
58
+
59
+ export function getDescription(tag: string): string {
60
+ return tag.replace(new RegExp(`^@${tags.description}=`), '');
61
+ }
62
+
63
+ export function getLabel(tag: string): string {
64
+ return tag.replace(new RegExp(`^@${tags.labels}=`), '');
65
+ }
66
+
67
+ export function parseTags(tags: readonly Pick<Tag, 'name'>[]): ParsedTags {
68
+ const parsedTags: ParsedTags = { links: [], labels: [] };
69
+ for (const tag of tags) {
70
+ switch (getTagType(tag.name)) {
71
+ case TagType.ExternalId:
72
+ parsedTags.externalId = getExternalId(tag.name);
73
+ continue;
74
+ case TagType.LinkUrl: {
75
+ parsedTags.links?.push({ url: getLinkUrl(tag.name) });
76
+ continue;
77
+ }
78
+ case TagType.Link: {
79
+ parsedTags.links?.push(getLink(tag.name));
80
+ continue;
81
+ }
82
+ case TagType.Title: {
83
+ parsedTags.title = getTitle(tag.name);
84
+ continue;
85
+ }
86
+ case TagType.WorkItemId: {
87
+ parsedTags.workItemId = getWorkItemId(tag.name);
88
+ continue;
89
+ }
90
+ case TagType.Name: {
91
+ parsedTags.name = getName(tag.name);
92
+ continue;
93
+ }
94
+ case TagType.Description: {
95
+ parsedTags.description = getDescription(tag.name);
96
+ continue;
97
+ }
98
+ case TagType.Label: {
99
+ parsedTags.labels?.push(getLabel(tag.name));
100
+ continue;
101
+ }
102
+ case TagType.Unknown:
103
+ continue;
104
+ default:
105
+ throw new Error('Unknown tag type');
106
+ }
107
+ }
108
+ return parsedTags;
109
+ }
110
+
111
+ export function calculateResultOutcome(
112
+ outcomes: (OutcomeType | undefined)[]
113
+ ): OutcomeType {
114
+ if (outcomes.some((outcome) => outcome === 'Failed')) {
115
+ return 'Failed';
116
+ }
117
+ if (outcomes.some((outcome) => outcome === 'Blocked')) {
118
+ return 'Blocked';
119
+ }
120
+ if (outcomes.some((outcome) => outcome === 'Skipped')) {
121
+ return 'Skipped';
122
+ }
123
+ if (outcomes.every((outcome) => outcome === 'Passed')) {
124
+ return 'Passed';
125
+ }
126
+ throw new Error('Cannot calculate result outcome');
127
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,101 @@
1
+ {
2
+ "compilerOptions": {
3
+ /* Visit https://aka.ms/tsconfig.json to read more about this file */
4
+
5
+ /* Projects */
6
+ // "incremental": true, /* Enable incremental compilation */
7
+ // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
8
+ // "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */
9
+ // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */
10
+ // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
11
+ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
12
+
13
+ /* Language and Environment */
14
+ "target": "ES2017" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
15
+ // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
16
+ // "jsx": "preserve", /* Specify what JSX code is generated. */
17
+ // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
18
+ // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
19
+ // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */
20
+ // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
21
+ // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */
22
+ // "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */
23
+ // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
24
+ // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
25
+
26
+ /* Modules */
27
+ "module": "commonjs" /* Specify what module code is generated. */,
28
+ // "rootDir": "./", /* Specify the root folder within your source files. */
29
+ // "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
30
+ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
31
+ // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
32
+ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
33
+ // "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
34
+ // "types": [], /* Specify type package names to be included without being referenced in a source file. */
35
+ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
36
+ // "resolveJsonModule": true, /* Enable importing .json files */
37
+ // "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
38
+
39
+ /* JavaScript Support */
40
+ // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
41
+ // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
42
+ // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
43
+
44
+ /* Emit */
45
+ "declaration": true /* Generate .d.ts files from TypeScript and JavaScript files in your project. */,
46
+ // "declarationMap": true, /* Create sourcemaps for d.ts files. */
47
+ // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
48
+ "sourceMap": true /* Create source map files for emitted JavaScript files. */,
49
+ // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
50
+ "outDir": "./dist" /* Specify an output folder for all emitted files. */,
51
+ // "removeComments": true, /* Disable emitting comments. */
52
+ // "noEmit": true, /* Disable emitting files from a compilation. */
53
+ // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
54
+ // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
55
+ // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
56
+ // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
57
+ // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
58
+ // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
59
+ // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
60
+ // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
61
+ // "newLine": "crlf", /* Set the newline character for emitting files. */
62
+ // "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
63
+ // "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
64
+ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
65
+ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
66
+ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */
67
+
68
+ /* Interop Constraints */
69
+ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
70
+ // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
71
+ "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
72
+ // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
73
+ "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
74
+
75
+ /* Type Checking */
76
+ "strict": true /* Enable all strict type-checking options. */,
77
+ // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
78
+ // "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
79
+ // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
80
+ // "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */
81
+ // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
82
+ // "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */
83
+ // "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
84
+ // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
85
+ // "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */
86
+ // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */
87
+ // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
88
+ // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
89
+ // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
90
+ // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
91
+ // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
92
+ // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
93
+ // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
94
+ // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
95
+
96
+ /* Completeness */
97
+ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
98
+ "skipLibCheck": true /* Skip type checking all .d.ts files. */
99
+ },
100
+ "include": ["src"]
101
+ }