@jahia/cypress 6.0.0 → 6.2.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.
@@ -4,3 +4,4 @@ export * from './login';
4
4
  export * from './logout';
5
5
  export * from './repeatUntil';
6
6
  export * from './testStep';
7
+ export * from './jsErrorsLogger';
@@ -16,3 +16,4 @@ __exportStar(require("./login"), exports);
16
16
  __exportStar(require("./logout"), exports);
17
17
  __exportStar(require("./repeatUntil"), exports);
18
18
  __exportStar(require("./testStep"), exports);
19
+ __exportStar(require("./jsErrorsLogger"), exports);
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Module for monitoring and reporting JavaScript errors and warnings in Cypress tests.
3
+ * Provides methods to enable, disable, and check logger status.
4
+ */
5
+ /**
6
+ * Strategy for handling JavaScript errors and warnings in Cypress tests.
7
+ * - failFast: Fail *immediately* when an error is detected.
8
+ * - failAfterEach: Collect all errors and warnings *during test* execution and fail if any issues are found.
9
+ * - failAfterAll: Collect all errors and warnings *after all tests* and fail at the end of the test suite.
10
+ */
11
+ declare enum STRATEGY {
12
+ failFast = 0,
13
+ failAfterAll = 1,
14
+ failAfterEach = 2
15
+ }
16
+ /**
17
+ * Sets the strategy for handling JavaScript errors and warnings in Cypress tests.
18
+ * @param {STRATEGY} strategy - Strategy for handling JavaScript errors and warnings.
19
+ * @throws {Error} If an invalid strategy is provided.
20
+ * @returns {void}
21
+ */
22
+ declare function setStrategy(strategy: STRATEGY): void;
23
+ /**
24
+ * Sets the list of allowed warnings that will not be reported by the logger.
25
+ * @param warnings {string[]} - Array of warning messages to be allowed.
26
+ * @return {void}
27
+ */
28
+ declare function setAllowedJsWarnings(warnings: string[]): void;
29
+ /**
30
+ * Attaches custom hooks to Cypress events to monitor and report JavaScript errors and warnings.
31
+ * This method is called automatically in registerSupport.ts#registerSupport
32
+ * It sets up listeners for console errors and warnings, collects them after each test,
33
+ * and throws an error if any issues are found after all tests are executed.
34
+ */
35
+ declare function attachHooks(): void;
36
+ /**
37
+ * Exports the jsLogger module with methods to attach hooks, enable/disable logging, and set allowed warnings.
38
+ */
39
+ export declare const jsErrorsLogger: {
40
+ attachHooks: typeof attachHooks;
41
+ setAllowedJsWarnings: typeof setAllowedJsWarnings;
42
+ setStrategy: typeof setStrategy;
43
+ STRATEGY: typeof STRATEGY;
44
+ };
45
+ export {};
@@ -0,0 +1,280 @@
1
+ "use strict";
2
+ /* eslint-disable brace-style */
3
+ /* eslint-disable max-statements-per-line */
4
+ /**
5
+ * Module for monitoring and reporting JavaScript errors and warnings in Cypress tests.
6
+ * Provides methods to enable, disable, and check logger status.
7
+ */
8
+ var __spreadArray = (this && this.__spreadArray) || function (to, from) {
9
+ for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
10
+ to[j] = from[i];
11
+ return to;
12
+ };
13
+ exports.__esModule = true;
14
+ exports.jsErrorsLogger = void 0;
15
+ var envVarDisabled = 'JS_LOGGER_DISABLED';
16
+ var envVarCollector = '__JS_LOGGER_FAILURES__';
17
+ var envVarAllowedWarnings = '__JS_LOGGER_ALLOWED_WARNINGS__';
18
+ var envVarStrategy = '__JS_LOGGER_STRATEGY__';
19
+ /**
20
+ * Strategy for handling JavaScript errors and warnings in Cypress tests.
21
+ * - failFast: Fail *immediately* when an error is detected.
22
+ * - failAfterEach: Collect all errors and warnings *during test* execution and fail if any issues are found.
23
+ * - failAfterAll: Collect all errors and warnings *after all tests* and fail at the end of the test suite.
24
+ */
25
+ var STRATEGY;
26
+ (function (STRATEGY) {
27
+ STRATEGY[STRATEGY["failFast"] = 0] = "failFast";
28
+ STRATEGY[STRATEGY["failAfterAll"] = 1] = "failAfterAll";
29
+ STRATEGY[STRATEGY["failAfterEach"] = 2] = "failAfterEach";
30
+ })(STRATEGY || (STRATEGY = {}));
31
+ /**
32
+ * Returns the current strategy for handling JavaScript errors and warnings in Cypress tests.
33
+ * @returns {STRATEGY} - The current strategy for handling JavaScript errors and warnings.
34
+ * @note be careful with Cypress.env(envVarStrategy), since it might return `0` for `failFast` strategy,
35
+ * which is falsy in JavaScript, so we need to check if the variable is undefined.
36
+ */
37
+ function getStrategy() {
38
+ return typeof Cypress.env(envVarStrategy) === 'undefined' ? STRATEGY.failFast : Cypress.env(envVarStrategy);
39
+ }
40
+ /**
41
+ * Sets the strategy for handling JavaScript errors and warnings in Cypress tests.
42
+ * @param {STRATEGY} strategy - Strategy for handling JavaScript errors and warnings.
43
+ * @throws {Error} If an invalid strategy is provided.
44
+ * @returns {void}
45
+ */
46
+ function setStrategy(strategy) { Cypress.env(envVarStrategy, strategy); }
47
+ /**
48
+ * Checks if the js errors and warnings logger is disabled.
49
+ * @returns {boolean} - true if the logger is disabled, false otherwise.
50
+ */
51
+ function isDisabled() { return Cypress.env(envVarDisabled) === true; }
52
+ /**
53
+ * Sets the list of allowed warnings that will not be reported by the logger.
54
+ * @param warnings {string[]} - Array of warning messages to be allowed.
55
+ * @return {void}
56
+ */
57
+ function setAllowedJsWarnings(warnings) { Cypress.env(envVarAllowedWarnings, warnings); }
58
+ /**
59
+ * Attaches custom hooks to Cypress events to monitor and report JavaScript errors and warnings.
60
+ * This method is called automatically in registerSupport.ts#registerSupport
61
+ * It sets up listeners for console errors and warnings, collects them after each test,
62
+ * and throws an error if any issues are found after all tests are executed.
63
+ */
64
+ function attachHooks() {
65
+ // Skip hook attachment if the logger is disabled (e.g. from CI/CD pipeline)
66
+ if (isDisabled()) {
67
+ return;
68
+ }
69
+ switch (getStrategy()) {
70
+ case STRATEGY.failAfterAll:
71
+ attachFailAfterAll();
72
+ break;
73
+ case STRATEGY.failAfterEach:
74
+ attachFailAfterEach();
75
+ break;
76
+ default:
77
+ attachFailFast();
78
+ }
79
+ }
80
+ /**
81
+ * Custom hook implementation which fails the test after all tests are executed if any JavaScript errors or warnings are found.
82
+ *
83
+ * Proc: Allows all tests to run, collects console errors and warnings, and fails the test suite at the end if any issues are found.
84
+ * This is useful for reporting all issues at once after all tests are executed, rather than failing immediately on the first issue.
85
+ * Cons: Reporting might be confusing, e.g. - cypress will report the very last test as failed, while many tests might have issues.
86
+ * This is because the hook is executed after all tests are completed, so the last test is reported as failed.
87
+ */
88
+ function attachFailAfterAll() {
89
+ /**
90
+ * Custom 'window:before:load' hook to spy on console errors and warnings
91
+ * This hook is executed before the page is loaded, allowing us to capture console messages.
92
+ */
93
+ Cypress.on('window:before:load', function (window) {
94
+ // Skip 'window:before:load' hook if the logger is not enabled
95
+ // Double-check in case if functionality was disabled within the test-case code
96
+ // to skip js validations for some particular test and enable it afterward
97
+ if (isDisabled()) {
98
+ return;
99
+ }
100
+ cy.spy(window.console, 'error').as('errors');
101
+ cy.spy(window.console, 'warn').as('warnings');
102
+ });
103
+ /**
104
+ * Custom 'afterEach' hook to collect console errors and warnings
105
+ */
106
+ afterEach(function () {
107
+ // Skip 'afterEach' hook if the logger is not enabled
108
+ // Double-check in case if functionality was disabled within the test-case code
109
+ // to skip js validations for some particular test and enable it afterward
110
+ if (isDisabled()) {
111
+ return;
112
+ }
113
+ var consoleIssues = [];
114
+ var allowedWarnings = Cypress.env(envVarAllowedWarnings) || [];
115
+ // All errors should be collected
116
+ cy.get('@errors')
117
+ .invoke('getCalls')
118
+ .then(function (calls) { consoleIssues = calls; });
119
+ // Only warnings not in the allowed list should be collected
120
+ cy.get('@warnings')
121
+ .invoke('getCalls')
122
+ .each(function (call) {
123
+ call.args.forEach(function (arg) {
124
+ if (!allowedWarnings.some(function (item) { return arg.includes(item); })) {
125
+ consoleIssues.push(arg);
126
+ }
127
+ });
128
+ });
129
+ // Collect all issues and store them in the Cypress environment variable for later usage
130
+ cy.then(function () {
131
+ if (consoleIssues.length > 0) {
132
+ Cypress.env(envVarCollector, __spreadArray(__spreadArray([], (Cypress.env(envVarCollector) || [])), [{
133
+ test: Cypress.currentTest.title,
134
+ errors: consoleIssues
135
+ }]));
136
+ }
137
+ });
138
+ });
139
+ /**
140
+ * Custom 'after' hook to analyze collected errors and warnings after all tests are executed.
141
+ */
142
+ after(function () {
143
+ // Skip 'after' hook if the logger is not enabled
144
+ // Double-check in case if functionality was disabled within the test-case code
145
+ // to skip js validations for some particular test and enable it afterward
146
+ if (isDisabled()) {
147
+ return;
148
+ }
149
+ // Analyze collected errors and warnings
150
+ var failures = Cypress.env(envVarCollector) || [];
151
+ cy.log('[JS ERRORS LOGGER] Analyze collected issues').then(function () {
152
+ if (failures.length > 0) {
153
+ // Format the error message for each test
154
+ var errorMessage = failures.map(function (failure) {
155
+ return "TEST: " + failure.test + "\nISSUES:\n" + failure.errors.map(function (e) { return "- " + e; }).join('\n');
156
+ }).join('\n\n');
157
+ // Throw an error with the collected issues
158
+ throw new Error('CONSOLE ERRORS and WARNINGS FOUND:\n\n' + errorMessage);
159
+ }
160
+ else {
161
+ cy.log('[JS ERRORS LOGGER] No console errors or warnings found.');
162
+ }
163
+ });
164
+ });
165
+ }
166
+ /**
167
+ * Custom hook implementation which fails the test after each test execution if any JavaScript errors or warnings are found.
168
+ *
169
+ * Proc: Allows each test to run, collects console errors and warnings, and fails the particular test by the end of it's execution
170
+ * if any issues are found.
171
+ * Cons: If errors or warnings were found during beforeEach or afterEach hook(s), the rest of spec will be ignored.
172
+ */
173
+ function attachFailAfterEach() {
174
+ // Double-check in case if functionality was disabled within the test-case code
175
+ // to skip js validations for some particular test and enable it afterward
176
+ if (isDisabled()) {
177
+ return;
178
+ }
179
+ /**
180
+ * Custom 'window:before:load' hook to spy on console errors and warnings
181
+ * This hook is executed before the page is loaded, allowing us to capture console messages.
182
+ */
183
+ Cypress.on('window:before:load', function (window) {
184
+ cy.spy(window.console, 'error').as('errors');
185
+ cy.spy(window.console, 'warn').as('warnings');
186
+ });
187
+ /**
188
+ * Custom 'afterEach' hook to collect console errors and warnings
189
+ */
190
+ afterEach(function () {
191
+ var consoleIssues = [];
192
+ var allowedWarnings = Cypress.env(envVarAllowedWarnings) || [];
193
+ // All errors should be collected
194
+ cy.get('@errors')
195
+ .invoke('getCalls')
196
+ .then(function (calls) { consoleIssues = calls; });
197
+ // Only warnings not in the allowed list should be collected
198
+ cy.get('@warnings')
199
+ .invoke('getCalls')
200
+ .each(function (call) {
201
+ call.args.forEach(function (arg) {
202
+ if (!allowedWarnings.some(function (item) { return arg.includes(item); })) {
203
+ consoleIssues.push(arg);
204
+ }
205
+ });
206
+ });
207
+ // Analyze collected errors and warnings
208
+ cy.log('[JS ERRORS LOGGER] Analyze collected issues').then(function () {
209
+ if (consoleIssues.length > 0) {
210
+ // Format the error message for each test
211
+ var errorMessage = consoleIssues.map(function (e) { return "- " + e; }).join('\n');
212
+ // Throw an error with the collected issues
213
+ throw new Error('CONSOLE ERRORS and WARNINGS FOUND:\n\n' + errorMessage);
214
+ }
215
+ else {
216
+ cy.log('[JS ERRORS LOGGER] No console errors or warnings found.');
217
+ }
218
+ });
219
+ });
220
+ }
221
+ /**
222
+ * Custom hook implementation which fails the test immediately when a JavaScript error or warning is detected.
223
+ *
224
+ * Proc: Allows each test to run, looks for console errors and warnings, and fails the particular test IMMEDIATELLY
225
+ * if any issues are found.
226
+ * Cons: If errors or warnings were found during beforeEach or afterEach hook(s), the rest of spec will be ignored.
227
+ */
228
+ function attachFailFast() {
229
+ // Double-check in case if functionality was disabled within the test-case code
230
+ // to skip js validations for some particular test and enable it afterward
231
+ if (isDisabled()) {
232
+ return;
233
+ }
234
+ /**
235
+ * Custom 'window:before:load' hook to spy on console errors and warnings
236
+ * This hook is executed before the page is loaded, allowing us to capture console messages.
237
+ */
238
+ Cypress.on('window:before:load', function (window) {
239
+ cy.spy(window.console, 'error').as('errors');
240
+ cy.spy(window.console, 'warn').as('warnings');
241
+ });
242
+ Cypress.on('window:load', function () {
243
+ // DOM should be loaded and parsed by now, so we can check for console errors and warnings
244
+ var consoleIssues = [];
245
+ var allowedWarnings = Cypress.env(envVarAllowedWarnings) || [];
246
+ // All errors should be collected
247
+ cy.get('@errors')
248
+ .invoke('getCalls')
249
+ .then(function (calls) { consoleIssues = calls; });
250
+ // Only warnings not in the allowed list should be collected
251
+ cy.get('@warnings')
252
+ .invoke('getCalls')
253
+ .each(function (call) {
254
+ call.args.forEach(function (arg) {
255
+ if (!allowedWarnings.some(function (item) { return arg.includes(item); })) {
256
+ consoleIssues.push(arg);
257
+ }
258
+ });
259
+ }).then(function () {
260
+ if (consoleIssues.length > 0) {
261
+ // Format the error message for each test
262
+ var errorMessage = consoleIssues.map(function (e) { return "- " + e; }).join('\n');
263
+ // Throw an error with the collected issues
264
+ throw new Error('CONSOLE ERRORS and WARNINGS FOUND:\n' + errorMessage);
265
+ }
266
+ else {
267
+ cy.log('[JS ERRORS LOGGER] No console errors or warnings found.');
268
+ }
269
+ });
270
+ });
271
+ }
272
+ /**
273
+ * Exports the jsLogger module with methods to attach hooks, enable/disable logging, and set allowed warnings.
274
+ */
275
+ exports.jsErrorsLogger = {
276
+ attachHooks: attachHooks,
277
+ setAllowedJsWarnings: setAllowedJsWarnings,
278
+ setStrategy: setStrategy,
279
+ STRATEGY: STRATEGY
280
+ };
@@ -18,7 +18,8 @@ var executeGroovy = function (scriptFile, replacements, jahiaServer) {
18
18
  files: [{
19
19
  fileName: scriptFile,
20
20
  replacements: replacements,
21
- type: 'text/plain'
21
+ type: 'text/plain',
22
+ encoding: 'utf-8'
22
23
  }],
23
24
  jahiaServer: jahiaServer
24
25
  }).then(function (r) { return r[0]; });
