allure-cypress 3.0.0-beta.8 → 3.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.
@@ -5,245 +5,600 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.allureCypress = exports.AllureCypress = void 0;
7
7
  var _allureJsCommons = require("allure-js-commons");
8
- var _sdk = require("allure-js-commons/sdk");
9
8
  var _reporter = require("allure-js-commons/sdk/reporter");
10
9
  var _utils = require("./utils.js");
11
- var _excluded = ["resultsDir", "videoOnFailOnly"];
10
+ var _excluded = ["fullNameSuffix"],
11
+ _excluded2 = ["labels"],
12
+ _excluded3 = ["fullNameSuffix", "suites", "duration", "retries"],
13
+ _excluded4 = ["resultsDir", "videoOnFailOnly"];
12
14
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
13
15
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
14
16
  function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], t.indexOf(o) >= 0 || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
15
17
  function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }
18
+ function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); }
19
+ function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }
16
20
  function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
17
21
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
18
22
  function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
23
+ function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); }
24
+ function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); }
25
+ var _endAllSpecs = /*#__PURE__*/new WeakMap();
26
+ var _applyAllureCypressMessages = /*#__PURE__*/new WeakMap();
27
+ var _startRun = /*#__PURE__*/new WeakMap();
28
+ var _startSuite = /*#__PURE__*/new WeakMap();
29
+ var _pushNewSuiteScope = /*#__PURE__*/new WeakMap();
30
+ var _stopSuite = /*#__PURE__*/new WeakMap();
31
+ var _writeLastSuiteScope = /*#__PURE__*/new WeakMap();
32
+ var _startHook = /*#__PURE__*/new WeakMap();
33
+ var _stopHook = /*#__PURE__*/new WeakMap();
34
+ var _startTest = /*#__PURE__*/new WeakMap();
35
+ var _addNewTestResult = /*#__PURE__*/new WeakMap();
36
+ var _failHookAndTest = /*#__PURE__*/new WeakMap();
37
+ var _passTest = /*#__PURE__*/new WeakMap();
38
+ var _skipTest = /*#__PURE__*/new WeakMap();
39
+ var _addSkippedTest = /*#__PURE__*/new WeakMap();
40
+ var _stopTest = /*#__PURE__*/new WeakMap();
41
+ var _stopExistingTestResult = /*#__PURE__*/new WeakMap();
42
+ var _startCommand = /*#__PURE__*/new WeakMap();
43
+ var _stopCommand = /*#__PURE__*/new WeakMap();
44
+ var _applyRuntimeApiMessages = /*#__PURE__*/new WeakMap();
45
+ var _emitPreviousTestScope = /*#__PURE__*/new WeakMap();
46
+ var _resolveRootUuid = /*#__PURE__*/new WeakMap();
47
+ var _attachSpecVideo = /*#__PURE__*/new WeakMap();
48
+ var _emitRemainingScopes = /*#__PURE__*/new WeakMap();
49
+ var _initializeSpecContext = /*#__PURE__*/new WeakMap();
19
50
  class AllureCypress {
20
- constructor(config) {
51
+ constructor() {
52
+ var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
21
53
  _defineProperty(this, "allureRuntime", void 0);
22
- _defineProperty(this, "messagesByAbsolutePath", new Map());
23
- _defineProperty(this, "runContextByAbsolutePath", new Map());
24
- _defineProperty(this, "globalHooksMessages", []);
54
+ _defineProperty(this, "specContextByAbsolutePath", new Map());
25
55
  _defineProperty(this, "videoOnFailOnly", false);
26
- var _ref = config || {},
27
- {
28
- resultsDir = "./allure-results",
29
- videoOnFailOnly = false
30
- } = _ref,
31
- rest = _objectWithoutProperties(_ref, _excluded);
32
- this.videoOnFailOnly = videoOnFailOnly;
33
- this.allureRuntime = new _reporter.ReporterRuntime(_objectSpread({
34
- writer: new _reporter.FileSystemWriter({
35
- resultsDir
36
- })
37
- }, rest));
38
- }
39
- createEmptyRunContext(absolutePath) {
40
- this.runContextByAbsolutePath.set(absolutePath, {
41
- executables: [],
42
- steps: [],
43
- scopes: [],
44
- globalHooksMessages: []
56
+ _defineProperty(this, "attachToCypress", on => {
57
+ on("task", {
58
+ reportAllureCypressSpecMessages: args => {
59
+ _classPrivateFieldGet(_applyAllureCypressMessages, this).call(this, args);
60
+ return null;
61
+ },
62
+ reportFinalAllureCypressSpecMessages: args => {
63
+ _classPrivateFieldGet(_applyAllureCypressMessages, this).call(this, args);
64
+ if (args.isInteractive) {
65
+ // In non-interactive mode the spec is ended via the 'after:spec' event instead
66
+ // to get the spec's video.
67
+ this.endSpec(args.absolutePath);
68
+ }
69
+ return null;
70
+ }
71
+ });
72
+
73
+ // Emits the remaining fixtures and writes the video of the spec.
74
+ // In interactive mode it's invoked through the `reportFinalAllureCypressSpecMessages` task.
75
+ on("after:spec", this.onAfterSpec);
76
+
77
+ // Emits the categories and env info. Doesn't work in interactive mode unless
78
+ // `experimentalInteractiveRunEvents` is set.
79
+ on("after:run", this.onAfterRun);
45
80
  });
46
- }
47
- attachToCypress(on) {
48
- on("task", {
49
- readAllureTestPlan: () => {
50
- var _parseTestPlan;
51
- return (_parseTestPlan = (0, _reporter.parseTestPlan)()) !== null && _parseTestPlan !== void 0 ? _parseTestPlan : null;
52
- },
53
- allureReportTest: _ref2 => {
54
- var {
55
- messages,
56
- absolutePath
57
- } = _ref2;
58
- this.messagesByAbsolutePath.set(absolutePath, messages);
59
- return null;
60
- },
61
- allureReportSpec: spec => {
62
- this.createEmptyRunContext(spec.absolute);
63
- this.endSpec(spec);
64
- return null;
81
+ /**
82
+ * Forward the `after:spec` event into Allure Cypress using this function if
83
+ * you need to define your own handler or combine Allure Cypress with other
84
+ * plugins. More info [here](https://github.com/allure-framework/allure-js/blob/main/packages/allure-cypress/README.md#setupnodeevents-limitations).
85
+ * @param spec The first argument of the `after:spec` event.
86
+ * @param results The second argument of the `after:spec` event. It's `undefined` in interactive mode.
87
+ * @example
88
+ * ```javascript
89
+ * import { defineConfig } from "cypress";
90
+ * import { allureCypress } from "allure-cypress/reporter";
91
+ *
92
+ * export default defineConfig({
93
+ * setupNodeEvents: (on, config) => {
94
+ * const allureReporter = allureCypress(on, config);
95
+ * on("after:spec", (spec, results) => {
96
+ * allureReporter.onAfterSpec(spec, results);
97
+ * });
98
+ * return config;
99
+ * }
100
+ * // ...
101
+ * });
102
+ * ```
103
+ */
104
+ _defineProperty(this, "onAfterSpec", (spec, results) => {
105
+ var _results$video;
106
+ this.endSpec(spec.absolute, (_results$video = results === null || results === void 0 ? void 0 : results.video) !== null && _results$video !== void 0 ? _results$video : undefined);
107
+ });
108
+ /**
109
+ * Forward the `after:run` event into Allure Cypress using this function if
110
+ * you need to define your own handler or combine Allure Cypress with other
111
+ * plugins. More info [here](https://github.com/allure-framework/allure-js/blob/main/packages/allure-cypress/README.md#setupnodeevents-limitations).
112
+ * @param results The argument of the `after:run` event. It's `undefined` in interactive mode.
113
+ * @example
114
+ * ```javascript
115
+ * import { defineConfig } from "cypress";
116
+ * import { allureCypress } from "allure-cypress/reporter";
117
+ *
118
+ * export default defineConfig({
119
+ * setupNodeEvents: (on, config) => {
120
+ * const allureReporter = allureCypress(on, config);
121
+ * on("after:run", (results) => {
122
+ * allureReporter.onAfterRun(results);
123
+ * });
124
+ * return config;
125
+ * }
126
+ * // ...
127
+ * });
128
+ * ```
129
+ */
130
+ _defineProperty(this, "onAfterRun", results => {
131
+ this.endRun();
132
+ });
133
+ _defineProperty(this, "endRun", () => {
134
+ _classPrivateFieldGet(_endAllSpecs, this).call(this);
135
+ this.allureRuntime.writeEnvironmentInfo();
136
+ this.allureRuntime.writeCategoriesDefinitions();
137
+ });
138
+ _defineProperty(this, "endSpec", (specAbsolutePath, cypressVideoPath) => {
139
+ var specContext = this.specContextByAbsolutePath.get(specAbsolutePath);
140
+ if (specContext) {
141
+ _classPrivateFieldGet(_attachSpecVideo, this).call(this, specContext, cypressVideoPath);
142
+ _classPrivateFieldGet(_emitRemainingScopes, this).call(this, specContext);
143
+ this.specContextByAbsolutePath.delete(specAbsolutePath);
65
144
  }
66
145
  });
67
- }
68
- endRun(result) {
69
- result.runs.forEach(run => {
70
- this.createEmptyRunContext(run.spec.absolute);
71
- this.endSpec(run.spec, run.video || undefined);
146
+ _classPrivateFieldInitSpec(this, _endAllSpecs, () => {
147
+ for (var specAbsolutePath of this.specContextByAbsolutePath.keys()) {
148
+ this.endSpec(specAbsolutePath);
149
+ }
72
150
  });
73
- this.allureRuntime.writeEnvironmentInfo();
74
- this.allureRuntime.writeCategoriesDefinitions();
75
- }
76
- endSpec(spec, cypressVideoPath) {
77
- var _this$messagesByAbsol;
78
- var specMessages = (_this$messagesByAbsol = this.messagesByAbsolutePath.get(spec.absolute)) !== null && _this$messagesByAbsol !== void 0 ? _this$messagesByAbsol : [];
79
- var runContext = this.runContextByAbsolutePath.get(spec.absolute);
80
- var isSpecFailed = specMessages.some(message => message.type === "cypress_test_end" && (message.data.status === _allureJsCommons.Status.FAILED || message.data.status === _allureJsCommons.Status.BROKEN));
81
- var shouldVideoBeAttached = (!this.videoOnFailOnly || isSpecFailed) && cypressVideoPath;
82
- specMessages.forEach((message, i) => {
83
- var _data;
84
- // we add cypressTestId to messages where it's possible because the field is very useful to glue data
85
- // @ts-ignore
86
- var previousMessagesSlice = specMessages.slice(0, i);
87
- var lastHookMessage = (0, _utils.toReversed)(previousMessagesSlice).find(_ref3 => {
88
- var {
89
- type
90
- } = _ref3;
91
- return type === "cypress_hook_start" || type === "cypress_hook_end";
151
+ _classPrivateFieldInitSpec(this, _applyAllureCypressMessages, _ref => {
152
+ var {
153
+ messages,
154
+ absolutePath
155
+ } = _ref;
156
+ messages.forEach(message => {
157
+ if (message.type === "cypress_run_start") {
158
+ _classPrivateFieldGet(_startRun, this).call(this, absolutePath);
159
+ return;
160
+ }
161
+ var context = this.specContextByAbsolutePath.get(absolutePath);
162
+ if (!context) {
163
+ return;
164
+ }
165
+ switch (message.type) {
166
+ case "cypress_suite_start":
167
+ _classPrivateFieldGet(_startSuite, this).call(this, context, message);
168
+ break;
169
+ case "cypress_suite_end":
170
+ _classPrivateFieldGet(_stopSuite, this).call(this, context, message);
171
+ break;
172
+ case "cypress_hook_start":
173
+ _classPrivateFieldGet(_startHook, this).call(this, context, message);
174
+ break;
175
+ case "cypress_hook_end":
176
+ _classPrivateFieldGet(_stopHook, this).call(this, context, message);
177
+ break;
178
+ case "cypress_test_start":
179
+ _classPrivateFieldGet(_startTest, this).call(this, context, message);
180
+ break;
181
+ case "cypress_test_pass":
182
+ _classPrivateFieldGet(_passTest, this).call(this, context);
183
+ break;
184
+ case "cypress_fail":
185
+ _classPrivateFieldGet(_failHookAndTest, this).call(this, context, message);
186
+ break;
187
+ case "cypress_test_skip":
188
+ _classPrivateFieldGet(_skipTest, this).call(this, context, message);
189
+ break;
190
+ case "cypress_skipped_test":
191
+ _classPrivateFieldGet(_addSkippedTest, this).call(this, context, message);
192
+ break;
193
+ case "cypress_test_end":
194
+ _classPrivateFieldGet(_stopTest, this).call(this, context, message);
195
+ break;
196
+ case "cypress_command_start":
197
+ _classPrivateFieldGet(_startCommand, this).call(this, context, message);
198
+ break;
199
+ case "cypress_command_end":
200
+ _classPrivateFieldGet(_stopCommand, this).call(this, context, message);
201
+ break;
202
+ default:
203
+ _classPrivateFieldGet(_applyRuntimeApiMessages, this).call(this, context, message);
204
+ break;
205
+ }
92
206
  });
93
- if (message.type === "cypress_suite_start") {
94
- var scopeUuid = this.allureRuntime.startScope();
95
- runContext.scopes.push(scopeUuid);
96
- return;
97
- }
98
- if (message.type === "cypress_suite_end") {
99
- var _scopeUuid = runContext.scopes.pop();
100
- this.allureRuntime.writeScope(_scopeUuid);
101
- return;
102
- }
103
- if (message.type === "cypress_hook_start" && message.data.global) {
104
- runContext.globalHooksMessages.push(message);
105
- return;
106
- }
107
- if (message.type === "cypress_hook_start") {
108
- var fixtureUuid = this.allureRuntime.startFixture((0, _utils.last)(runContext.scopes), message.data.type, {
109
- name: message.data.name,
110
- start: message.data.start
207
+ });
208
+ _classPrivateFieldInitSpec(this, _startRun, absolutePath => {
209
+ // This function is executed once on `cypress run`, but it can be executed
210
+ // multiple times during an interactive session (`cypress open`). Ideally,
211
+ // in that case, we should remove previous result objects that haven't been
212
+ // written yet, but it would've required support in ReporterRuntime.
213
+ // Currently, we're discarding the entire spec context.
214
+ _classPrivateFieldGet(_initializeSpecContext, this).call(this, absolutePath);
215
+ });
216
+ _classPrivateFieldInitSpec(this, _startSuite, (context, _ref2) => {
217
+ var {
218
+ data: {
219
+ id,
220
+ name,
221
+ root
222
+ }
223
+ } = _ref2;
224
+ _classPrivateFieldGet(_pushNewSuiteScope, this).call(this, context, id);
225
+ if (!root) {
226
+ _classPrivateFieldGet(_emitPreviousTestScope, this).call(this, context);
227
+ context.suiteNames.push(name);
228
+ }
229
+ });
230
+ _classPrivateFieldInitSpec(this, _pushNewSuiteScope, (context, suiteId) => {
231
+ var scope = this.allureRuntime.startScope();
232
+ context.suiteScopes.push(scope);
233
+ context.suiteIdToScope.set(suiteId, scope);
234
+ context.suiteScopeToId.set(scope, suiteId);
235
+ return scope;
236
+ });
237
+ _classPrivateFieldInitSpec(this, _stopSuite, (context, _ref3) => {
238
+ var {
239
+ data: {
240
+ root
241
+ }
242
+ } = _ref3;
243
+ _classPrivateFieldGet(_emitPreviousTestScope, this).call(this, context);
244
+ if (!root) {
245
+ context.suiteNames.pop();
246
+ }
247
+ _classPrivateFieldGet(_writeLastSuiteScope, this).call(this, context);
248
+ });
249
+ _classPrivateFieldInitSpec(this, _writeLastSuiteScope, context => {
250
+ var scope = context.suiteScopes.pop();
251
+ if (scope) {
252
+ var suiteId = context.suiteScopeToId.get(scope);
253
+ if (suiteId) {
254
+ context.suiteScopeToId.delete(scope);
255
+ context.suiteIdToScope.delete(suiteId);
256
+ }
257
+ this.allureRuntime.writeScope(scope);
258
+ }
259
+ });
260
+ _classPrivateFieldInitSpec(this, _startHook, (context, _ref4) => {
261
+ var {
262
+ data: {
263
+ name,
264
+ scopeType,
265
+ position,
266
+ start
267
+ }
268
+ } = _ref4;
269
+ var isEach = scopeType === "each";
270
+ var isAfterEach = position === "after" && isEach;
271
+ if (!isAfterEach) {
272
+ _classPrivateFieldGet(_emitPreviousTestScope, this).call(this, context);
273
+ }
274
+ var scope = isEach ? context.testScope : (0, _utils.last)(context.suiteScopes);
275
+ if (scope) {
276
+ context.fixture = this.allureRuntime.startFixture(scope, position, {
277
+ name,
278
+ start,
279
+ status: undefined
111
280
  });
112
- runContext.executables.push(fixtureUuid);
113
- return;
114
- }
115
- if (message.type === "cypress_hook_end" && lastHookMessage !== null && lastHookMessage !== void 0 && (_data = lastHookMessage.data) !== null && _data !== void 0 && _data.global && (lastHookMessage === null || lastHookMessage === void 0 ? void 0 : lastHookMessage.type) === "cypress_hook_start") {
116
- runContext.globalHooksMessages.push(message);
117
- return;
118
- }
119
- if (message.type === "cypress_hook_end") {
120
- var _fixtureUuid = runContext.executables.pop();
121
- this.allureRuntime.updateFixture(_fixtureUuid, r => {
122
- r.stage = _allureJsCommons.Stage.FINISHED;
123
- r.status = message.data.status;
124
- r.stop = message.data.stop;
125
- if (message.data.statusDetails) {
126
- r.statusDetails = message.data.statusDetails;
127
- }
281
+ }
282
+ });
283
+ _classPrivateFieldInitSpec(this, _stopHook, (context, _ref5) => {
284
+ var {
285
+ data: {
286
+ duration
287
+ }
288
+ } = _ref5;
289
+ var fixtureUuid = context.fixture;
290
+ if (fixtureUuid) {
291
+ this.allureRuntime.updateFixture(fixtureUuid, fixture => {
292
+ var _fixture$status;
293
+ (_fixture$status = fixture.status) !== null && _fixture$status !== void 0 ? _fixture$status : fixture.status = _allureJsCommons.Status.PASSED;
294
+ });
295
+ this.allureRuntime.stopFixture(fixtureUuid, {
296
+ duration
128
297
  });
129
- this.allureRuntime.stopFixture(_fixtureUuid);
130
- return;
131
- }
132
- if (message.type === "cypress_test_start") {
133
- var suiteLabels = (0, _reporter.getSuiteLabels)(message.data.specPath.slice(0, -1));
134
- var testTitle = message.data.specPath[message.data.specPath.length - 1];
135
- var titleMetadata = (0, _sdk.extractMetadataFromString)(testTitle);
136
- var testUuid = this.allureRuntime.startTest({
137
- name: titleMetadata.cleanTitle || testTitle,
138
- start: message.data.start,
139
- fullName: "".concat(message.data.filename, "#").concat(message.data.specPath.join(" ")),
140
- stage: _allureJsCommons.Stage.RUNNING,
141
- labels: [{
142
- name: _allureJsCommons.LabelName.LANGUAGE,
143
- value: "javascript"
144
- }, {
145
- name: _allureJsCommons.LabelName.FRAMEWORK,
146
- value: "cypress"
147
- }, ...suiteLabels, ...titleMetadata.labels, ...(0, _reporter.getEnvironmentLabels)()]
148
- }, runContext.scopes);
149
- runContext.executables.push(testUuid);
150
- return;
151
- }
152
- if (message.type === "cypress_test_end") {
153
- var _testUuid = runContext.executables.pop();
154
- this.allureRuntime.updateTest(_testUuid, result => {
155
- result.stage = _allureJsCommons.Stage.FINISHED;
156
- result.status = message.data.status;
157
- if (message.data.retries > 0) {
158
- result.parameters.push({
159
- name: "Retry",
160
- value: message.data.retries.toString()
161
- });
298
+ context.fixture = undefined;
299
+ }
300
+ });
301
+ _classPrivateFieldInitSpec(this, _startTest, (context, _ref6) => {
302
+ var {
303
+ data: {
304
+ fullNameSuffix
162
305
  }
163
- if (!message.data.statusDetails) {
164
- return;
306
+ } = _ref6,
307
+ testResultData = _objectWithoutProperties(_ref6.data, _excluded);
308
+ _classPrivateFieldGet(_emitPreviousTestScope, this).call(this, context);
309
+ var testScope = this.allureRuntime.startScope();
310
+ context.testScope = testScope;
311
+ context.test = _classPrivateFieldGet(_addNewTestResult, this).call(this, context, fullNameSuffix, testResultData, [context.videoScope, ...context.suiteScopes, testScope]);
312
+ });
313
+ _classPrivateFieldInitSpec(this, _addNewTestResult, (context, fullNameSuffix, _ref7, scopes) => {
314
+ var {
315
+ labels: metadataLabels = []
316
+ } = _ref7,
317
+ otherTestData = _objectWithoutProperties(_ref7, _excluded2);
318
+ return this.allureRuntime.startTest(_objectSpread({
319
+ stage: _allureJsCommons.Stage.RUNNING,
320
+ labels: [(0, _reporter.getLanguageLabel)(), (0, _reporter.getFrameworkLabel)("cypress"), ...(0, _reporter.getSuiteLabels)(context.suiteNames), ...metadataLabels, ...(0, _reporter.getEnvironmentLabels)(), (0, _reporter.getHostLabel)(), (0, _reporter.getThreadLabel)(), (0, _reporter.getPackageLabel)(context.specPath)],
321
+ fullName: "".concat((0, _reporter.getPosixPath)(context.specPath), "#").concat(fullNameSuffix)
322
+ }, otherTestData), scopes);
323
+ });
324
+ _classPrivateFieldInitSpec(this, _failHookAndTest, (context, _ref8) => {
325
+ var {
326
+ data: {
327
+ status,
328
+ statusDetails
329
+ }
330
+ } = _ref8;
331
+ var setError = result => Object.assign(result, {
332
+ status,
333
+ statusDetails
334
+ });
335
+ var fixtureUuid = context.fixture;
336
+ if (fixtureUuid) {
337
+ this.allureRuntime.updateFixture(fixtureUuid, setError);
338
+ }
339
+ var testUuid = context.test;
340
+ if (testUuid) {
341
+ this.allureRuntime.updateTest(testUuid, setError);
342
+ }
343
+ context.failed = true;
344
+ });
345
+ _classPrivateFieldInitSpec(this, _passTest, context => {
346
+ var testUuid = context.test;
347
+ if (testUuid) {
348
+ this.allureRuntime.updateTest(testUuid, testResult => {
349
+ testResult.status = _allureJsCommons.Status.PASSED;
350
+ });
351
+ }
352
+ });
353
+ _classPrivateFieldInitSpec(this, _skipTest, (context, _ref9) => {
354
+ var {
355
+ data: {
356
+ statusDetails
357
+ }
358
+ } = _ref9;
359
+ var testUuid = context.test;
360
+ if (testUuid) {
361
+ this.allureRuntime.updateTest(testUuid, testResult => {
362
+ testResult.status = _allureJsCommons.Status.SKIPPED;
363
+ if (statusDetails) {
364
+ testResult.statusDetails = statusDetails;
165
365
  }
166
- result.statusDetails = message.data.statusDetails;
167
366
  });
168
- this.allureRuntime.stopTest(_testUuid);
169
- this.allureRuntime.writeTest(_testUuid);
170
- return;
171
367
  }
172
-
173
- // we can get error when we try to attach screenshot to a failed test because there is no test due to error in hook
174
- if (runContext.executables.length === 0) {
175
- return;
176
- }
177
- if (message.type === "cypress_command_start") {
178
- var lastExecutableUuid = (0, _utils.last)(runContext.executables);
179
- var lastStepUuid = (0, _utils.last)(runContext.steps);
180
- var stepUuid = this.allureRuntime.startStep(lastExecutableUuid, lastStepUuid, {
181
- name: message.data.name,
182
- parameters: message.data.args.map((arg, j) => ({
368
+ });
369
+ _classPrivateFieldInitSpec(this, _addSkippedTest, (context, _ref10) => {
370
+ var {
371
+ data: {
372
+ fullNameSuffix,
373
+ suites,
374
+ duration,
375
+ retries
376
+ }
377
+ } = _ref10,
378
+ testResultData = _objectWithoutProperties(_ref10.data, _excluded3);
379
+ // Tests skipped because of a hook error may share all suites of the current context
380
+ // or just a part thereof (if it's from a sibling suite).
381
+ var scopes = suites.map(s => context.suiteIdToScope.get(s)).filter(s => Boolean(s));
382
+ var testUuid = _classPrivateFieldGet(_addNewTestResult, this).call(this, context, fullNameSuffix, testResultData, [context.videoScope, ...scopes]);
383
+ _classPrivateFieldGet(_stopExistingTestResult, this).call(this, testUuid, {
384
+ duration,
385
+ retries
386
+ });
387
+ this.allureRuntime.writeTest(testUuid);
388
+ });
389
+ _classPrivateFieldInitSpec(this, _stopTest, (context, _ref11) => {
390
+ var {
391
+ data
392
+ } = _ref11;
393
+ var testUuid = context.test;
394
+ if (testUuid) {
395
+ _classPrivateFieldGet(_stopExistingTestResult, this).call(this, testUuid, data);
396
+ this.allureRuntime.writeTest(testUuid);
397
+ context.test = undefined;
398
+ }
399
+ });
400
+ _classPrivateFieldInitSpec(this, _stopExistingTestResult, (testUuid, _ref12) => {
401
+ var {
402
+ retries,
403
+ duration
404
+ } = _ref12;
405
+ this.allureRuntime.updateTest(testUuid, testResult => {
406
+ if (retries > 0) {
407
+ testResult.parameters.push({
408
+ name: "Retry",
409
+ value: retries.toString(),
410
+ excluded: true
411
+ });
412
+ }
413
+ testResult.stage = _allureJsCommons.Stage.FINISHED;
414
+ });
415
+ this.allureRuntime.stopTest(testUuid, {
416
+ duration
417
+ });
418
+ });
419
+ _classPrivateFieldInitSpec(this, _startCommand, (context, _ref13) => {
420
+ var {
421
+ data: {
422
+ name,
423
+ args
424
+ }
425
+ } = _ref13;
426
+ var rootUuid = _classPrivateFieldGet(_resolveRootUuid, this).call(this, context);
427
+ if (rootUuid) {
428
+ var stepUuid = this.allureRuntime.startStep(rootUuid, undefined, {
429
+ name,
430
+ parameters: args.map((arg, j) => ({
183
431
  name: "Argument [".concat(j, "]"),
184
432
  value: arg
185
433
  }))
186
434
  });
187
- runContext.steps.push(stepUuid);
188
- return;
189
- }
190
- if (message.type === "cypress_command_end") {
191
- var _stepUuid = runContext.steps.pop();
192
- this.allureRuntime.updateStep(_stepUuid, r => {
193
- r.status = message.data.status;
194
- if (message.data.statusDetails) {
195
- r.statusDetails = message.data.statusDetails;
435
+ if (stepUuid) {
436
+ context.commandSteps.push(stepUuid);
437
+ }
438
+ }
439
+ });
440
+ _classPrivateFieldInitSpec(this, _stopCommand, (context, _ref14) => {
441
+ var {
442
+ data: {
443
+ status,
444
+ statusDetails,
445
+ stop
446
+ }
447
+ } = _ref14;
448
+ var stepUuid = context.commandSteps.pop();
449
+ if (stepUuid) {
450
+ this.allureRuntime.updateStep(stepUuid, r => {
451
+ r.status = status;
452
+ if (statusDetails) {
453
+ r.statusDetails = statusDetails;
196
454
  }
197
455
  });
198
- this.allureRuntime.stopStep(_stepUuid);
199
- return;
456
+ this.allureRuntime.stopStep(stepUuid, {
457
+ stop
458
+ });
200
459
  }
201
- this.allureRuntime.applyRuntimeMessages((0, _utils.last)(runContext.executables), [message]);
202
460
  });
203
- if (shouldVideoBeAttached) {
204
- var fixtureUuid = this.allureRuntime.startFixture(runContext.scopes[0], "after", {
205
- name: "Cypress video",
206
- status: _allureJsCommons.Status.PASSED,
207
- stage: _allureJsCommons.Stage.FINISHED
208
- });
209
- this.allureRuntime.writeAttachment(fixtureUuid, undefined, "Cypress video", cypressVideoPath, {
210
- contentType: _allureJsCommons.ContentType.MP4
211
- });
212
- this.allureRuntime.stopFixture(fixtureUuid);
213
- }
214
- if (runContext.globalHooksMessages.length > 0) {
215
- runContext.globalHooksMessages.forEach(message => {
216
- if (message.type === "cypress_hook_start") {
217
- var _fixtureUuid2 = this.allureRuntime.startFixture(runContext.scopes[0], message.data.type, {
218
- name: message.data.name,
219
- start: message.data.start
220
- });
221
- runContext.executables.push(_fixtureUuid2);
222
- return;
223
- }
224
- if (message.type === "cypress_hook_end") {
225
- var _fixtureUuid3 = runContext.executables.pop();
226
- this.allureRuntime.updateFixture(_fixtureUuid3, r => {
227
- r.status = message.data.status;
228
- r.stop = message.data.stop;
229
- if (message.data.statusDetails) {
230
- r.statusDetails = message.data.statusDetails;
231
- }
232
- });
233
- this.allureRuntime.stopFixture(_fixtureUuid3);
234
- }
461
+ _classPrivateFieldInitSpec(this, _applyRuntimeApiMessages, (context, message) => {
462
+ var rootUuid = _classPrivateFieldGet(_resolveRootUuid, this).call(this, context);
463
+ if (rootUuid) {
464
+ this.allureRuntime.applyRuntimeMessages(rootUuid, [message]);
465
+ }
466
+ });
467
+ /**
468
+ * We must defer emitting a test's scope until we receive all the test's `afterEach` hooks.
469
+ * At the same time, we should report it as early as we can. That means we should call this
470
+ * method in the following cases:
471
+ * - when an `after` hook of the test starts (`after` hooks are called later than `afterEach`)
472
+ * - when a `before` or `beforeEach` hook of the next test starts (in case the next test has `before`/`beforeEach` hooks)
473
+ * - when the next test starts (in case the next test doesn't have `before`/`beforeEach` hooks)
474
+ * - when the test's suite ends (in case the test is the last one in its suite, including the root suite of the spec)
475
+ * - when a nested suite starts
476
+ * - when the spec ends
477
+ */
478
+ _classPrivateFieldInitSpec(this, _emitPreviousTestScope, context => {
479
+ var testScope = context.testScope;
480
+
481
+ // Checking the test allows us to tell `beforeEach` and `afterEach` apart.
482
+ // Here we're interested in `afterEach` only.
483
+ if (!context.test && testScope) {
484
+ this.allureRuntime.writeScope(testScope);
485
+ context.testScope = undefined;
486
+ }
487
+ });
488
+ _classPrivateFieldInitSpec(this, _resolveRootUuid, context => {
489
+ var _context$fixture;
490
+ return (_context$fixture = context.fixture) !== null && _context$fixture !== void 0 ? _context$fixture : context.test;
491
+ });
492
+ _classPrivateFieldInitSpec(this, _attachSpecVideo, (context, cypressVideoPath) => {
493
+ var shouldVideoBeAttached = (!this.videoOnFailOnly || context.failed) && cypressVideoPath;
494
+ if (shouldVideoBeAttached) {
495
+ var fixtureUuid = this.allureRuntime.startFixture(context.videoScope, "after", {
496
+ name: "Cypress video",
497
+ status: _allureJsCommons.Status.PASSED,
498
+ stage: _allureJsCommons.Stage.FINISHED
499
+ });
500
+ this.allureRuntime.writeAttachment(fixtureUuid, undefined, "Cypress video", cypressVideoPath, {
501
+ contentType: _allureJsCommons.ContentType.MP4
502
+ });
503
+ this.allureRuntime.stopFixture(fixtureUuid);
504
+ this.allureRuntime.writeScope(context.videoScope);
505
+ }
506
+ });
507
+ _classPrivateFieldInitSpec(this, _emitRemainingScopes, context => {
508
+ _classPrivateFieldGet(_emitPreviousTestScope, this).call(this, context);
509
+ context.suiteScopes.forEach(scope => {
510
+ this.allureRuntime.writeScope(scope);
235
511
  });
236
- }
237
- this.allureRuntime.writeScope(runContext.scopes.pop());
512
+ });
513
+ _classPrivateFieldInitSpec(this, _initializeSpecContext, absolutePath => {
514
+ var specPath = (0, _reporter.getRelativePath)(absolutePath);
515
+ var context = {
516
+ specPath,
517
+ test: undefined,
518
+ fixture: undefined,
519
+ commandSteps: [],
520
+ videoScope: this.allureRuntime.startScope(),
521
+ suiteIdToScope: new Map(),
522
+ suiteScopeToId: new Map(),
523
+ suiteScopes: [],
524
+ testScope: undefined,
525
+ suiteNames: [],
526
+ failed: false
527
+ };
528
+ this.specContextByAbsolutePath.set(absolutePath, context);
529
+ });
530
+ var {
531
+ resultsDir,
532
+ videoOnFailOnly = false
533
+ } = config,
534
+ rest = _objectWithoutProperties(config, _excluded4);
535
+ this.videoOnFailOnly = videoOnFailOnly;
536
+ this.allureRuntime = new _reporter.ReporterRuntime(_objectSpread({
537
+ writer: (0, _reporter.createDefaultWriter)({
538
+ resultsDir
539
+ })
540
+ }, rest));
238
541
  }
239
542
  }
240
543
  exports.AllureCypress = AllureCypress;
241
- var allureCypress = (on, allureConfig) => {
544
+ var createRuntimeState = allureConfig => ({
545
+ config: getRuntimeConfigDefaults(allureConfig),
546
+ initialized: false,
547
+ messages: [],
548
+ testPlan: (0, _reporter.parseTestPlan)(),
549
+ projectDir: (0, _reporter.getProjectRoot)()
550
+ });
551
+ var getRuntimeConfigDefaults = function getRuntimeConfigDefaults() {
552
+ var {
553
+ stepsFromCommands: {
554
+ maxArgumentLength = _utils.DEFAULT_RUNTIME_CONFIG.stepsFromCommands.maxArgumentLength,
555
+ maxArgumentDepth = _utils.DEFAULT_RUNTIME_CONFIG.stepsFromCommands.maxArgumentDepth
556
+ } = _utils.DEFAULT_RUNTIME_CONFIG.stepsFromCommands
557
+ } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _utils.DEFAULT_RUNTIME_CONFIG;
558
+ return {
559
+ stepsFromCommands: {
560
+ maxArgumentDepth,
561
+ maxArgumentLength
562
+ }
563
+ };
564
+ };
565
+ var initializeRuntimeState = (cypressConfig, allureConfig) => {
566
+ cypressConfig.env.allure = createRuntimeState(allureConfig);
567
+ return cypressConfig;
568
+ };
569
+
570
+ /**
571
+ * Sets up Allure Cypress.
572
+ * @param on The function used to subscribe to Cypress Node events (it's the first argument of `setupNodeEvents`).
573
+ * @param cypressConfig The Cypress configuration (the second argument of `setupNodeEvents`). If provided, the selective run feature will be enabled.
574
+ * @param allureConfig An Allure configuration object (optional).
575
+ * @example
576
+ * ```javascript
577
+ * import { defineConfig } from "cypress";
578
+ * import { allureCypress } from "allure-cypress/reporter";
579
+ *
580
+ * export default defineConfig({
581
+ * e2e: {
582
+ * setupNodeEvents: (on, config) => {
583
+ * allureCypress(on, config, { videoOnFailOnly: true });
584
+ * return config;
585
+ * },
586
+ * // ...
587
+ * }
588
+ * });
589
+ * ```
590
+ */
591
+ var allureCypress = (on, cypressConfig, allureConfig) => {
592
+ // Backward compatibility; mainly for JS users who have no type hints
593
+ if (!allureConfig && cypressConfig && !("env" in cypressConfig)) {
594
+ allureConfig = cypressConfig;
595
+ }
596
+ var hasCypressConfig = cypressConfig && "env" in cypressConfig;
242
597
  var allureCypressReporter = new AllureCypress(allureConfig);
243
598
  allureCypressReporter.attachToCypress(on);
244
- on("after:run", results => {
245
- allureCypressReporter.endRun(results);
246
- });
599
+ if (hasCypressConfig) {
600
+ initializeRuntimeState(cypressConfig, allureConfig);
601
+ }
247
602
  return allureCypressReporter;
248
603
  };
249
604
  exports.allureCypress = allureCypress;