testit-adapter-mocha 4.0.4 → 4.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.
package/README.md CHANGED
@@ -21,6 +21,10 @@ module.exports = {
21
21
 
22
22
  ## Configuration
23
23
 
24
+ #### Log level
25
+
26
+ The adapter includes a custom logger; the default level is `warn`. Set another level at runtime with the `LOG_LEVEL` environment variable: `error`, `warn`, `info`, or `debug`.
27
+
24
28
  | Description | File property | Environment variable |
25
29
  |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------|--------------------------------------------|
26
30
  | Location of the TMS instance | url | TMS_URL |
@@ -32,6 +36,7 @@ module.exports = {
32
36
  | Adapter mode. Default value - 1. The adapter supports following modes:<br>1 - in this mode, the adapter sends all results to the test run without filtering or [with filtering CLI](#run-with-filter)<br/>2 - in this mode, the adapter creates a new test run and sends results to the new test run | adapterMode | TMS_ADAPTER_MODE |
33
37
  | Mode of automatic creation test cases (**It's optional**). Default value - false. The adapter supports following modes:<br/>true - in this mode, the adapter will create a test case linked to the created autotest (not to the updated autotest)<br/>false - in this mode, the adapter will not create a test case | automaticCreationTestCases | TMS_AUTOMATIC_CREATION_TEST_CASES |
34
38
  | Mode of automatic updation links to test cases (**It's optional**). Default value - false. The adapter supports following modes:<br/>true - in this mode, the adapter will update links to test cases<br/>false - in this mode, the adapter will not update link to test cases | automaticUpdationLinksToTestCases | TMS_AUTOMATIC_UPDATION_LINKS_TO_TEST_CASES |
39
+ | Logger level, default value is `warn`, available values: [`error`, `warn`, `info`, `debug`] | | LOG_LEVEL |
35
40
 
36
41
  ### File
37
42
 
package/lib/reporter.d.ts CHANGED
@@ -6,8 +6,10 @@ 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 readonly importRealtime;
9
10
  private isRunEnded;
10
11
  private attachmentsQueue;
12
+ private currentTestAttachmentsQueue;
11
13
  private autotestsQueue;
12
14
  private autotestsForTestRun;
13
15
  private currentType?;
package/lib/reporter.js CHANGED
@@ -9,6 +9,7 @@ const deasync_promise_1 = __importDefault(require("deasync-promise"));
9
9
  const testit_js_commons_1 = require("testit-js-commons");
10
10
  const step_1 = require("./step");
11
11
  const utils_1 = require("./utils");
12
+ const testit_js_commons_2 = require("testit-js-commons");
12
13
  const Reporter = mocha_1.reporters.Base;
13
14
  const Events = mocha_1.Runner.constants;
14
15
  // eslint-disable-next-line @typescript-eslint/no-var-requires
@@ -55,6 +56,7 @@ class TmsReporter extends Reporter {
55
56
  super(runner, options);
56
57
  this.isRunEnded = false;
57
58
  this.attachmentsQueue = [];
59
+ this.currentTestAttachmentsQueue = [];
58
60
  this.autotestsQueue = [];
59
61
  this.autotestsForTestRun = [];
60
62
  this.currentTest = emptyTest();
@@ -69,7 +71,7 @@ class TmsReporter extends Reporter {
69
71
  };
70
72
  this.onStartRun = () => {
71
73
  (0, deasync_promise_1.default)(this.strategy.setup().catch((err) => {
72
- console.log("Error setup test run. \n", err?.body ?? err);
74
+ testit_js_commons_2.logger.log("Error setup test run. \n", err?.body ?? err);
73
75
  }));
74
76
  };
75
77
  this.onEndRun = () => {
@@ -78,7 +80,7 @@ class TmsReporter extends Reporter {
78
80
  }
79
81
  this.isRunEnded = true;
80
82
  (0, deasync_promise_1.default)(this.teardown().catch((err) => {
81
- console.log("Error during teardown. \n", err?.body ?? err);
83
+ testit_js_commons_2.logger.log("Error during teardown. \n", err?.body ?? err);
82
84
  }));
83
85
  };
84
86
  this.addAttachments = (pathsOrContent, fileName) => {
@@ -89,9 +91,10 @@ class TmsReporter extends Reporter {
89
91
  promise.then((attachments) => {
90
92
  target.attachments?.push(...attachments);
91
93
  }).catch((err) => {
92
- console.log("Error loading attachment. \n", err?.body ?? err?.error ?? err);
94
+ testit_js_commons_2.logger.log("Error loading attachment. \n", err?.body ?? err?.error ?? err);
93
95
  });
94
96
  this.attachmentsQueue.push(promise);
97
+ this.currentTestAttachmentsQueue.push(promise);
95
98
  return promise;
96
99
  };
97
100
  this.addLinks = (links) => {
@@ -112,7 +115,7 @@ class TmsReporter extends Reporter {
112
115
  catch (err) {
113
116
  this.currentStep.outcome = "Failed";
114
117
  this.currentTest.outcome = "Failed";
115
- console.log("Step failed. \n", err);
118
+ testit_js_commons_2.logger.log("Step failed. \n", err);
116
119
  }
117
120
  this.currentStep.title = step.title;
118
121
  this.currentStep.description = step.description;
@@ -126,6 +129,8 @@ class TmsReporter extends Reporter {
126
129
  };
127
130
  installSuperagentDoubleCallbackGuard();
128
131
  const config = new testit_js_commons_1.ConfigComposer().compose(options?.tmsOptions);
132
+ this.importRealtime = Boolean(config.importRealtime);
133
+ testit_js_commons_2.logger.debug("[mocha] reporter init", { importRealtime: this.importRealtime });
129
134
  this.strategy = testit_js_commons_1.StrategyFactory.create(config);
130
135
  this.additions = new testit_js_commons_1.Additions(config);
131
136
  if (options.parallel) {
@@ -137,16 +142,22 @@ class TmsReporter extends Reporter {
137
142
  }
138
143
  async teardown() {
139
144
  await Promise.all(this.attachmentsQueue).catch((err) => {
140
- console.log("Error loading attachments. \n", err?.body ?? err);
145
+ testit_js_commons_2.logger.log("Error loading attachments. \n", err?.body ?? err);
141
146
  });
142
147
  await Promise.all(this.autotestsQueue).catch((err) => {
143
- console.log("Error load autotests. \n", err?.body ?? err);
144
- });
145
- await this.strategy.loadTestRun(this.autotestsForTestRun).catch((err) => {
146
- console.log("Error load test run. \n", err?.body ?? err);
148
+ testit_js_commons_2.logger.log("Error load autotests. \n", err?.body ?? err);
147
149
  });
150
+ if (!this.importRealtime) {
151
+ testit_js_commons_2.logger.debug("[mocha] onEndRun batch loadTestRun", { count: this.autotestsForTestRun.length });
152
+ await this.strategy.loadTestRun(this.autotestsForTestRun).catch((err) => {
153
+ testit_js_commons_2.logger.log("Error load test run. \n", err?.body ?? err);
154
+ });
155
+ }
156
+ else {
157
+ testit_js_commons_2.logger.debug("[mocha] onEndRun skip batch (importRealtime)");
158
+ }
148
159
  await this.strategy.teardown().catch((err) => {
149
- console.log("Error complete test run. \n", err?.body ?? err);
160
+ testit_js_commons_2.logger.log("Error complete test run. \n", err?.body ?? err);
150
161
  });
151
162
  }
152
163
  clearContext(ctx) {
@@ -173,6 +184,7 @@ class TmsReporter extends Reporter {
173
184
  onStartTest() {
174
185
  this.currentType = "test";
175
186
  this.currentTest.startedOn = new Date();
187
+ this.currentTestAttachmentsQueue = [];
176
188
  }
177
189
  onEndTest(test) {
178
190
  const hooks = (0, utils_1.extractHooks)(test.parent);
@@ -194,26 +206,42 @@ class TmsReporter extends Reporter {
194
206
  workItemIds: test.ctx?.workItemsIds,
195
207
  externalKey: test.title,
196
208
  };
197
- const promise = this.strategy.loadAutotest(autotestPost, this._getOutcome(test.state));
198
- this.autotestsQueue.push(promise);
199
- this.autotestsForTestRun.push({
209
+ const outcome = this._getOutcome(test.state);
210
+ const currentTestSnapshot = this.currentTest;
211
+ const additionsLinksSnapshot = [...this.additions.links];
212
+ const additionsMessagesSnapshot = [...this.additions.messages];
213
+ const testRunResult = {
200
214
  autoTestExternalId: autotestPost.externalId,
201
- outcome: this._getOutcome(test.state),
202
- startedOn: this.currentTest.startedOn,
215
+ outcome,
216
+ startedOn: currentTestSnapshot.startedOn,
203
217
  completedOn: new Date(),
204
218
  duration: this._getDuration(test.duration),
205
- stepResults: this.currentTest.stepResults,
219
+ stepResults: currentTestSnapshot.stepResults,
206
220
  setupResults: setup,
207
221
  teardownResults: teardown,
208
- attachments: this.currentTest.attachments,
209
- links: this.additions.links,
222
+ attachments: currentTestSnapshot.attachments,
223
+ links: additionsLinksSnapshot,
210
224
  message: test.err?.message
211
- ? this.additions.messages.concat(test.err.message).join("\n")
212
- : this.additions.messages.join("\n"),
225
+ ? additionsMessagesSnapshot.concat(test.err.message).join("\n")
226
+ : additionsMessagesSnapshot.join("\n"),
213
227
  traces: test.err?.stack,
214
228
  parameters: test.ctx?.parameters,
215
229
  properties: test.ctx?.properties,
230
+ };
231
+ const promise = Promise.allSettled(this.currentTestAttachmentsQueue).then(async () => {
232
+ await this.strategy.loadAutotest(autotestPost, outcome);
233
+ if (this.importRealtime) {
234
+ testit_js_commons_2.logger.debug("[mocha] onEndTest realtime", {
235
+ externalId: autotestPost.externalId,
236
+ outcome,
237
+ teardownSteps: teardown.length,
238
+ });
239
+ await this.strategy.loadTestRun([testRunResult]);
240
+ return;
241
+ }
242
+ this.autotestsForTestRun.push(testRunResult);
216
243
  });
244
+ this.autotestsQueue.push(promise);
217
245
  this.resetTest(test);
218
246
  }
219
247
  resetTest(test) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "testit-adapter-mocha",
3
- "version": "4.0.4",
3
+ "version": "4.1.0",
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": "4.0.4"
34
+ "testit-js-commons": "4.1.0"
35
35
  },
36
36
  "files": [
37
37
  "lib"