@testomatio/reporter 1.4.1-beta-1 → 1.4.1

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 (45) hide show
  1. package/README.md +60 -57
  2. package/lib/adapter/codecept.js +86 -27
  3. package/lib/adapter/cucumber/current.js +17 -10
  4. package/lib/adapter/cucumber/legacy.js +5 -4
  5. package/lib/adapter/cucumber.js +2 -2
  6. package/lib/adapter/cypress-plugin/index.js +53 -26
  7. package/lib/adapter/jasmine.js +2 -2
  8. package/lib/adapter/jest.js +48 -10
  9. package/lib/adapter/mocha.js +90 -10
  10. package/lib/adapter/playwright.js +68 -18
  11. package/lib/adapter/webdriver.js +47 -2
  12. package/lib/bin/reportXml.js +21 -16
  13. package/lib/bin/startTest.js +12 -12
  14. package/lib/client.js +112 -52
  15. package/lib/config.js +34 -0
  16. package/lib/constants.js +28 -1
  17. package/lib/data-storage.js +203 -0
  18. package/lib/fileUploader.js +66 -53
  19. package/lib/junit-adapter/adapter.js +0 -2
  20. package/lib/junit-adapter/csharp.js +3 -4
  21. package/lib/junit-adapter/index.js +3 -3
  22. package/lib/junit-adapter/java.js +11 -11
  23. package/lib/junit-adapter/javascript.js +1 -2
  24. package/lib/junit-adapter/python.js +12 -14
  25. package/lib/junit-adapter/ruby.js +1 -2
  26. package/lib/pipe/csv.js +35 -32
  27. package/lib/pipe/github.js +4 -3
  28. package/lib/pipe/gitlab.js +17 -10
  29. package/lib/pipe/html.js +361 -0
  30. package/lib/pipe/index.js +3 -1
  31. package/lib/pipe/testomatio.js +230 -51
  32. package/lib/reporter-functions.js +12 -9
  33. package/lib/reporter.js +8 -12
  34. package/lib/services/artifacts.js +57 -0
  35. package/lib/services/index.js +13 -0
  36. package/lib/{storages/key-value-storage.js → services/key-values.js} +19 -19
  37. package/lib/{storages → services}/logger.js +58 -37
  38. package/lib/template/emptyData.svg +23 -0
  39. package/lib/template/testomatio.hbs +1421 -0
  40. package/lib/utils/pipe_utils.js +73 -79
  41. package/lib/utils/utils.js +53 -40
  42. package/lib/xmlReader.js +113 -105
  43. package/package.json +13 -7
  44. package/lib/storages/artifact-storage.js +0 -70
  45. package/lib/storages/data-storage.js +0 -307