@@ -20,6 +20,14 @@ function processContent(formFile) {
20
20
  });
21
21
  }
22
22
  formFile.fileContent = content;
23
+ // If the file has an encoding, create a Blob with the new Blob constructor. It handle correctly the encoding
24
+ // for the groovy scripts
25
+ // Did not change for all files because jar files can be added as well, which are binary files
26
+ // In that case the function binaryStringToBlob will be used
27
+ if (formFile.encoding) {
28
+ return new Blob([content], { type: formFile.type });
29
+ }
30
+ // Default to binary string to blob conversion
23
31
  return Cypress.Blob.binaryStringToBlob(content, formFile.type);
24
32
  }
25
33
  function append(formFile, formData, key) {
@@ -8,6 +8,8 @@ var logout_1 = require("./logout");
8
8
  var fixture_1 = require("./fixture");
9
9
  var repeatUntil_1 = require("./repeatUntil");
10
10
  var testStep_1 = require("./testStep");
11
+ // FUNCTIONALITY IS TEMPORARY DISABLED DUE TO UNEXPECTED ISSUES
12
+ // import {jsErrorsLogger} from './jsErrorsLogger';
11
13
  var registerSupport = function () {
12
14
  Cypress.Commands.add('apolloClient', apollo_1.apolloClient);
13
15
  Cypress.Commands.add('apollo', { prevSubject: 'optional' }, apollo_1.apollo);
@@ -22,5 +24,11 @@ var registerSupport = function () {
22
24
  Cypress.Commands.add('repeatUntil', repeatUntil_1.repeatUntil);
23
25
  Cypress.Commands.overwrite('fixture', fixture_1.fixture);
24
26
  Cypress.Commands.add('step', testStep_1.step);
27
+ // Since registerSupport() function is called in each repo from e2e.js,
28
+ // attaching the JavaScript errors logger hooks here ensures that logger is initialized automatically
29
+ // for all tests without needing to call it explicitly in each test file.
30
+ // This is useful for capturing and logging JavaScript errors across all tests.
31
+ // FUNCTIONALITY IS TEMPORARY DISABLED DUE TO UNEXPECTED ISSUES
32
+ // jsErrorsLogger.attachHooks();
25
33
  };
26
34
  exports.registerSupport = registerSupport;
@@ -1,46 +1,54 @@
1
1
  /**
2
- * Helper class to decorate Cypress log messages with different log levels (INFO and DEBUG at the moment).
3
- * Class contains only static methods and should not be instantiated.
2
+ * Helper module to decorate Cypress log messages with different log levels (INFO and DEBUG at the moment).
4
3
  * @example
4
+ * // Switch default logging verbosity to DEBUG
5
+ * Log.setVerbosity(Log.LEVELS.DEBUG);
6
+ *
5
7
  * Log.info('This is an info message');
6
8
  * Log.debug('This is a debug message');
7
- * Log.json('DEBUG', myJSON);
9
+ * Log.json(Log.LEVELS.DEBUG, myJSON);
8
10
  * Log.info('My info message').then(() => { ... });
9
- * @note The log level can be set by changing the `Log.LEVEL` variable in the code (default is `INFO`).
11
+ *
12
+ * @note The log verbosity can be set by calling `Log.setVerbosity(Log.LEVELS.DEBUG)` in the code (default is `INFO`).
10
13
  * It tells the logger to log only messages with the given level and above.
11
14
  */
12
- export declare class Log {
13
- static LEVEL: string;
14
- private static levels;
15
- /**
16
- * Logs INFO message
17
- * @param {string} message - log message
18
- * @returns {Cypress.Chainable} - Cypress chainable object
19
- */
20
- static info(message: string): Cypress.Chainable;
21
- /**
22
- * Logs DEBUG message
23
- * @param {string} message - log message
24
- * @returns {Cypress.Chainable} - Cypress chainable object
25
- */
26
- static debug(message: string): Cypress.Chainable;
27
- /**
28
- * Logs JSON object with logging level given
29
- * @param {string} level - log level (e.g. 'INFO', 'DEBUG')
30
- * @param {string} json - json object to be logged
31
- * @returns {Cypress.Chainable} - Cypress chainable object
32
- */
33
- static json(level: string, json: string): Cypress.Chainable;
34
- /**
35
- * Private method to send the log message to Cypress log
36
- * @param {string} level - log level (e.g. 'INFO', 'DEBUG')
37
- * @param {string} message - log message
38
- * @note The method checks if the log level is enabled before sending the message to Cypress log
39
- * and uses the Cypress.log method to display the message in the Cypress log
40
- * @note The method is private and should not be called directly
41
- * Use the public methods (info, debug, error, warning) to send log messages
42
- * @returns {Cypress.Chainable} - Cypress chainable object
43
- * @private
44
- */
45
- private static _send_;
15
+ /**
16
+ * Logging levels enumerator.
17
+ */
18
+ declare enum LEVEL {
19
+ DEBUG = 0,
20
+ INFO = 1
46
21
  }
22
+ /**
23
+ * Sets the logging verbosity level for the logger. Messages with a level lower than the set level will not be logged.
24
+ * @param {LEVEL} level - log level to be set (e.g. 'DEBUG', 'INFO')
25
+ * @return {void}
26
+ */
27
+ declare function setVerbosity(level: LEVEL): void;
28
+ /**
29
+ * Logs INFO message
30
+ * @param {string} message - log message
31
+ * @returns {Cypress.Chainable} - Cypress chainable object
32
+ */
33
+ declare function info(message: string): Cypress.Chainable;
34
+ /**
35
+ * Logs DEBUG message
36
+ * @param {string} message - log message
37
+ * @returns {Cypress.Chainable} - Cypress chainable object
38
+ */
39
+ declare function debug(message: string): Cypress.Chainable;
40
+ /**
41
+ * Logs JSON object with logging level given
42
+ * @param {LEVEL} level - log level (e.g. 'INFO', 'DEBUG')
43
+ * @param {string} text - json object to be logged
44
+ * @returns {Cypress.Chainable} - Cypress chainable object
45
+ */
46
+ declare function json(level: LEVEL, text: string): Cypress.Chainable;
47
+ export declare const Log: {
48
+ info: typeof info;
49
+ debug: typeof debug;
50
+ json: typeof json;
51
+ setVerbosity: typeof setVerbosity;
52
+ LEVEL: typeof LEVEL;
53
+ };
54
+ export {};
@@ -1,79 +1,105 @@
1
1
  "use strict";
2
- exports.__esModule = true;
3
- exports.Log = void 0;
4
2
  /**
5
- * Helper class to decorate Cypress log messages with different log levels (INFO and DEBUG at the moment).
6
- * Class contains only static methods and should not be instantiated.
3
+ * Helper module to decorate Cypress log messages with different log levels (INFO and DEBUG at the moment).
7
4
  * @example
5
+ * // Switch default logging verbosity to DEBUG
6
+ * Log.setVerbosity(Log.LEVELS.DEBUG);
7
+ *
8
8
  * Log.info('This is an info message');
9
9
  * Log.debug('This is a debug message');
10
- * Log.json('DEBUG', myJSON);
10
+ * Log.json(Log.LEVELS.DEBUG, myJSON);
11
11
  * Log.info('My info message').then(() => { ... });
12
- * @note The log level can be set by changing the `Log.LEVEL` variable in the code (default is `INFO`).
12
+ *
13
+ * @note The log verbosity can be set by calling `Log.setVerbosity(Log.LEVELS.DEBUG)` in the code (default is `INFO`).
13
14
  * It tells the logger to log only messages with the given level and above.
14
15
  */
15
- var Log = /** @class */ (function () {
16
- function Log() {
16
+ exports.__esModule = true;
17
+ exports.Log = void 0;
18
+ // ENV variable to store the logging verbosity level
19
+ var envVarLoggingVerbosity = '__LOG_VERBOSITY__';
20
+ /**
21
+ * Logging levels enumerator.
22
+ */
23
+ var LEVEL;
24
+ (function (LEVEL) {
25
+ LEVEL[LEVEL["DEBUG"] = 0] = "DEBUG";
26
+ LEVEL[LEVEL["INFO"] = 1] = "INFO";
27
+ })(LEVEL || (LEVEL = {}));
28
+ /**
29
+ * Return the current logging verbosity level.
30
+ * @returns {LEVEL} - current logging level set in Cypress environment variable `__LOG_VERBOSITY__`
31
+ * @note be careful with Cypress.env(envVarLoggingVerbosity), since it might return `0` for `DEBUG` level,
32
+ * which is falsy in JavaScript, so we need to check if the variable is undefined.
33
+ */
34
+ function getVerbosity() {
35
+ return typeof Cypress.env(envVarLoggingVerbosity) === 'undefined' ? LEVEL.INFO : Cypress.env(envVarLoggingVerbosity);
36
+ }
37
+ /**
38
+ * Sets the logging verbosity level for the logger. Messages with a level lower than the set level will not be logged.
39
+ * @param {LEVEL} level - log level to be set (e.g. 'DEBUG', 'INFO')
40
+ * @return {void}
41
+ */
42
+ function setVerbosity(level) {
43
+ Cypress.env(envVarLoggingVerbosity, level);
44
+ }
45
+ /**
46
+ * Logs INFO message
47
+ * @param {string} message - log message
48
+ * @returns {Cypress.Chainable} - Cypress chainable object
49
+ */
50
+ function info(message) {
51
+ return _send_(exports.Log.LEVEL.INFO, message);
52
+ }
53
+ /**
54
+ * Logs DEBUG message
55
+ * @param {string} message - log message
56
+ * @returns {Cypress.Chainable} - Cypress chainable object
57
+ */
58
+ function debug(message) {
59
+ return _send_(exports.Log.LEVEL.DEBUG, message);
60
+ }
61
+ /**
62
+ * Logs JSON object with logging level given
63
+ * @param {LEVEL} level - log level (e.g. 'INFO', 'DEBUG')
64
+ * @param {string} text - json object to be logged
65
+ * @returns {Cypress.Chainable} - Cypress chainable object
66
+ */
67
+ function json(level, text) {
68
+ return _send_(level, JSON.stringify(text, null, 2));
69
+ }
70
+ /**
71
+ * Private method to send the log message to Cypress log
72
+ * @param {LEVEL} level - log level (e.g. 'INFO', 'DEBUG')
73
+ * @param {string} message - log message
74
+ * @note The method checks if the log level is enabled before sending the message to Cypress log
75
+ * and uses the Cypress.log method to display the message in the Cypress log
76
+ * @note The method is private and should not be called directly
77
+ * Use the public methods (info, debug, error, warning) to send log messages
78
+ * @returns {Cypress.Chainable} - Cypress chainable object
79
+ * @private
80
+ */
81
+ function _send_(level, message) {
82
+ // Check if the log level is valid
83
+ if (!Object.values(exports.Log.LEVEL).includes(level)) {
84
+ throw new Error("Log level \"" + level + "\" is not supported. Supported levels are: " + exports.Log.LEVEL);
85
+ }
86
+ // Check if the log level is enabled,
87
+ // take into account the log level set in Cypress.env('LOG_LEVEL') and the log level set in the Log.LEVEL variable.
88
+ // If the log level is enabled, send the message to Cypress log.
89
+ if (level >= getVerbosity()) {
90
+ // Send the message to Cypress log
91
+ // use cy.then() to ensure that the log message is sent in the correct order
92
+ // and use cy.wrap() to return the Cypress chainable object
93
+ return cy.then(function () {
94
+ Cypress.log({ displayName: "[ " + exports.Log.LEVEL[level].toUpperCase() + " ]", message: "" + message });
95
+ }).then(cy.wrap);
17
96
  }
18
- /**
19
- * Logs INFO message
20
- * @param {string} message - log message
21
- * @returns {Cypress.Chainable} - Cypress chainable object
22
- */
23
- Log.info = function (message) {
24
- return Log._send_('INFO', message);
25
- };
26
- /**
27
- * Logs DEBUG message
28
- * @param {string} message - log message
29
- * @returns {Cypress.Chainable} - Cypress chainable object
30
- */
31
- Log.debug = function (message) {
32
- return Log._send_('DEBUG', message);
33
- };
34
- /**
35
- * Logs JSON object with logging level given
36
- * @param {string} level - log level (e.g. 'INFO', 'DEBUG')
37
- * @param {string} json - json object to be logged
38
- * @returns {Cypress.Chainable} - Cypress chainable object
39
- */
40
- Log.json = function (level, json) {
41
- return Log._send_(level, JSON.stringify(json, null, 2));
42
- };
43
- /**
44
- * Private method to send the log message to Cypress log
45
- * @param {string} level - log level (e.g. 'INFO', 'DEBUG')
46
- * @param {string} message - log message
47
- * @note The method checks if the log level is enabled before sending the message to Cypress log
48
- * and uses the Cypress.log method to display the message in the Cypress log
49
- * @note The method is private and should not be called directly
50
- * Use the public methods (info, debug, error, warning) to send log messages
51
- * @returns {Cypress.Chainable} - Cypress chainable object
52
- * @private
53
- */
54
- Log._send_ = function (level, message) {
55
- var _a, _b;
56
- // Check if the log level is valid
57
- if (!Log.levels.includes(level.toUpperCase())) {
58
- throw new Error("Log level \"" + level + "\" is not supported. Supported levels are: " + Log.levels.join(', '));
59
- }
60
- // Check if the log level is enabled,
61
- // take into account the log level set in Cypress.env('LOG_LEVEL') and the log level set in the Log.LEVEL variable.
62
- // If the log level is enabled, send the message to Cypress log.
63
- if ((Log.levels.includes((_a = Cypress.env('LOG_LEVEL')) === null || _a === void 0 ? void 0 : _a.toUpperCase()) && Log.levels.indexOf(level.toUpperCase()) >= Log.levels.indexOf((_b = Cypress.env('LOG_LEVEL')) === null || _b === void 0 ? void 0 : _b.toUpperCase())) ||
64
- Log.levels.indexOf(level.toUpperCase()) >= Log.levels.indexOf(Log.LEVEL)) {
65
- // Send the message to Cypress log
66
- // use cy.then() to ensure that the log message is sent in the correct order
67
- // and use cy.wrap() to return the Cypress chainable object
68
- return cy.then(function () {
69
- Cypress.log({ displayName: "[ " + level.toUpperCase() + " ]", message: "" + message });
70
- }).then(cy.wrap);
71
- }
72
- };
73
- // The default log level is set to 'INFO' and can be changed by setting the Log.LEVEL variable in the code
74
- Log.LEVEL = 'INFO';
75
- // The log levels are defined in the levels array and are ordered from the lowest to the highest priority
76
- Log.levels = ['DEBUG', 'INFO'];
77
- return Log;
78
- }());
79
- exports.Log = Log;
97
+ }
98
+ // Export the Log module with methods to log messages and set the logging level
99
+ exports.Log = {
100
+ info: info,
101
+ debug: debug,
102
+ json: json,
103
+ setVerbosity: setVerbosity,
104
+ LEVEL: LEVEL
105
+ };