testit-adapter-mocha 3.7.9 → 4.0.0-TMS-5.7

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/lib/reporter.d.ts CHANGED
@@ -6,6 +6,7 @@ declare const Reporter: typeof reporters.Base;
6
6
  export declare class TmsReporter extends Reporter {
7
7
  private readonly strategy;
8
8
  private readonly additions;
9
+ private isRunEnded;
9
10
  private attachmentsQueue;
10
11
  private autotestsQueue;
11
12
  private autotestsForTestRun;
package/lib/reporter.js CHANGED
@@ -11,6 +11,8 @@ const step_1 = require("./step");
11
11
  const utils_1 = require("./utils");
12
12
  const Reporter = mocha_1.reporters.Base;
13
13
  const Events = mocha_1.Runner.constants;
14
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
15
+ const superagent = require("superagent");
14
16
  const emptyTest = () => ({
15
17
  autoTestExternalId: "",
16
18
  outcome: "Passed",
@@ -29,9 +31,29 @@ const StateConstants = {
29
31
  STATE_PASSED: 'passed',
30
32
  STATE_PENDING: 'pending',
31
33
  };
34
+ let superagentCallbackGuardInstalled = false;
35
+ const installSuperagentDoubleCallbackGuard = () => {
36
+ if (superagentCallbackGuardInstalled) {
37
+ return;
38
+ }
39
+ superagentCallbackGuardInstalled = true;
40
+ const RequestProto = superagent.Request?.prototype;
41
+ const originalCallback = RequestProto?.callback;
42
+ if (typeof originalCallback !== "function") {
43
+ return;
44
+ }
45
+ RequestProto.callback = function guardedCallback(...args) {
46
+ if (this.__tmsCallbackDone) {
47
+ return;
48
+ }
49
+ this.__tmsCallbackDone = true;
50
+ return originalCallback.apply(this, args);
51
+ };
52
+ };
32
53
  class TmsReporter extends Reporter {
33
54
  constructor(runner, options) {
34
55
  super(runner, options);
56
+ this.isRunEnded = false;
35
57
  this.attachmentsQueue = [];
36
58
  this.autotestsQueue = [];
37
59
  this.autotestsForTestRun = [];
@@ -46,10 +68,18 @@ class TmsReporter extends Reporter {
46
68
  this.runner.on(Events.EVENT_TEST_END, (test) => this.onEndTest(test));
47
69
  };
48
70
  this.onStartRun = () => {
49
- this.strategy.setup();
71
+ (0, deasync_promise_1.default)(this.strategy.setup().catch((err) => {
72
+ console.log("Error setup test run. \n", err?.body ?? err);
73
+ }));
50
74
  };
51
75
  this.onEndRun = () => {
52
- (0, deasync_promise_1.default)(this.teardown());
76
+ if (this.isRunEnded) {
77
+ return;
78
+ }
79
+ this.isRunEnded = true;
80
+ (0, deasync_promise_1.default)(this.teardown().catch((err) => {
81
+ console.log("Error during teardown. \n", err?.body ?? err);
82
+ }));
53
83
  };
54
84
  this.addAttachments = (pathsOrContent, fileName) => {
55
85
  const target = this.currentType === "test" ? this.currentTest : this.currentStep;
@@ -58,6 +88,8 @@ class TmsReporter extends Reporter {
58
88
  : this.additions.addAttachments(pathsOrContent);
59
89
  promise.then((attachments) => {
60
90
  target.attachments?.push(...attachments);
91
+ }).catch((err) => {
92
+ console.log("Error loading attachment. \n", err?.body ?? err?.error ?? err);
61
93
  });
62
94
  this.attachmentsQueue.push(promise);
63
95
  return promise;
@@ -92,6 +124,7 @@ class TmsReporter extends Reporter {
92
124
  this.currentType = prevType;
93
125
  this.currentStep = emptyStep();
94
126
  };
127
+ installSuperagentDoubleCallbackGuard();
95
128
  const config = new testit_js_commons_1.ConfigComposer().compose(options?.tmsOptions);
96
129
  this.strategy = testit_js_commons_1.StrategyFactory.create(config);
97
130
  this.additions = new testit_js_commons_1.Additions(config);
@@ -104,13 +137,17 @@ class TmsReporter extends Reporter {
104
137
  }
105
138
  async teardown() {
106
139
  await Promise.all(this.attachmentsQueue).catch((err) => {
107
- console.log("Error loading attachments. \n", err.body);
140
+ console.log("Error loading attachments. \n", err?.body ?? err);
141
+ });
142
+ await Promise.all(this.autotestsQueue).catch((err) => {
143
+ console.log("Error load autotests. \n", err?.body ?? err);
108
144
  });
109
- await Promise.all(this.autotestsQueue);
110
145
  await this.strategy.loadTestRun(this.autotestsForTestRun).catch((err) => {
111
- console.log("Error load test run. \n", err.body);
146
+ console.log("Error load test run. \n", err?.body ?? err);
147
+ });
148
+ await this.strategy.teardown().catch((err) => {
149
+ console.log("Error complete test run. \n", err?.body ?? err);
112
150
  });
113
- await this.strategy.teardown();
114
151
  }
115
152
  clearContext(ctx) {
116
153
  ctx.externalId = undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "testit-adapter-mocha",
3
- "version": "3.7.9",
3
+ "version": "4.0.0-TMS-5.7",
4
4
  "description": "Mocha adapter for Test IT",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/types.d.ts",
@@ -31,7 +31,7 @@
31
31
  "dependencies": {
32
32
  "mocha": "^10.2.0",
33
33
  "deasync-promise": "^1.0.1",
34
- "testit-js-commons": "3.7.9"
34
+ "testit-js-commons": "4.0.0-TMS-5.7"
35
35
  },
36
36
  "files": [
37
37
  "lib"