allure-cucumberjs 2.0.0-beta.9 → 2.1.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.
Files changed (43) hide show
  1. package/README.md +188 -48
  2. package/dist/index.d.ts +2 -1
  3. package/dist/index.js +3 -2
  4. package/dist/index.js.map +1 -1
  5. package/dist/src/CucumberAllureWorld.d.ts +25 -0
  6. package/dist/src/CucumberAllureWorld.js +102 -0
  7. package/dist/src/CucumberAllureWorld.js.map +1 -0
  8. package/dist/src/CucumberJSAllureReporter.d.ts +52 -67
  9. package/dist/src/CucumberJSAllureReporter.js +348 -222
  10. package/dist/src/CucumberJSAllureReporter.js.map +1 -1
  11. package/package.json +39 -28
  12. package/LICENSE.txt +0 -201
  13. package/dist/src/CucumberAllureInterface.d.ts +0 -24
  14. package/dist/src/CucumberAllureInterface.js +0 -89
  15. package/dist/src/CucumberAllureInterface.js.map +0 -1
  16. package/dist/src/events/Example.d.ts +0 -8
  17. package/dist/src/events/Example.js +0 -26
  18. package/dist/src/events/Example.js.map +0 -1
  19. package/dist/src/events/GherkinDocument.d.ts +0 -17
  20. package/dist/src/events/GherkinDocument.js +0 -10
  21. package/dist/src/events/GherkinDocument.js.map +0 -1
  22. package/dist/src/events/GherkinExample.d.ts +0 -15
  23. package/dist/src/events/GherkinExample.js +0 -7
  24. package/dist/src/events/GherkinExample.js.map +0 -1
  25. package/dist/src/events/GherkinStep.d.ts +0 -17
  26. package/dist/src/events/GherkinStep.js +0 -7
  27. package/dist/src/events/GherkinStep.js.map +0 -1
  28. package/dist/src/events/GherkinTestCase.d.ts +0 -18
  29. package/dist/src/events/GherkinTestCase.js +0 -11
  30. package/dist/src/events/GherkinTestCase.js.map +0 -1
  31. package/dist/src/events/Result.d.ts +0 -5
  32. package/dist/src/events/Result.js +0 -7
  33. package/dist/src/events/Result.js.map +0 -1
  34. package/dist/src/events/SourceLocation.d.ts +0 -11
  35. package/dist/src/events/SourceLocation.js +0 -10
  36. package/dist/src/events/SourceLocation.js.map +0 -1
  37. package/dist/src/events/TestHookDefinition.d.ts +0 -8
  38. package/dist/src/events/TestHookDefinition.js +0 -3
  39. package/dist/src/events/TestHookDefinition.js.map +0 -1
  40. package/dist/src/utilities.d.ts +0 -7
  41. package/dist/src/utilities.js +0 -50
  42. package/dist/src/utilities.js.map +0 -1
  43. package/package-lock.json +0 -14
@@ -1,40 +1,76 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __importDefault = (this && this.__importDefault) || function (mod) {
22
+ return (mod && mod.__esModule) ? mod : { "default": mod };
23
+ };
2
24
  Object.defineProperty(exports, "__esModule", { value: true });
3
25
  exports.CucumberJSAllureFormatter = exports.CucumberJSAllureFormatterConfig = exports.Allure = void 0;
4
- const cucumber_1 = require("cucumber");
26
+ const os_1 = __importDefault(require("os"));
27
+ const process_1 = __importDefault(require("process"));
28
+ const cucumber_1 = require("@cucumber/cucumber");
29
+ const messages = __importStar(require("@cucumber/messages"));
30
+ const messages_1 = require("@cucumber/messages");
5
31
  const allure_js_commons_1 = require("allure-js-commons");
6
- const SourceLocation_1 = require("./events/SourceLocation");
7
- const Example_1 = require("./events/Example");
8
- const CucumberAllureInterface_1 = require("./CucumberAllureInterface");
9
- const utilities_1 = require("./utilities");
10
- var allure_js_commons_2 = require("allure-js-commons");
11
- Object.defineProperty(exports, "Allure", { enumerable: true, get: function () { return allure_js_commons_2.Allure; } });
32
+ Object.defineProperty(exports, "Allure", { enumerable: true, get: function () { return allure_js_commons_1.Allure; } });
33
+ const internal_1 = require("allure-js-commons/internal");
34
+ const CucumberAllureWorld_1 = require("./CucumberAllureWorld");
12
35
  class CucumberJSAllureFormatterConfig {
13
36
  }