@@ -1,307 +0,0 @@
1
- const debug = require('debug')('@testomatio/reporter:storage');
2
- const fs = require('fs');
3
- const path = require('path');
4
- const os = require('os');
5
- const { join } = require('path');
6
- const { TESTOMAT_TMP_STORAGE_DIR } = require('../constants');
7
- const { fileSystem, jestHelpers, parseTest } = require('../utils/utils');
8
-
9
- const getTestIdFromTestTitle = parseTest;
10
-
11
- class DataStorage {
12
- /**
13
- * Creates data storage instance for specific data type.
14
- * Stores data to global variable or to file depending on what is applicable for current test runner
15
- * (running environment).
16
- * Recommend to use composition while using this class (instead of inheritance).
17
- * ! Also the class which will use data storage should be singleton (to avoid data loss).
18
- * @param {*} dataType storage type (like 'log', 'artifact'); could be any string, used only to define file path
19
- */
20
- constructor(dataType) {
21
- this.dataType = dataType || 'data';
22
- this.isFileStorage = true;
23
- this.#refreshStorageType();
24
-
25
- this.dataDirPath = path.join(TESTOMAT_TMP_STORAGE_DIR, this.dataType);
26
-
27
- // set test title for mocha (could not be moved to adapter)
28
- // it does not override existing hook, just add new one
29
- try {
30
- // @ts-ignore
31
- if (!beforeEach) return;
32
- // @ts-ignore-next-line
33
- beforeEach(function () {
34
- if (this.currentTest?.__mocha_id__) {
35
- global.testTitle = this.currentTest.fullTitle();
36
- }
37
- });
38
- } catch (e) {
39
- // ignore
40
- }
41
-
42
- // set test title for playwright
43
- try {
44
- // eslint-disable-next-line import/no-extraneous-dependencies, import/no-unresolved
45
- const { test } = require('@playwright/test');
46
- // eslint-disable-next-line no-empty-pattern
47
- test.beforeEach(async ({}, testInfo) => {
48
- global.testTitle = testInfo.title;
49
- global.testomatioRunningEnvironment = 'playwright';
50
- });
51
- } catch (e) {
52
- // ignore
53
- }
54
- }
55
-
56
- /**
57
- * Try to define the running environment. Not 100% reliable and used as additional check
58
- * @returns jest | mocha | ...
59
- */
60
- getRunningEnviroment() {
61
- // jest
62
- if (process.env.JEST_WORKER_ID) return 'jest';
63
-
64
- if (global.codeceptjs) return 'codeceptjs';
65
-
66
- // 'cucumber:current', 'cucumber:legacy'
67
- if (global.testomatioRunningEnvironment) return global.testomatioRunningEnvironment;
68
-
69
- if (process.env.PLAYWRIGHT_TEST_BASE_URL || process.env.TEST_WORKER_INDEX) return 'playwright';
70
-
71
- // mocha - can't detect
72
- return null;
73
- }
74
-
75
- /**
76
- * Puts any data to storage (file or global variable).
77
- * If file: stores data as text, if global variable – stores as array of data.
78
- * @param {*} data anything you want to store (string, object, array, etc)
79
- * @param {*} context could be testId or any context (test, suite, etc) which is used to define testId
80
- * @returns
81
- */
82
- putData(data, context = null) {
83
- fileSystem.createDir(this.dataDirPath);
84
-
85
- this.#refreshStorageType();
86
- context = context ?? global.testTitle ?? null;
87
- let testId = this.#tryToRetrieveTestId(context) || context || null;
88
-
89
- if (this.runningEnvironment === 'codeceptjs') {
90
- this.isFileStorage = false;
91
- testId = testId ?? global.testomatioDataStore?.currentlyRunningTestId;
92
- }
93
-
94
- if (this.runningEnvironment === 'jest') {
95
- testId = testId ?? jestHelpers.getIdOfCurrentlyRunningTest();
96
- }
97
-
98
- // logs in playwright are gathered by pw framework itself
99
- if (this.runningEnvironment === 'playwright' && this.dataType === 'log') return;
100
-
101
- // get id from global store
102
- if (global.testomatioDataStore && global.testomatioDataStore.currentlyRunningTestId)
103
- testId = testId ?? global.testomatioDataStore?.currentlyRunningTestId;
104
-
105
- if (!testId && global?.currentlyRunningTestTitle)
106
- testId = this.#tryToRetrieveTestId(global.currentlyRunningTestTitle);
107
-
108
- if (!testId) {
109
- debug(`No test id provided for "${this.dataType}" data:`, data);
110
- return;
111
- }
112
-
113
- if (this.isFileStorage) {
114
- this.#putDataToFile(data, testId);
115
- } else {
116
- this.#putDataToGlobalVar(data, testId);
117
- }
118
- }
119
-
120
- /**
121
- * Returns data, stored for specific testId (or data which was stored without test id specified).
122
- * This method will get data from global variable and/or from from file (previosly saved with put method).
123
- *
124
- * @param {*} context could be testId or any context (test title, suite, etc) which is used to define testId
125
- * @returns array of data (any type), null (if no data found for test) or string (if data type is log)
126
- */
127
- getData(context) {
128
- this.#refreshStorageType();
129
-
130
- let testId = null;
131
- if (typeof context === 'string') {
132
- testId = this.#tryToRetrieveTestId(context) || context;
133
- }
134
-
135
- if (!testId) {
136
- debug(`Cannot get test id from passed context:`, context);
137
- return null;
138
- }
139
-
140
- let testDataFromFile = [];
141
- let testDataFromGlobalVar = [];
142
-
143
- if (global?.testomatioDataStore) {
144
- testDataFromGlobalVar = this.#getDataFromGlobalVar(testId);
145
- // these frameworks use global variable storage
146
- // if (testDataFromGlobalVar && ['cucumber', 'codeceptjs'].includes(this.runningEnvironment)) {
147
- if (testDataFromGlobalVar) {
148
- // return as string if its log data
149
- if (this.dataType === 'log' && testDataFromGlobalVar.length) return testDataFromGlobalVar.join('\n');
150
- // return as array for other data types
151
- if (testDataFromGlobalVar.length) return testDataFromGlobalVar;
152
- }
153
- // don't return nothing if no data in global variable
154
- }
155
-
156
- /* condition is removed for mocha
157
- mocha has created a global storage, but just in some cases, generally it is not available
158
- */
159
- // if (this.isFileStorage || !testData) {
160
- // testData = this.#getDataFromFile(testId);
161
- // if (testData) return testData;
162
- // }
163
-
164
- testDataFromFile = this.#getDataFromFile(testId);
165
-
166
- if (testDataFromFile.length) {
167
- if (this.dataType === 'log') return testDataFromFile.join('\n');
168
- return testDataFromFile;
169
- }
170
- debug(`No "${this.dataType}" data for test id ${testId} in both file and global variable`);
171
-
172
- // in case no data found for test
173
- return null;
174
- }
175
-
176
- /**
177
- * This method is named as "try" because it does not guarantee that test id could be retrieved.
178
- * Context could be anything (test, suite, string, etc) which is used to define testId.
179
- * Or it could represent any other entity (which does not contain test id).
180
- * @param {*} context
181
- */
182
- #tryToRetrieveTestId(context) {
183
- if (!context) return null;
184
- this.#refreshStorageType();
185
-
186
- if (this.runningEnvironment === 'playwright' || context?.title) {
187
- // context is testInfo
188
- const testId = getTestIdFromTestTitle(context.title);
189
- if (testId) return testId;
190
- }
191
-
192
- if (typeof context === 'string') {
193
- const testId = getTestIdFromTestTitle(context);
194
- if (testId) return testId;
195
- }
196
-
197
- return null;
198
- }
199
-
200
- /**
201
- * Refreshes storage type (file or global variable) depending on current environment
202
- * This method should be run on each attempt to put or get data from/to storage
203
- * Because storage instance is created before the test runner is started. And storage type could be changed
204
- */
205
- #refreshStorageType() {
206
- this.runningEnvironment = this.getRunningEnviroment();
207
-
208
- // some test frameworks do not persist global variables, thus file storage is used for them (by default)
209
- if (this.runningEnvironment === 'codeceptjs') this.isFileStorage = false;
210
- // playwrigth stores logs by itself; but file storage is used for other data types for playwright
211
- if (this.runningEnvironment === 'playwright' && this.dataType === 'log') this.isFileStorage = false;
212
- }
213
-
214
- /**
215
- * @param {*} testId
216
- * @returns aray of data (any type)
217
- */
218
- #getDataFromGlobalVar(testId) {
219
- try {
220
- if (global?.testomatioDataStore[this.dataType]) {
221
- const testData = global.testomatioDataStore[this.dataType][testId];
222
- if (testData) debug(`"${this.dataType}" data for test id ${testId}:`, testData.join(', '));
223
- return testData || [];
224
- }
225
- // debug(`No ${this.dataType} data for test id ${testId} in <global> storage`);
226
- return [];
227
- } catch (e) {
228
- // there could be no data, ignore
229
- }
230
- }
231
-
232
- /**
233
- *
234
- * @param {*} testId
235
- * @returns array of data (any type)
236
- */
237
- #getDataFromFile(testId) {
238
- try {
239
- const filepath = join(this.dataDirPath, `${this.dataType}_${testId}`);
240
- if (fs.existsSync(filepath)) {
241
- const testDataAsText = fs.readFileSync(filepath, 'utf-8');
242
- if (testDataAsText) debug(`"${this.dataType}" data for test id ${testId}:`, testDataAsText);
243
- const testDataArr = testDataAsText?.split(os.EOL) || [];
244
- return testDataArr;
245
- }
246
- // debug(`No ${this.dataType} data for test id ${testId} in <file> storage`);
247
- return [];
248
- } catch (e) {
249
- // there could be no data, ignore
250
- }
251
- return [];
252
- }
253
-
254
- /**
255
- * Puts data to global variable. Unlike the file storage, stores data in array (file storage just append as string).
256
- * @param {*} data
257
- * @param {*} testId
258
- */
259
- #putDataToGlobalVar(data, testId) {
260
- debug(`Saving data to global variable for test ${testId}:`, data);
261
- if (!global.testomatioDataStore) global.testomatioDataStore = {};
262
- if (!global.testomatioDataStore?.[this.dataType]) global.testomatioDataStore[this.dataType] = {};
263
-
264
- if (!global.testomatioDataStore?.[this.dataType][testId]) global.testomatioDataStore[this.dataType][testId] = [];
265
- global.testomatioDataStore[this.dataType][testId].push(data);
266
- }
267
-
268
- #putDataToFile(data, testId) {
269
- if (typeof data !== 'string') data = JSON.stringify(data);
270
- const filename = `${this.dataType}_${testId}`;
271
- const filepath = join(this.dataDirPath, filename);
272
- if (!fs.existsSync(this.dataDirPath)) fileSystem.createDir(this.dataDirPath);
273
- debug('Saving data to file for test', testId, 'to', filepath, ':\n', data, '\n');
274
-
275
- // append new line if file already exists (in this case its definitely includes some data)
276
- if (fs.existsSync(filepath)) {
277
- fs.appendFileSync(filepath, os.EOL + data, 'utf-8');
278
- } else {
279
- fs.writeFileSync(filepath, data, 'utf-8');
280
- }
281
- }
282
- }
283
-
284
- module.exports = DataStorage;
285
-
286
- // TODO: consider using fs promises instead of writeSync/appendFileSync to
287
- // prevent blocking and improve performance (probably queue usage will be required)
288
-
289
- // TODO: rewrite client - everything regarding storing artifacts
290
- // TODO: try to define adapter inside client
291
- // TODO: ability to intercept multiple loggers (upd: no need)
292
-
293
- /* does not work for Playwright
294
- try {
295
- const { test } = require('@playwright/test');
296
- test.beforeEach(async ({}, testInfo) => {
297
- global.testTitle = testInfo.title;
298
- global.testomatioRunningEnvironment = 'playwright';
299
- });
300
- } catch (e) {
301
- // ignore
302
- }
303
- // Playwright storage works fine only when running tests from a single file;
304
- // otherwise it is not possible to define test id/title, it is overwritten
305
- TODO: the only way implementation for Pw for now is to pass testInfo inside test
306
- // (and process this testInfo inside "put" function)
307
- */