@jahia/cypress 6.3.0 → 6.4.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/dist/support/jsErrorsLogger.d.ts +38 -2
- package/dist/support/jsErrorsLogger.js +137 -181
- package/dist/support/provisioning/index.d.ts +1 -0
- package/dist/support/provisioning/index.js +1 -0
- package/dist/support/registerSupport.js +0 -8
- package/dist/utils/Logger.d.ts +10 -2
- package/dist/utils/Logger.js +64 -4
- package/docs/js-errors-logger.md +34 -22
- package/package.json +1 -1
- package/src/support/jsErrorsLogger.ts +152 -186
- package/src/support/provisioning/index.ts +1 -0
- package/src/support/registerSupport.ts +0 -9
- package/src/utils/Logger.ts +96 -5
package/docs/js-errors-logger.md
CHANGED
|
@@ -15,23 +15,23 @@ The JavaScript Errors Logger is a comprehensive monitoring and reporting module
|
|
|
15
15
|
|
|
16
16
|
The logger supports three distinct strategies for handling JavaScript errors and warnings:
|
|
17
17
|
|
|
18
|
-
### 1. Fail Fast (
|
|
18
|
+
### 1. Fail Fast (default)
|
|
19
19
|
- **Strategy**: `STRATEGY.failFast`
|
|
20
|
-
- **Behavior**: Fails immediately when an error or warning is detected
|
|
20
|
+
- **Behavior**: Fails immediately when an error or warning is detected; the rest of tests will be executed
|
|
21
21
|
- **Use Case**: Best for development environments where immediate feedback is crucial
|
|
22
22
|
- **Pros**: Quick identification of issues
|
|
23
23
|
- **Cons**: May stop test execution on first error, preventing discovery of additional issues
|
|
24
24
|
|
|
25
25
|
### 2. Fail After Each Test
|
|
26
26
|
- **Strategy**: `STRATEGY.failAfterEach`
|
|
27
|
-
- **Behavior**: Collects errors/warnings during test execution and fails at the end of
|
|
28
|
-
- **Use Case**: Suitable when you want
|
|
29
|
-
- **Pros**: Allows
|
|
30
|
-
- **Cons**:
|
|
27
|
+
- **Behavior**: Collects errors/warnings during test execution and fails at the end of the one; the rest of tests will be skipped
|
|
28
|
+
- **Use Case**: Suitable when you want the test to complete but still get immediate feedback
|
|
29
|
+
- **Pros**: Allows test to be executed till the end before providing a report
|
|
30
|
+
- **Cons**: Additional log review might be needed to identify - where the error or warning occur
|
|
31
31
|
|
|
32
32
|
### 3. Fail After All Tests
|
|
33
33
|
- **Strategy**: `STRATEGY.failAfterAll`
|
|
34
|
-
- **Behavior**: Collects all errors/warnings and reports them after the entire test suite completes
|
|
34
|
+
- **Behavior**: Collects all errors/warnings and reports them after the entire test suite completes; the last test will be marked as failed
|
|
35
35
|
- **Use Case**: Ideal for CI/CD environments where you want a complete test run overview
|
|
36
36
|
- **Pros**: Complete test suite execution with comprehensive error reporting
|
|
37
37
|
- **Cons**: Error reporting may be confusing as the last test will be marked as failed, since the errors analysis and reporting is done in after() hook
|
|
@@ -40,9 +40,9 @@ The logger supports three distinct strategies for handling JavaScript errors and
|
|
|
40
40
|
|
|
41
41
|
### Environment Variables
|
|
42
42
|
|
|
43
|
-
| Variable
|
|
44
|
-
|
|
45
|
-
| `
|
|
43
|
+
| Variable | Type | Description |
|
|
44
|
+
|---------------------------------|------|-------------|
|
|
45
|
+
| `JAHIA_HOOKS_DISABLE_JS_LOGGER` | boolean | Disables the logger when set to `true` |
|
|
46
46
|
|
|
47
47
|
### Programmatic Configuration
|
|
48
48
|
|
|
@@ -51,8 +51,11 @@ It is **strongly** recommended to add custom configuration in project's common f
|
|
|
51
51
|
```typescript
|
|
52
52
|
import { jsErrorsLogger } from '@jahia/cypress';
|
|
53
53
|
|
|
54
|
+
// Attach Logger
|
|
55
|
+
jsErrorsLogger.enable();
|
|
56
|
+
|
|
54
57
|
// Set preferrable error handling strategy
|
|
55
|
-
jsErrorsLogger.setStrategy(jsErrorsLogger.STRATEGY.
|
|
58
|
+
jsErrorsLogger.setStrategy(jsErrorsLogger.STRATEGY.failFast);
|
|
56
59
|
|
|
57
60
|
// Define allowed warnings that won't trigger failures
|
|
58
61
|
jsErrorsLogger.setAllowedJsWarnings([
|
|
@@ -65,7 +68,16 @@ jsErrorsLogger.setAllowedJsWarnings([
|
|
|
65
68
|
|
|
66
69
|
### Basic Setup
|
|
67
70
|
|
|
68
|
-
|
|
71
|
+
#### Enable the Logger for the repo
|
|
72
|
+
This call should only be used in `tests/cypress/support/e2e.js`. Add the following code in the repo where functionality should be used:
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import { jsErrorsLogger } from '@jahia/cypress';
|
|
76
|
+
|
|
77
|
+
before(() => {
|
|
78
|
+
jsErrorsLogger.enable();
|
|
79
|
+
});
|
|
80
|
+
```
|
|
69
81
|
|
|
70
82
|
### Disabling the Logger
|
|
71
83
|
|
|
@@ -73,19 +85,17 @@ The logger is automatically initialized when jahia-cypress is imported and regis
|
|
|
73
85
|
|
|
74
86
|
```bash
|
|
75
87
|
# Disable in CI/CD or specific environments
|
|
76
|
-
export
|
|
88
|
+
export JAHIA_HOOKS_DISABLE_JS_LOGGER="true"
|
|
77
89
|
```
|
|
78
90
|
|
|
79
|
-
####
|
|
91
|
+
#### Disable for the specific Spec
|
|
80
92
|
|
|
81
93
|
```typescript
|
|
94
|
+
import { jsErrorsLogger } from '@jahia/cypress';
|
|
95
|
+
|
|
82
96
|
describe('Tests with disabled JS logger', () => {
|
|
83
97
|
before(() => {
|
|
84
|
-
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
after(() => {
|
|
88
|
-
Cypress.env('JS_LOGGER_DISABLED', false);
|
|
98
|
+
jsErrorsLogger.disable();
|
|
89
99
|
});
|
|
90
100
|
|
|
91
101
|
it('should run without JS error monitoring', () => {
|
|
@@ -140,10 +150,12 @@ ISSUES:
|
|
|
140
150
|
|
|
141
151
|
### Methods
|
|
142
152
|
|
|
143
|
-
| Method | Parameters | Return Type | Description
|
|
144
|
-
|
|
153
|
+
| Method | Parameters | Return Type | Description |
|
|
154
|
+
|--------|------------|-------------|--------------------------------------------------------|
|
|
145
155
|
| `setStrategy(strategy)` | STRATEGY enum | void | Sets the error handling strategy |
|
|
146
|
-
| `setAllowedJsWarnings(warnings)` | string[] | void | Configures allowed warning messages
|
|
156
|
+
| `setAllowedJsWarnings(warnings)` | string[] | void | Configures allowed warning messages |
|
|
157
|
+
| `disable()` | - | void | Disables the logger for the current spec |
|
|
158
|
+
| `enable()` | - | void | Enables the logger for the current repo |
|
|
147
159
|
|
|
148
160
|
### Enums
|
|
149
161
|
|
package/package.json
CHANGED
|
@@ -5,19 +5,45 @@
|
|
|
5
5
|
* Provides methods to enable, disable, and check logger status.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
const
|
|
8
|
+
const envVarDisableAll = 'JAHIA_HOOKS_DISABLE';
|
|
9
|
+
const envVarDisableJsLogger = 'JAHIA_HOOKS_DISABLE_JS_LOGGER';
|
|
9
10
|
const envVarCollector = '__JS_LOGGER_FAILURES__';
|
|
10
11
|
const envVarAllowedWarnings = '__JS_LOGGER_ALLOWED_WARNINGS__';
|
|
11
12
|
const envVarStrategy = '__JS_LOGGER_STRATEGY__';
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* Strategy for handling JavaScript errors and warnings in Cypress tests.
|
|
16
|
+
*
|
|
15
17
|
* - failFast: Fail *immediately* when an error is detected.
|
|
18
|
+
*
|
|
19
|
+
* Proc: Allows each test to run, looks for console errors and warnings, and fails the particular test IMMEDIATELY
|
|
20
|
+
* if any issues are found.
|
|
21
|
+
* Cons: If errors or warnings were found during beforeEach or afterEach hook(s), the rest of spec will be ignored.
|
|
22
|
+
*
|
|
16
23
|
* - failAfterEach: Collect all errors and warnings *during test* execution and fail if any issues are found.
|
|
24
|
+
*
|
|
25
|
+
* Proc: Allows each test to run, collects console errors and warnings, and fails the particular test by the end of it's execution
|
|
26
|
+
* if any issues are found.
|
|
27
|
+
* Cons: If errors or warnings were found during beforeEach or afterEach hook(s), the rest of spec will be ignored.
|
|
28
|
+
*
|
|
17
29
|
* - failAfterAll: Collect all errors and warnings *after all tests* and fail at the end of the test suite.
|
|
30
|
+
*
|
|
31
|
+
* Proc: Allows all tests to run, collects console errors and warnings, and fails the test suite at the end if any issues are found.
|
|
32
|
+
* This is useful for reporting all issues at once after all tests are executed, rather than failing immediately on the first issue.
|
|
33
|
+
* Cons: Reporting might be confusing, e.g. - cypress will report the very last test as failed, while many tests might have issues.
|
|
34
|
+
* This is because the hook is executed after all tests are completed, so the last test is reported as failed.
|
|
18
35
|
*/
|
|
19
36
|
enum STRATEGY { failFast, failAfterAll, failAfterEach }
|
|
20
37
|
|
|
38
|
+
/**
|
|
39
|
+
* Auxiliary type to represent a single item in the collector.
|
|
40
|
+
* It contains the test title and an array of error or warning messages collected during the test.
|
|
41
|
+
*/
|
|
42
|
+
type CollectorItem = {
|
|
43
|
+
test: string; // The title of the test where the issue was found
|
|
44
|
+
errors: string[]; // Array of error or warning messages collected during the test
|
|
45
|
+
};
|
|
46
|
+
|
|
21
47
|
/**
|
|
22
48
|
* Returns the current strategy for handling JavaScript errors and warnings in Cypress tests.
|
|
23
49
|
* @returns {STRATEGY} - The current strategy for handling JavaScript errors and warnings.
|
|
@@ -36,11 +62,29 @@ function getStrategy(): STRATEGY {
|
|
|
36
62
|
*/
|
|
37
63
|
function setStrategy(strategy: STRATEGY): void { Cypress.env(envVarStrategy, strategy); }
|
|
38
64
|
|
|
65
|
+
/**
|
|
66
|
+
* Returns console issues collected during the test execution.
|
|
67
|
+
* @returns {CollectorItem []} - Array of collected issues, each issue is an object with test title and errors.
|
|
68
|
+
*/
|
|
69
|
+
function getCollectedIssues(): CollectorItem [] { return Cypress.env(envVarCollector) || []; }
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Sets the console issues collected during the test execution.
|
|
73
|
+
* @returns {void}
|
|
74
|
+
*/
|
|
75
|
+
function setCollectedIssues(items: CollectorItem []): void { Cypress.env(envVarCollector, items); }
|
|
76
|
+
|
|
39
77
|
/**
|
|
40
78
|
* Checks if the js errors and warnings logger is disabled.
|
|
41
79
|
* @returns {boolean} - true if the logger is disabled, false otherwise.
|
|
42
80
|
*/
|
|
43
|
-
function isDisabled(): boolean { return Cypress.env(
|
|
81
|
+
function isDisabled(): boolean { return ((Cypress.env(envVarDisableAll) === true) || (Cypress.env(envVarDisableJsLogger) === true)); }
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Returns the list of allowed warnings that will not be reported by the logger.
|
|
85
|
+
* @returns {string[]} - Array of allowed warning messages.
|
|
86
|
+
*/
|
|
87
|
+
function getAllowedJsWarnings(): string[] { return Cypress.env(envVarAllowedWarnings) || []; }
|
|
44
88
|
|
|
45
89
|
/**
|
|
46
90
|
* Sets the list of allowed warnings that will not be reported by the logger.
|
|
@@ -50,223 +94,142 @@ function isDisabled(): boolean { return Cypress.env(envVarDisabled) === true; }
|
|
|
50
94
|
function setAllowedJsWarnings(warnings: string[]): void { Cypress.env(envVarAllowedWarnings, warnings); }
|
|
51
95
|
|
|
52
96
|
/**
|
|
53
|
-
* Attaches custom
|
|
54
|
-
* This
|
|
55
|
-
* It sets up listeners for console errors and warnings, collects them after each test,
|
|
56
|
-
* and throws an error if any issues are found after all tests are executed.
|
|
97
|
+
* Attaches a custom JavaScript interceptor to capture console errors and warnings.
|
|
98
|
+
* This interceptor is executed before the page is loaded, allowing us to spy on console messages.
|
|
57
99
|
*/
|
|
58
|
-
function
|
|
59
|
-
// Skip hook attachment if the logger is disabled (e.g. from CI/CD pipeline)
|
|
60
|
-
if (isDisabled()) { return; }
|
|
61
|
-
|
|
62
|
-
switch (getStrategy()) {
|
|
63
|
-
case STRATEGY.failAfterAll:
|
|
64
|
-
attachFailAfterAll();
|
|
65
|
-
break;
|
|
66
|
-
case STRATEGY.failAfterEach:
|
|
67
|
-
attachFailAfterEach();
|
|
68
|
-
break;
|
|
69
|
-
default:
|
|
70
|
-
attachFailFast();
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Custom hook implementation which fails the test after all tests are executed if any JavaScript errors or warnings are found.
|
|
76
|
-
*
|
|
77
|
-
* Proc: Allows all tests to run, collects console errors and warnings, and fails the test suite at the end if any issues are found.
|
|
78
|
-
* This is useful for reporting all issues at once after all tests are executed, rather than failing immediately on the first issue.
|
|
79
|
-
* Cons: Reporting might be confusing, e.g. - cypress will report the very last test as failed, while many tests might have issues.
|
|
80
|
-
* This is because the hook is executed after all tests are completed, so the last test is reported as failed.
|
|
81
|
-
*/
|
|
82
|
-
function attachFailAfterAll(): void {
|
|
83
|
-
/**
|
|
84
|
-
* Custom 'window:before:load' hook to spy on console errors and warnings
|
|
85
|
-
* This hook is executed before the page is loaded, allowing us to capture console messages.
|
|
86
|
-
*/
|
|
100
|
+
function attachJsInterceptor(): void {
|
|
87
101
|
Cypress.on('window:before:load', window => {
|
|
88
102
|
// Skip 'window:before:load' hook if the logger is not enabled
|
|
89
|
-
// Double-check in case if functionality was disabled within the test-case code
|
|
90
|
-
// to skip js validations for some particular test and enable it afterward
|
|
91
103
|
if (isDisabled()) { return; }
|
|
92
104
|
|
|
105
|
+
// Spy on console.error and console.warn to capture errors and warnings
|
|
93
106
|
cy.spy(window.console, 'error').as('errors');
|
|
94
107
|
cy.spy(window.console, 'warn').as('warnings');
|
|
95
108
|
});
|
|
109
|
+
}
|
|
96
110
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
consoleIssues.push(arg);
|
|
121
|
-
}
|
|
111
|
+
/**
|
|
112
|
+
* Collects JavaScript errors and warnings using the spies set up in attachJsInterceptor.
|
|
113
|
+
* @returns {Cypress.Chainable} - Cypress chainable object that resolves when issues are collected.
|
|
114
|
+
*/
|
|
115
|
+
function collectIssues(): Cypress.Chainable {
|
|
116
|
+
const allowedWarnings = getAllowedJsWarnings();
|
|
117
|
+
let consoleIssues: string[] = [];
|
|
118
|
+
|
|
119
|
+
// Look for console errors and warnings, collected by the spies
|
|
120
|
+
return cy.get('@errors')
|
|
121
|
+
.invoke('getCalls')
|
|
122
|
+
.then(errorCalls => {
|
|
123
|
+
// All errors should be collected
|
|
124
|
+
consoleIssues = errorCalls.flatMap((call: { args: string[] }) => call.args);
|
|
125
|
+
|
|
126
|
+
// Analyze warnings
|
|
127
|
+
cy.get('@warnings')
|
|
128
|
+
.invoke('getCalls')
|
|
129
|
+
.then(warningCalls => {
|
|
130
|
+
warningCalls.flatMap((call: { args: string[] }) => call.args).forEach((arg: string) => {
|
|
131
|
+
// Only warnings not in the allowed list should be collected
|
|
132
|
+
if (!allowedWarnings.some((item: string) => arg.includes(item))) { consoleIssues.push(arg); }
|
|
133
|
+
});
|
|
122
134
|
});
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
cy.then(() => {
|
|
135
|
+
})
|
|
136
|
+
.then(() => {
|
|
137
|
+
// Update the Cypress environment variable with the collected issues
|
|
127
138
|
if (consoleIssues.length > 0) {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
errors: consoleIssues
|
|
131
|
-
|
|
139
|
+
setCollectedIssues([
|
|
140
|
+
...getCollectedIssues(),
|
|
141
|
+
{test: Cypress.currentTest.title, errors: consoleIssues}
|
|
142
|
+
]);
|
|
132
143
|
}
|
|
144
|
+
})
|
|
145
|
+
.then(() => {
|
|
146
|
+
// Return a Cypress chainable object to allow chaining
|
|
147
|
+
return cy.wrap(null, {log: false});
|
|
133
148
|
});
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Custom 'after' hook to analyze collected errors and warnings after all tests are executed.
|
|
138
|
-
*/
|
|
139
|
-
after(() => {
|
|
140
|
-
// Skip 'after' hook if the logger is not enabled
|
|
141
|
-
// Double-check in case if functionality was disabled within the test-case code
|
|
142
|
-
// to skip js validations for some particular test and enable it afterward
|
|
143
|
-
if (isDisabled()) { return; }
|
|
149
|
+
}
|
|
144
150
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
151
|
+
/**
|
|
152
|
+
* Analyzes collected JavaScript errors and warnings and throws an error if any were found.
|
|
153
|
+
*/
|
|
154
|
+
function analyzeIssues(): void {
|
|
155
|
+
cy.then(() => {
|
|
156
|
+
const failures = getCollectedIssues();
|
|
157
|
+
if (failures.length > 0) {
|
|
158
|
+
// Format the error message for each test
|
|
159
|
+
const errorMessage = failures.map((failure: { test: string; errors: string[]; }) => {
|
|
160
|
+
return `TEST: ${failure.test}\nISSUES:\n${failure.errors.map((e: string) => `- ${e}`).join('\n')}`;
|
|
161
|
+
}).join('\n\n');
|
|
162
|
+
// Reset the collector for the next test run
|
|
163
|
+
setCollectedIssues([]);
|
|
164
|
+
|
|
165
|
+
// Throw an error with the collected issues
|
|
166
|
+
throw new Error('CONSOLE ERRORS and WARNINGS FOUND:\n\n' + errorMessage);
|
|
167
|
+
}
|
|
159
168
|
});
|
|
160
169
|
}
|
|
161
170
|
|
|
162
171
|
/**
|
|
163
|
-
*
|
|
164
|
-
*
|
|
165
|
-
* Proc: Allows each test to run, collects console errors and warnings, and fails the particular test by the end of it's execution
|
|
166
|
-
* if any issues are found.
|
|
167
|
-
* Cons: If errors or warnings were found during beforeEach or afterEach hook(s), the rest of spec will be ignored.
|
|
172
|
+
* Disables the js errors and warnings logger.
|
|
173
|
+
* @returns {void}
|
|
168
174
|
*/
|
|
169
|
-
function
|
|
170
|
-
// Double-check in case if functionality was disabled within the test-case code
|
|
171
|
-
// to skip js validations for some particular test and enable it afterward
|
|
172
|
-
if (isDisabled()) { return; }
|
|
175
|
+
function disable(): void { Cypress.env(envVarDisableJsLogger, true); }
|
|
173
176
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
177
|
+
/**
|
|
178
|
+
* Attaches custom hooks to Cypress events to monitor and report JavaScript errors and warnings.
|
|
179
|
+
* This method is called automatically in registerSupport.ts#registerSupport
|
|
180
|
+
* It sets up listeners for console errors and warnings, collects them after each test,
|
|
181
|
+
* and throws an error if any issues are found after all tests are executed.
|
|
182
|
+
*/
|
|
183
|
+
function enable(): void {
|
|
184
|
+
// Ensure the logger is enabled by default
|
|
185
|
+
Cypress.env(envVarDisableJsLogger, false);
|
|
186
|
+
|
|
187
|
+
// Attach errors and warnings collector
|
|
188
|
+
attachJsInterceptor();
|
|
182
189
|
|
|
183
190
|
/**
|
|
184
|
-
* Custom 'afterEach' hook to collect
|
|
191
|
+
* Custom 'afterEach' hook to collect JavaScript errors and warnings after each test execution.
|
|
192
|
+
* The behavior of this hook depends on the strategy set for the logger.
|
|
185
193
|
*/
|
|
186
194
|
afterEach(() => {
|
|
187
|
-
|
|
188
|
-
|
|
195
|
+
// Skip the hook if the logger is disabled
|
|
196
|
+
if (isDisabled()) { return; }
|
|
189
197
|
|
|
190
|
-
//
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
198
|
+
// Depending on the strategy, collect issues and analyze them
|
|
199
|
+
// If the strategy is failFast, issues will be collected in 'window:load'
|
|
200
|
+
if (getStrategy() === STRATEGY.failAfterEach) {
|
|
201
|
+
// Collect issues after each test and analyze them immediately
|
|
202
|
+
collectIssues().then(() => analyzeIssues());
|
|
203
|
+
} else if (getStrategy() === STRATEGY.failAfterAll) {
|
|
204
|
+
// Collect issues after each test, but analyze them only after all tests are executed
|
|
205
|
+
collectIssues();
|
|
206
|
+
} else {
|
|
207
|
+
// Do nothing for failFast strategy, issues will be collected and analyzed in 'window:load' hook
|
|
208
|
+
}
|
|
209
|
+
});
|
|
194
210
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
}
|
|
203
|
-
});
|
|
204
|
-
});
|
|
211
|
+
/**
|
|
212
|
+
* Custom 'after' hook to analyze collected errors and warnings after all tests are executed.
|
|
213
|
+
*/
|
|
214
|
+
after(() => {
|
|
215
|
+
// Skip the hook if the logger is disabled or if the strategy is not failAfterAll
|
|
216
|
+
// This hook is only relevant for the failAfterAll strategy, where we analyze issues after
|
|
217
|
+
if (isDisabled() || getStrategy() !== STRATEGY.failAfterAll) { return; }
|
|
205
218
|
|
|
206
219
|
// Analyze collected errors and warnings
|
|
207
|
-
|
|
208
|
-
if (consoleIssues.length > 0) {
|
|
209
|
-
// Format the error message for each test
|
|
210
|
-
const errorMessage = consoleIssues.map((e: string) => `- ${e}`).join('\n');
|
|
211
|
-
// Throw an error with the collected issues
|
|
212
|
-
throw new Error('CONSOLE ERRORS and WARNINGS FOUND:\n\n' + errorMessage);
|
|
213
|
-
} else {
|
|
214
|
-
cy.log('[JS ERRORS LOGGER] No console errors or warnings found.');
|
|
215
|
-
}
|
|
216
|
-
});
|
|
220
|
+
analyzeIssues();
|
|
217
221
|
});
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Custom hook implementation which fails the test immediately when a JavaScript error or warning is detected.
|
|
222
|
-
*
|
|
223
|
-
* Proc: Allows each test to run, looks for console errors and warnings, and fails the particular test IMMEDIATELLY
|
|
224
|
-
* if any issues are found.
|
|
225
|
-
* Cons: If errors or warnings were found during beforeEach or afterEach hook(s), the rest of spec will be ignored.
|
|
226
|
-
*/
|
|
227
|
-
function attachFailFast(): void {
|
|
228
|
-
// Double-check in case if functionality was disabled within the test-case code
|
|
229
|
-
// to skip js validations for some particular test and enable it afterward
|
|
230
|
-
if (isDisabled()) { return; }
|
|
231
222
|
|
|
232
223
|
/**
|
|
233
|
-
* Custom 'window:
|
|
234
|
-
*
|
|
224
|
+
* Custom 'window:load' hook to collect JavaScript errors and warnings right after the page is loaded.
|
|
225
|
+
* Applicable only for the failFast strategy.
|
|
235
226
|
*/
|
|
236
|
-
Cypress.on('window:before:load', window => {
|
|
237
|
-
cy.spy(window.console, 'error').as('errors');
|
|
238
|
-
cy.spy(window.console, 'warn').as('warnings');
|
|
239
|
-
});
|
|
240
|
-
|
|
241
227
|
Cypress.on('window:load', () => {
|
|
242
|
-
//
|
|
243
|
-
|
|
244
|
-
const allowedWarnings = Cypress.env(envVarAllowedWarnings) || [];
|
|
228
|
+
// Skip the hook if the logger is disabled or if the strategy is not failFast
|
|
229
|
+
if (isDisabled() || getStrategy() !== STRATEGY.failFast) { return; }
|
|
245
230
|
|
|
246
|
-
//
|
|
247
|
-
|
|
248
|
-
.invoke('getCalls')
|
|
249
|
-
.then(calls => { consoleIssues = calls; });
|
|
250
|
-
|
|
251
|
-
// Only warnings not in the allowed list should be collected
|
|
252
|
-
cy.get('@warnings')
|
|
253
|
-
.invoke('getCalls')
|
|
254
|
-
.each((call: { args: string[]}) => {
|
|
255
|
-
call.args.forEach((arg: string) => {
|
|
256
|
-
if (!allowedWarnings.some((item: string) => arg.includes(item))) {
|
|
257
|
-
consoleIssues.push(arg);
|
|
258
|
-
}
|
|
259
|
-
});
|
|
260
|
-
}).then(() => {
|
|
261
|
-
if (consoleIssues.length > 0) {
|
|
262
|
-
// Format the error message for each test
|
|
263
|
-
const errorMessage = consoleIssues.map((e: string) => `- ${e}`).join('\n');
|
|
264
|
-
// Throw an error with the collected issues
|
|
265
|
-
throw new Error('CONSOLE ERRORS and WARNINGS FOUND:\n' + errorMessage);
|
|
266
|
-
} else {
|
|
267
|
-
cy.log('[JS ERRORS LOGGER] No console errors or warnings found.');
|
|
268
|
-
}
|
|
269
|
-
});
|
|
231
|
+
// Collect issues immediately after the window is loaded and analyze them
|
|
232
|
+
collectIssues().then(() => analyzeIssues());
|
|
270
233
|
});
|
|
271
234
|
}
|
|
272
235
|
|
|
@@ -274,8 +237,11 @@ function attachFailFast(): void {
|
|
|
274
237
|
* Exports the jsLogger module with methods to attach hooks, enable/disable logging, and set allowed warnings.
|
|
275
238
|
*/
|
|
276
239
|
export const jsErrorsLogger = {
|
|
277
|
-
attachHooks,
|
|
278
240
|
setAllowedJsWarnings,
|
|
241
|
+
getAllowedJsWarnings,
|
|
279
242
|
setStrategy,
|
|
243
|
+
getStrategy,
|
|
244
|
+
enable,
|
|
245
|
+
disable,
|
|
280
246
|
STRATEGY
|
|
281
247
|
};
|
|
@@ -5,8 +5,6 @@ import {logout} from './logout';
|
|
|
5
5
|
import {fixture} from './fixture';
|
|
6
6
|
import {repeatUntil} from './repeatUntil';
|
|
7
7
|
import {step} from './testStep';
|
|
8
|
-
// FUNCTIONALITY IS TEMPORARY DISABLED DUE TO UNEXPECTED ISSUES
|
|
9
|
-
// import {jsErrorsLogger} from './jsErrorsLogger';
|
|
10
8
|
|
|
11
9
|
export const registerSupport = (): void => {
|
|
12
10
|
Cypress.Commands.add('apolloClient', apolloClient);
|
|
@@ -26,11 +24,4 @@ export const registerSupport = (): void => {
|
|
|
26
24
|
Cypress.Commands.overwrite('fixture', fixture);
|
|
27
25
|
|
|
28
26
|
Cypress.Commands.add('step', step);
|
|
29
|
-
|
|
30
|
-
// Since registerSupport() function is called in each repo from e2e.js,
|
|
31
|
-
// attaching the JavaScript errors logger hooks here ensures that logger is initialized automatically
|
|
32
|
-
// for all tests without needing to call it explicitly in each test file.
|
|
33
|
-
// This is useful for capturing and logging JavaScript errors across all tests.
|
|
34
|
-
// FUNCTIONALITY IS TEMPORARY DISABLED DUE TO UNEXPECTED ISSUES
|
|
35
|
-
// jsErrorsLogger.attachHooks();
|
|
36
27
|
};
|