14
37
  exports.CucumberJSAllureFormatterConfig = CucumberJSAllureFormatterConfig;
38
+ const { ALLURE_HOST_NAME, ALLURE_THREAD_NAME } = process_1.default.env;
15
39
  class CucumberJSAllureFormatter extends cucumber_1.Formatter {
16
40
  constructor(options, allureRuntime, config) {
17
41
  super(options);
18
42
  this.allureRuntime = allureRuntime;
43
+ this.currentTestsMap = new Map();
44
+ this.hostname = ALLURE_HOST_NAME || os_1.default.hostname();
45
+ this.documentMap = new Map();
46
+ this.scenarioMap = new Map();
47
+ this.stepMap = new Map();
48
+ this.testStepMap = new Map();
49
+ this.pickleStepMap = new Map();
50
+ this.testCaseTestStepsResults = new Map();
51
+ this.pickleMap = new Map();
52
+ this.hookMap = new Map();
19
53
  this.sourceMap = new Map();
20
- this.stepsMap = new Map();
21
- this.featureMap = new Map();
22
- this.stepStack = [];
23
- this.currentGroup = null;
24
- this.currentTest = null;
25
- this.currentBefore = null;
26
- this.currentAfter = null;
27
- options.eventBroadcaster
28
- .on("source", this.onSource.bind(this))
29
- .on("gherkin-document", this.onGherkinDocument.bind(this))
30
- .on("test-case-prepared", this.onTestCasePrepared.bind(this))
31
- .on("test-case-started", this.onTestCaseStarted.bind(this))
32
- .on("test-step-started", this.onTestStepStarted.bind(this))
33
- .on("test-step-attachment", this.onTestStepAttachment.bind(this))
34
- .on("test-step-finished", this.onTestStepFinished.bind(this))
35
- .on("test-case-finished", this.onTestCaseFinished.bind(this));
36
- this.labels = config.labels || {};
37
- this.exceptionFormatter = function (message) {
54
+ this.testCaseMap = new Map();
55
+ this.testCaseStartedMap = new Map();
56
+ this.allureSteps = new Map();
57
+ this.convertStatus = (status) => {
58
+ switch (status) {
59
+ case messages_1.TestStepResultStatus.FAILED:
60
+ return allure_js_commons_1.Status.FAILED;
61
+ case messages_1.TestStepResultStatus.PASSED:
62
+ return allure_js_commons_1.Status.PASSED;
63
+ case messages_1.TestStepResultStatus.SKIPPED:
64
+ case messages_1.TestStepResultStatus.PENDING:
65
+ return allure_js_commons_1.Status.SKIPPED;
66
+ default:
67
+ return undefined;
68
+ }
69
+ };
70
+ options.eventBroadcaster.on("envelope", this.parseEnvelope.bind(this));
71
+ this.labelsMatchers = config.labels || [];
72
+ this.linksMatchers = config.links || [];
73
+ this.exceptionFormatter = (message) => {
38
74
  if (config.exceptionFormatter !== undefined) {
39
75
  try {
40
76
  return config.exceptionFormatter(message);
@@ -45,230 +81,320 @@ class CucumberJSAllureFormatter extends cucumber_1.Formatter {
45
81
  }
46
82
  return message;
47
83
  };
48
- this.allureInterface = new CucumberAllureInterface_1.CucumberAllureInterface(this, this.allureRuntime);
49
- options.supportCodeLibrary.World.prototype.allure = this.allureInterface;
84
+ if (options.supportCodeLibrary.World === cucumber_1.World) {
85
+ options.supportCodeLibrary.World = CucumberAllureWorld_1.CucumberAllureWorld;
86
+ }
50
87
  this.beforeHooks = options.supportCodeLibrary.beforeTestCaseHookDefinitions;
51
88
  this.afterHooks = options.supportCodeLibrary.afterTestCaseHookDefinitions;
52
89
  }
53
- onSource(data) {
54
- this.sourceMap.set(data.uri, data.data.split(/\n/));
90
+ get tagsIgnorePatterns() {
91
+ const { linksMatchers, labelsMatchers } = this;
92
+ return [...linksMatchers, ...labelsMatchers].flatMap(({ pattern }) => pattern);
55
93
  }
56
- onGherkinDocument(data) {
57
- data.document.caseMap = new Map();
58
- data.document.stepMap = new Map();
59
- if (data.document.feature !== undefined) {
60
- for (const test of data.document.feature.children || []) {
61
- test.stepMap = new Map();
62
- if (test.type === "Background") {
63
- data.document.stepMap = new Map();
64
- for (const step of test.steps) {
65
- step.isBackground = true;
66
- data.document.stepMap.set(step.location.line, step);
67
- }
68
- }
69
- else {
70
- for (const step of test.steps) {
71
- test.stepMap.set(step.location.line, step);
72
- }
73
- }
74
- if (test.type === "ScenarioOutline") {
75
- for (const example of Example_1.examplesToSensibleFormat(test.examples || [])) {
76
- const copy = Object.assign({}, test);
77
- copy.example = example;
78
- data.document.caseMap.set(example.line, copy);
79
- }
80
- }
81
- else {
82
- data.document.caseMap.set(test.location.line, test);
83
- }
84
- }
94
+ parseEnvelope(envelope) {
95
+ if (envelope.gherkinDocument) {
96
+ this.onGherkinDocument(envelope.gherkinDocument);
85
97
  }
86
- this.featureMap.set(data.uri, data.document);
87
- }
88
- onTestCasePrepared(data) {
89
- this.stepsMap.clear();
90
- this.stepsMap.set(SourceLocation_1.SourceLocation.toKey(data), data.steps);
91
- this.currentBefore = null;
92
- this.currentAfter = null;
93
- }
94
- onTestCaseStarted(data) {
95
- const feature = this.featureMap.get(data.sourceLocation.uri);
96
- if (feature === undefined || feature.feature === undefined) {
97
- throw new Error("Unknown feature");
98
- }
99
- const test = feature.caseMap === undefined
100
- ? undefined : feature.caseMap.get(data.sourceLocation.line);
101
- if (test === undefined) {
102
- throw new Error("Unknown scenario");
103
- }
104
- this.currentGroup = this.allureRuntime.startGroup();
105
- this.currentTest =
106
- this.currentGroup.startTest(utilities_1.applyExample(test.name || "Unnamed test", test.example));
107
- const info = {
108
- uri: data.sourceLocation.uri,
109
- f: feature.feature.name,
110
- t: test.name,
111
- a: null
112
- };
113
- if (test.example !== undefined) {
114
- info.a = test.example.arguments;
115
- for (const prop in test.example.arguments) {
116
- if (!test.example.arguments[prop])
117
- continue;
118
- this.currentTest.addParameter(prop, test.example.arguments[prop]);
119
- }
98
+ else if (envelope.pickle) {
99
+ this.onPickle(envelope.pickle);
120
100
  }
121
- this.currentTest.historyId = utilities_1.hash(JSON.stringify(info));
122
- this.currentTest.addLabel(allure_js_commons_1.LabelName.THREAD, `${process.pid}`);
123
- this.currentTest.fullName = `${data.sourceLocation.uri}:${feature.feature.name}:${test.name}`;
124
- this.currentTest.addLabel(allure_js_commons_1.LabelName.FEATURE, feature.feature.name);
125
- this.currentTest.description = utilities_1.stripIndent(test.description || "");
126
- for (const tag of [...(test.tags || []), ...feature.feature.tags]) {
127
- this.currentTest.addLabel(allure_js_commons_1.LabelName.TAG, tag.name);
128
- for (const label in this.labels) {
129
- if (!this.labels[label])
130
- continue;
131
- for (const reg of this.labels[label]) {
132
- const match = tag.name.match(reg);
133
- if (match != null && match.length > 1) {
134
- this.currentTest.addLabel(label, match[1]);
135
- }
136
- }
137
- }
101
+ else if (envelope.testCase) {
102
+ this.onTestCase(envelope.testCase);
103
+ }
104
+ else if (envelope.testCaseStarted) {
105
+ this.onTestCaseStarted(envelope.testCaseStarted);
106
+ }
107
+ else if (envelope.testCaseFinished) {
108
+ this.onTestCaseFinished(envelope.testCaseFinished);
109
+ }
110
+ else if (envelope.attachment) {
111
+ this.onAttachment(envelope.attachment);
112
+ }
113
+ else if (envelope.hook) {
114
+ this.onHook(envelope.hook);
115
+ }
116
+ else if (envelope.source) {
117
+ this.onSource(envelope.source);
118
+ }
119
+ else if (envelope.testStepStarted) {
120
+ this.onTestStepStarted(envelope.testStepStarted);
121
+ }
122
+ else if (envelope.testStepFinished) {
123
+ this.onTestStepFinished(envelope.testStepFinished);
138
124
  }
139
125
  }
140
- onTestStepStarted(data) {
141
- const location = (this.stepsMap.get(SourceLocation_1.SourceLocation.toKey(data.testCase)) || [])[data.index];
142
- const feature = this.featureMap.get(data.testCase.sourceLocation.uri);
143
- if (feature === undefined)
144
- throw new Error("Unknown feature");
145
- const test = feature.caseMap === undefined
146
- ? undefined : feature.caseMap.get(data.testCase.sourceLocation.line);
147
- if (test === undefined)
148
- throw new Error("Unknown scenario");
149
- let step;
150
- if (location.sourceLocation !== undefined && feature.stepMap !== undefined) {
151
- step = test.stepMap.get(location.sourceLocation.line)
152
- || feature.stepMap.get(location.sourceLocation.line);
126
+ parseTagsLabels(tags) {
127
+ const labels = [];
128
+ if (this.labelsMatchers.length === 0) {
129
+ return labels;
153
130
  }
154
- else {
155
- if (location.actionLocation === undefined)
156
- location.actionLocation = {
157
- uri: "unknown",
158
- line: -1
131
+ this.labelsMatchers.forEach((matcher) => {
132
+ const matchedTags = tags.filter((tag) => matcher.pattern.some((pattern) => pattern.test(tag.name)));
133
+ const matchedLabels = matchedTags.map((tag) => {
134
+ const tagValue = tag.name.replace(/^@\S+:/, "");
135
+ return {
136
+ name: matcher.name,
137
+ value: tagValue,
159
138
  };
160
- step = {
161
- location: { line: -1 },
162
- text: `${location.actionLocation.uri}:${location.actionLocation.line}`,
163
- keyword: ""
164
- };
139
+ });
140
+ labels.push(...matchedLabels);
141
+ });
142
+ return labels;
143
+ }
144
+ parseTagsLinks(tags) {
145
+ const tagKeyRe = /^@\S+=/;
146
+ const links = [];
147
+ if (this.linksMatchers.length === 0) {
148
+ return links;
165
149
  }
166
- if (step === undefined)
167
- throw new Error("Unknown step");
168
- let stepText = utilities_1.applyExample(`${step.keyword}${step.text}`, test.example);
169
- const isAfter = this.afterHooks.find(({ uri, line }) => {
170
- if (location.actionLocation === undefined)
171
- return false;
172
- return uri === location.actionLocation.uri &&
173
- line === location.actionLocation.line;
150
+ this.linksMatchers.forEach((matcher) => {
151
+ const matchedTags = tags.filter((tag) => matcher.pattern.some((pattern) => pattern.test(tag.name)));
152
+ const matchedLinks = matchedTags.map((tag) => {
153
+ const tagValue = tag.name.replace(tagKeyRe, "");
154
+ return {
155
+ url: matcher.urlTemplate.replace(/%s$/, tagValue) || tagValue,
156
+ type: matcher.type,
157
+ };
158
+ });
159
+ links.push(...matchedLinks);
174
160
  });
175
- const isBefore = this.beforeHooks.find(({ uri, line }) => {
176
- if (location.actionLocation === undefined)
177
- return false;
178
- return uri === location.actionLocation.uri &&
179
- line === location.actionLocation.line;
161
+ return links;
162
+ }
163
+ onGherkinDocument(data) {
164
+ var _a, _b;
165
+ if (data.uri) {
166
+ this.documentMap.set(data.uri, data);
167
+ }
168
+ (_b = (_a = data.feature) === null || _a === void 0 ? void 0 : _a.children) === null || _b === void 0 ? void 0 : _b.forEach((fc) => {
169
+ if (fc.scenario) {
170
+ this.onScenario(fc.scenario);
171
+ }
180
172
  });
181
- if (step.isBackground) {
182
- if (this.currentBefore === null)
183
- this.currentBefore = this.currentGroup.addBefore();
173
+ }
174
+ onScenario(data) {
175
+ this.scenarioMap.set(data.id, data);
176
+ data.steps.forEach((step) => this.stepMap.set(step.id, step));
177
+ }
178
+ onPickle(data) {
179
+ this.pickleMap.set(data.id, data);
180
+ data.steps.forEach((ps) => this.pickleStepMap.set(ps.id, ps));
181
+ }
182
+ onTestCase(data) {
183
+ this.testCaseMap.set(data.id, data);
184
+ data.testSteps.forEach((ts) => this.testStepMap.set(ts.id, ts));
185
+ }
186
+ onTestCaseStarted(data) {
187
+ var _a, _b, _c, _d, _e;
188
+ const testCase = this.testCaseMap.get(data.testCaseId);
189
+ if (!testCase) {
190
+ console.error("onTestCaseStarted", "test case not found", data);
191
+ return;
184
192
  }
185
- else if (isBefore) {
186
- if (this.currentBefore === null)
187
- this.currentBefore = this.currentGroup.addBefore();
188
- stepText = `Before: ${isBefore.code.name || step.text}`;
193
+ const pickle = this.pickleMap.get(testCase.pickleId);
194
+ if (!pickle) {
195
+ console.error("onTestCaseStarted", "pickle not found", data);
196
+ return;
189
197
  }
190
- else if (isAfter) {
191
- if (this.currentAfter === null)
192
- this.currentAfter = this.currentGroup.addAfter();
193
- stepText = `After: ${isAfter.code.name || step.text}`;
198
+ const doc = this.documentMap.get(pickle.uri);
199
+ const featureLabels = this.parseTagsLabels(((_a = doc === null || doc === void 0 ? void 0 : doc.feature) === null || _a === void 0 ? void 0 : _a.tags) || []);
200
+ const featureLinks = this.parseTagsLinks(((_b = doc === null || doc === void 0 ? void 0 : doc.feature) === null || _b === void 0 ? void 0 : _b.tags) || []);
201
+ const scenarioId = (_c = pickle === null || pickle === void 0 ? void 0 : pickle.astNodeIds) === null || _c === void 0 ? void 0 : _c[0];
202
+ const scenario = this.scenarioMap.get(scenarioId);
203
+ const scenarioLabels = this.parseTagsLabels((scenario === null || scenario === void 0 ? void 0 : scenario.tags) || []);
204
+ const scenarioLinks = this.parseTagsLinks((scenario === null || scenario === void 0 ? void 0 : scenario.tags) || []);
205
+ const currentTest = new allure_js_commons_1.AllureTest(this.allureRuntime, Date.now());
206
+ const thread = data.workerId || ALLURE_THREAD_NAME || process_1.default.pid.toString();
207
+ const fullName = `${pickle.uri}#${pickle.name}`;
208
+ const testCaseId = (0, allure_js_commons_1.md5)(fullName);
209
+ this.testCaseStartedMap.set(data.id, data);
210
+ this.testCaseTestStepsResults.set(data.id, []);
211
+ this.currentTestsMap.set(data.id, currentTest);
212
+ currentTest.name = pickle.name;
213
+ currentTest.fullName = fullName;
214
+ currentTest.testCaseId = testCaseId;
215
+ currentTest.historyId = testCaseId;
216
+ currentTest.addLabel(allure_js_commons_1.LabelName.HOST, this.hostname);
217
+ currentTest.addLabel(allure_js_commons_1.LabelName.LANGUAGE, "javascript");
218
+ currentTest.addLabel(allure_js_commons_1.LabelName.FRAMEWORK, "cucumberjs");
219
+ currentTest.addLabel(allure_js_commons_1.LabelName.THREAD, thread);
220
+ featureLabels.forEach((label) => currentTest.addLabel(label.name, label.value));
221
+ featureLinks.forEach((link) => currentTest.addLink(link.url, link.name, link.type));
222
+ scenarioLabels.forEach((label) => currentTest.addLabel(label.name, label.value));
223
+ scenarioLinks.forEach((link) => currentTest.addLink(link.url, link.name, link.type));
224
+ if (doc === null || doc === void 0 ? void 0 : doc.feature) {
225
+ currentTest.addLabel(allure_js_commons_1.LabelName.FEATURE, doc.feature.name);
194
226
  }
195
- else {
196
- if (this.currentBefore !== null)
197
- this.currentBefore = null;
198
- if (this.currentAfter !== null)
199
- this.currentAfter = null;
200
- }
201
- const allureStep = (this.currentAfter || this.currentBefore || this.currentTest).startStep(stepText);
202
- this.pushStep(allureStep);
203
- if (step.argument !== undefined) {
204
- if (step.argument.content !== undefined) {
205
- const file = this.allureRuntime.writeAttachment(step.argument.content, {
206
- contentType: allure_js_commons_1.ContentType.TEXT,
207
- });
208
- allureStep.addAttachment("Text", allure_js_commons_1.ContentType.TEXT, file);
209
- }
210
- if (step.argument.rows !== undefined) {
211
- const file = this.allureRuntime.writeAttachment(step.argument.rows.map(row => row.cells.map(cell => cell.value.replace(/\t/g, " ")).join("\t")).join("\n"), { contentType: allure_js_commons_1.ContentType.TSV });
212
- allureStep.addAttachment("Table", allure_js_commons_1.ContentType.TSV, file);
227
+ if (scenario) {
228
+ currentTest.addLabel(allure_js_commons_1.LabelName.SUITE, scenario.name);
229
+ }
230
+ if ((_d = pickle.tags) === null || _d === void 0 ? void 0 : _d.length) {
231
+ const filteredTags = pickle.tags.filter((tag) => !this.tagsIgnorePatterns.some((pattern) => pattern.test(tag.name)));
232
+ filteredTags.forEach((tag) => currentTest.addLabel(allure_js_commons_1.LabelName.TAG, tag.name));
233
+ }
234
+ if (!((_e = scenario === null || scenario === void 0 ? void 0 : scenario.examples) === null || _e === void 0 ? void 0 : _e.length)) {
235
+ return;
236
+ }
237
+ scenario.examples.forEach((example) => {
238
+ var _a;
239
+ const csvDataTableHeader = ((_a = example === null || example === void 0 ? void 0 : example.tableHeader) === null || _a === void 0 ? void 0 : _a.cells.map((cell) => cell.value).join(",")) || "";
240
+ const csvDataTableBody = (example === null || example === void 0 ? void 0 : example.tableBody.map((row) => row.cells.map((cell) => cell.value).join(",")).join("\n")) ||
241
+ "";
242
+ if (!csvDataTableHeader && !csvDataTableBody) {
243
+ return;
213
244
  }
245
+ const csvDataTable = `${csvDataTableHeader}\n${csvDataTableBody}\n`;
246
+ const attachmentFilename = this.allureRuntime.writeAttachment(csvDataTable, allure_js_commons_1.ContentType.CSV);
247
+ currentTest.addAttachment("Examples", {
248
+ contentType: allure_js_commons_1.ContentType.CSV,
249
+ }, attachmentFilename);
250
+ });
251
+ }
252
+ onAttachment(data) {
253
+ const currentTest = this.currentTestsMap.get((data === null || data === void 0 ? void 0 : data.testCaseStartedId) || "");
254
+ if (!currentTest) {
255
+ return;
256
+ }
257
+ const currentStep = this.allureSteps.get((data === null || data === void 0 ? void 0 : data.testStepId) || "");
258
+ if (!data) {
259
+ console.error("onAttachment", "attachment can't be empty");
260
+ return;
214
261
  }
262
+ const { fileName = "attachment", body, mediaType, contentEncoding } = data;
263
+ if (mediaType === internal_1.ALLURE_METADATA_CONTENT_TYPE) {
264
+ this.handleAllureAttachment({
265
+ test: currentTest,
266
+ step: currentStep,
267
+ metadata: JSON.parse(body),
268
+ });
269
+ return;
270
+ }
271
+ const encoding = Buffer.isEncoding(contentEncoding) ? contentEncoding : undefined;
272
+ const attachmentFilename = this.allureRuntime.writeAttachment(body, mediaType, encoding);
273
+ currentTest.addAttachment(fileName, {
274
+ contentType: mediaType,
275
+ }, attachmentFilename);
215
276
  }
216
- onTestStepAttachment(data) {
217
- if (this.currentStep === null)
218
- throw new Error("There is no step to add attachment to");
219
- const type = data.media.type;
220
- let content = data.data;
221
- if ([allure_js_commons_1.ContentType.JPEG, allure_js_commons_1.ContentType.PNG, allure_js_commons_1.ContentType.WEBM].indexOf(type) >= 0) {
222
- content = Buffer.from(content, "base64");
223
- }
224
- const file = this.allureRuntime.writeAttachment(content, { contentType: type });
225
- this.currentStep.addAttachment("attached", type, file);
277
+ handleAllureAttachment(payload) {
278
+ const { labels = [], links = [], parameter = [], steps = [], description, descriptionHtml, } = payload.metadata;
279
+ links.forEach((link) => payload.test.addLink(link.url, link.type, link.name));
280
+ labels.forEach((label) => payload.test.addLabel(label.name, label.value));
281
+ parameter.forEach(({ name, value, excluded, mode }) => payload.test.addParameter(name, value, {
282
+ excluded,
283
+ mode,
284
+ }));
285
+ steps.forEach((step) => {
286
+ this.handleAllureStep({
287
+ test: payload.test,
288
+ step: payload.step,
289
+ stepMetadata: step,
290
+ });
291
+ });
292
+ if (description) {
293
+ payload.test.description = description;
294
+ }
295
+ if (descriptionHtml) {
296
+ payload.test.descriptionHtml = descriptionHtml;
297
+ }
226
298
  }
227
- onTestStepFinished(data) {
228
- const currentStep = this.currentStep;
229
- if (currentStep === null)
230
- throw new Error("No current step defined");
231
- currentStep.status = utilities_1.statusTextToAllure(data.result.status);
232
- currentStep.stage = utilities_1.statusTextToStage(data.result.status);
233
- this.setException(currentStep, data.result.exception);
234
- currentStep.endStep();
235
- this.popStep();
299
+ handleAllureStep(payload) {
300
+ const step = allure_js_commons_1.AllureCommandStepExecutable.toExecutableItem(this.allureRuntime, payload.stepMetadata);
301
+ if (payload.step) {
302
+ payload.step.addStep(step);
303
+ return;
304
+ }
305
+ payload.test.addStep(step);
236
306
  }
237
307
  onTestCaseFinished(data) {
238
- if (this.currentTest === null || this.currentGroup === null) {
239
- throw new Error("No current test info");
240
- }
241
- this.currentTest.status = utilities_1.statusTextToAllure(data.result.status);
242
- this.currentTest.stage = utilities_1.statusTextToStage(data.result.status);
243
- this.setException(this.currentTest, data.result.exception);
244
- this.currentTest.endTest();
245
- this.currentGroup.endGroup();
246
- }
247
- setException(target, exception) {
248
- if (exception !== undefined) {
249
- if (typeof exception === "string") {
250
- target.detailsMessage = this.exceptionFormatter(exception);
251
- }
252
- else {
253
- target.detailsMessage =
254
- this.exceptionFormatter(exception.message || "Error.message === undefined");
255
- target.detailsTrace = exception.stack || "";
256
- }
308
+ const currentTest = this.currentTestsMap.get(data.testCaseStartedId);
309
+ if (!currentTest) {
310
+ console.error("onTestCaseFinished", "current test not found", data);
311
+ return;
257
312
  }
313
+ const testCaseStarted = this.testCaseStartedMap.get(data.testCaseStartedId);
314
+ if (!testCaseStarted) {
315
+ console.error("onTestCaseFinished", "testCaseStarted event not found", data);
316
+ return;
317
+ }
318
+ const testCase = this.testCaseMap.get(testCaseStarted.testCaseId);
319
+ if (!testCase) {
320
+ console.error("onTestCaseFinished", "testCase not found", data);
321
+ return;
322
+ }
323
+ const pickle = this.pickleMap.get(testCase.pickleId);
324
+ if (!pickle) {
325
+ console.error("onTestCaseFinished", "pickle not found", data);
326
+ return;
327
+ }
328
+ const testStepResults = this.testCaseTestStepsResults.get(testCaseStarted.id);
329
+ if (testStepResults === null || testStepResults === void 0 ? void 0 : testStepResults.length) {
330
+ const worstTestStepResult = messages.getWorstTestStepResult(testStepResults);
331
+ currentTest.status = currentTest.isAnyStepFailed
332
+ ? allure_js_commons_1.Status.FAILED
333
+ : this.convertStatus(worstTestStepResult.status);
334
+ currentTest.statusDetails = {
335
+ message: worstTestStepResult.message,
336
+ };
337
+ }
338
+ else {
339
+ currentTest.status = allure_js_commons_1.Status.PASSED;
340
+ }
341
+ currentTest.endTest(Date.now());
342
+ this.currentTestsMap.delete(data.testCaseStartedId);
258
343
  }
259
- pushStep(step) {
260
- this.stepStack.push(step);
344
+ onHook(data) {
345
+ this.hookMap.set(data.id, data);
261
346
  }
262
- popStep() {
263
- this.stepStack.pop();
347
+ onSource(data) {
348
+ if (data.uri) {
349
+ this.sourceMap.set(data.uri, data);
350
+ }
264
351
  }
265
- get currentStep() {
266
- if (this.stepStack.length > 0)
267
- return this.stepStack[this.stepStack.length - 1];
268
- return null;
352
+ onTestStepStarted(data) {
353
+ const currentTest = this.currentTestsMap.get(data.testCaseStartedId);
354
+ if (!currentTest) {
355
+ return;
356
+ }
357
+ const testStep = this.testStepMap.get(data.testStepId);
358
+ if (!testStep) {
359
+ console.error("onTestStepStarted", "can't find step", data);
360
+ return;
361
+ }
362
+ if (testStep.pickleStepId) {
363
+ const ps = this.pickleStepMap.get(testStep.pickleStepId);
364
+ if (!ps) {
365
+ return;
366
+ }
367
+ const keyword = ps.astNodeIds
368
+ .map((astNodeId) => this.stepMap.get(astNodeId))
369
+ .map((step) => step === null || step === void 0 ? void 0 : step.keyword)
370
+ .find((kw) => kw !== undefined) || "";
371
+ const { argument } = ps;
372
+ const allureStep = currentTest.startStep(keyword + ps.text, Date.now());
373
+ this.allureSteps.set(data.testStepId, allureStep);
374
+ if (!(argument === null || argument === void 0 ? void 0 : argument.dataTable)) {
375
+ return;
376
+ }
377
+ const csvDataTable = argument.dataTable.rows.reduce((acc, row) => `${acc + row.cells.map((cell) => cell.value).join(",")}\n`, "");
378
+ const attachmentFilename = this.allureRuntime.writeAttachment(csvDataTable, allure_js_commons_1.ContentType.CSV);
379
+ allureStep.addAttachment("Data table", {
380
+ contentType: allure_js_commons_1.ContentType.CSV,
381
+ }, attachmentFilename);
382
+ }
269
383
  }
270
- writeAttachment(content, options) {
271
- return this.allureRuntime.writeAttachment(content, options);
384
+ onTestStepFinished(data) {
385
+ var _a;
386
+ const currentTest = this.currentTestsMap.get(data.testCaseStartedId);
387
+ (_a = this.testCaseTestStepsResults.get(data.testCaseStartedId)) === null || _a === void 0 ? void 0 : _a.push(data.testStepResult);
388
+ if (!currentTest) {
389
+ return;
390
+ }
391
+ const allureStep = this.allureSteps.get(data.testStepId);
392
+ if (!allureStep) {
393
+ return;
394
+ }
395
+ allureStep.detailsMessage = data.testStepResult.message;
396
+ allureStep.status = this.convertStatus(data.testStepResult.status);
397
+ allureStep.endStep(Date.now());
272
398
  }
273
399
  }
274
400
  exports.CucumberJSAllureFormatter = CucumberJSAllureFormatter;