Skip to content

Commit 7e65d23

Browse files
committed
fix: cy.now with cy.task
1 parent 2949cc6 commit 7e65d23

File tree

6 files changed

+54
-68
lines changed

6 files changed

+54
-68
lines changed
Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
/* Event listeners + custom commands for Cypress */
22

33
/* Used to detect Gherkin steps */
4+
5+
const browserStackLog = (message) => {
6+
if (!Cypress.env('BROWSERSTACK_LOGS')) return;
7+
cy.task('browserstack_log', message);
8+
}
9+
10+
const shouldSkipCommand = (command) => {
11+
return command.attributes.name == 'log' || (command.attributes.name == 'task' && (['test_observability_platform_details', 'test_observability_step', 'test_observability_command'].some(event => command.attributes.args.includes(event))));
12+
}
13+
414
Cypress.on('log:added', (log) => {
5-
return () => {
6-
return cy.now('task', 'test_observability_step', {
7-
log
8-
}, {log: false})
9-
}
10-
});
11-
15+
return () => {
16+
return cy.task('test_observability_step', {
17+
log
18+
}, { log: false })
19+
}
20+
});
21+
1222
Cypress.on('command:start', (command) => {
13-
if(!command || !command.attributes) return;
14-
if(command.attributes.name == 'log' || (command.attributes.name == 'task' && (command.attributes.args.includes('test_observability_command') || command.attributes.args.includes('test_observability_log')))) {
23+
if (!command || !command.attributes) return;
24+
if (shouldSkipCommand(command)) {
1525
return;
1626
}
1727
/* Send command details */
18-
cy.now('task', 'test_observability_command', {
28+
cy.task('test_observability_command', {
1929
type: 'COMMAND_START',
2030
command: {
2131
attributes: {
@@ -25,27 +35,23 @@ Cypress.on('command:start', (command) => {
2535
},
2636
state: 'pending'
2737
}
28-
}, {log: false}).then((res) => {
29-
}).catch((err) => {
30-
});
38+
}, { log: false });
3139

3240
/* Send platform details */
33-
cy.now('task', 'test_observability_platform_details', {
34-
testTitle: Cypress.currentTest.title,
41+
cy.task('test_observability_platform_details', {
42+
testTitle: Cypress.currentRunnable?.title || '',
3543
browser: Cypress.browser,
3644
platform: Cypress.platform,
3745
cypressVersion: Cypress.version
38-
}, {log: false}).then((res) => {
39-
}).catch((err) => {
40-
});
46+
}, { log: false });
4147
});
4248

4349
Cypress.on('command:retry', (command) => {
44-
if(!command || !command.attributes) return;
45-
if(command.attributes.name == 'log' || (command.attributes.name == 'task' && (command.attributes.args.includes('test_observability_command') || command.attributes.args.includes('test_observability_log')))) {
50+
if (!command || !command.attributes) return;
51+
if (shouldSkipCommand(command)) {
4652
return;
4753
}
48-
cy.now('task', 'test_observability_command', {
54+
cy.task('test_observability_command', {
4955
type: 'COMMAND_RETRY',
5056
command: {
5157
_log: command._log,
@@ -54,17 +60,15 @@ Cypress.on('command:retry', (command) => {
5460
isDefaultAssertionErr: command && command.error ? command.error.isDefaultAssertionErr : null
5561
}
5662
}
57-
}, {log: false}).then((res) => {
58-
}).catch((err) => {
59-
});
63+
}, { log: false });
6064
});
6165

6266
Cypress.on('command:end', (command) => {
63-
if(!command || !command.attributes) return;
64-
if(command.attributes.name == 'log' || (command.attributes.name == 'task' && (command.attributes.args.includes('test_observability_command') || command.attributes.args.includes('test_observability_log')))) {
67+
if (!command || !command.attributes) return;
68+
if (shouldSkipCommand(command)) {
6569
return;
6670
}
67-
cy.now('task', 'test_observability_command', {
71+
cy.task('test_observability_command', {
6872
'type': 'COMMAND_END',
6973
'command': {
7074
'attributes': {
@@ -74,85 +78,69 @@ Cypress.on('command:end', (command) => {
7478
},
7579
'state': command.state
7680
}
77-
}, {log: false}).then((res) => {
78-
}).catch((err) => {
79-
});
81+
}, { log: false });
8082
});
8183

8284
Cypress.Commands.overwrite('log', (originalFn, ...args) => {
83-
if(args.includes('test_observability_log') || args.includes('test_observability_command')) return;
85+
if (args.includes('test_observability_log') || args.includes('test_observability_command')) return;
8486
const message = args.reduce((result, logItem) => {
8587
if (typeof logItem === 'object') {
8688
return [result, JSON.stringify(logItem)].join(' ');
8789
}
8890

8991
return [result, logItem ? logItem.toString() : ''].join(' ');
9092
}, '');
91-
cy.now('task', 'test_observability_log', {
93+
cy.task('test_observability_log', {
9294
'level': 'info',
9395
message,
94-
}, {log: false}).then((res) => {
95-
}).catch((err) => {
96-
});
96+
}, { log: false });
9797
originalFn(...args);
9898
});
9999

100100
Cypress.Commands.add('trace', (message, file) => {
101-
cy.now('task', 'test_observability_log', {
101+
cy.task('test_observability_log', {
102102
level: 'trace',
103103
message,
104104
file,
105-
}).then((res) => {
106-
}).catch((err) => {
107105
});
108106
});
109107

110108
Cypress.Commands.add('logDebug', (message, file) => {
111-
cy.now('task', 'test_observability_log', {
109+
cy.task('test_observability_log', {
112110
level: 'debug',
113111
message,
114112
file,
115-
}).then((res) => {
116-
}).catch((err) => {
117113
});
118114
});
119115

120116
Cypress.Commands.add('info', (message, file) => {
121-
cy.now('task', 'test_observability_log', {
117+
cy.task('test_observability_log', {
122118
level: 'info',
123119
message,
124120
file,
125-
}).then((res) => {
126-
}).catch((err) => {
127121
});
128122
});
129123

130124
Cypress.Commands.add('warn', (message, file) => {
131-
cy.now('task', 'test_observability_log', {
125+
cy.task('test_observability_log', {
132126
level: 'warn',
133127
message,
134128
file,
135-
}).then((res) => {
136-
}).catch((err) => {
137129
});
138130
});
139131

140132
Cypress.Commands.add('error', (message, file) => {
141-
cy.now('task', 'test_observability_log', {
133+
cy.task('test_observability_log', {
142134
level: 'error',
143135
message,
144136
file,
145-
}).then((res) => {
146-
}).catch((err) => {
147137
});
148138
});
149139

150140
Cypress.Commands.add('fatal', (message, file) => {
151-
cy.now('task', 'test_observability_log', {
141+
cy.task('test_observability_log', {
152142
level: 'fatal',
153143
message,
154144
file,
155-
}).then((res) => {
156-
}).catch((err) => {
157145
});
158146
});

bin/testObservability/helper/cleanupQueueSync.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
* Sending all the remaining queues for synchronous manner
33
*/
44

5-
const RequestQueueHandler = require('./requestQueueHandler');
5+
const requestHandler = require('./requestQueueHandler');
66

77
const shutdown = async () => {
8-
const requestHandler = new RequestQueueHandler();
98
requestHandler.queue = require(process.argv[2].trim());
109
await requestHandler.shutdown();
1110
}

bin/testObservability/helper/helper.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ exports.printBuildLink = async (shouldStopSession, exitCode = null) => {
117117
}
118118

119119
const nodeRequest = (type, url, data, config) => {
120+
const requestQueueHandler = require('./requestQueueHandler');
120121
return new Promise(async (resolve, reject) => {
121122
const options = {
122123
...config,
@@ -140,8 +141,8 @@ const nodeRequest = (type, url, data, config) => {
140141
options.proxy = false
141142
options.httpsAgent = new HttpsProxyAgent(process.env.HTTPS_PROXY);
142143
}
143-
144-
if(url === exports.requestQueueHandler.screenshotEventUrl) {
144+
145+
if(url === requestQueueHandler.screenshotEventUrl) {
145146
options.agent = httpsScreenshotsKeepAliveAgent;
146147
}
147148
axios(options)
@@ -521,10 +522,9 @@ exports.batchAndPostEvents = async (eventUrl, kind, data) => {
521522
}
522523
}
523524

524-
const RequestQueueHandler = require('./requestQueueHandler');
525-
exports.requestQueueHandler = new RequestQueueHandler();
526-
527525
exports.uploadEventData = async (eventData, run=0) => {
526+
527+
const requestQueueHandler = require('./requestQueueHandler');
528528
exports.debugOnConsole(`[uploadEventData] ${eventData.event_type}`);
529529
const log_tag = {
530530
['TestRunStarted']: 'Test_Start_Upload',
@@ -550,8 +550,8 @@ exports.uploadEventData = async (eventData, run=0) => {
550550
} else {
551551
let data = eventData, event_api_url = 'api/v1/event';
552552

553-
exports.requestQueueHandler.start();
554-
const { shouldProceed, proceedWithData, proceedWithUrl } = exports.requestQueueHandler.add(eventData);
553+
requestQueueHandler.start();
554+
const { shouldProceed, proceedWithData, proceedWithUrl } = requestQueueHandler.add(eventData);
555555
exports.debugOnConsole(`[Request Queue] ${eventData.event_type} with uuid ${eventData.test_run ? eventData.test_run.uuid : (eventData.hook_run ? eventData.hook_run.uuid : null)} is added`)
556556
if(!shouldProceed) {
557557
return;
@@ -576,7 +576,7 @@ exports.uploadEventData = async (eventData, run=0) => {
576576
if(response.data.error) {
577577
throw({message: response.data.error});
578578
} else {
579-
exports.debug(`${event_api_url !== exports.requestQueueHandler.eventUrl ? log_tag : 'Batch-Queue'}[${run}] event successfull!`)
579+
exports.debug(`${event_api_url !== requestQueueHandler.eventUrl ? log_tag : 'Batch-Queue'}[${run}] event successfull!`)
580580
exports.pending_test_uploads.count = Math.max(0,exports.pending_test_uploads.count - (event_api_url === 'api/v1/event' ? 1 : data.length));
581581
return {
582582
status: 'success',
@@ -586,9 +586,9 @@ exports.uploadEventData = async (eventData, run=0) => {
586586
} catch(error) {
587587
exports.debugOnConsole(`[Request Error] Error in sending request ${util.format(error)}`);
588588
if (error.response) {
589-
exports.debug(`EXCEPTION IN ${event_api_url !== exports.requestQueueHandler.eventUrl ? log_tag : 'Batch-Queue'} REQUEST TO TEST OBSERVABILITY : ${error.response.status} ${error.response.statusText} ${JSON.stringify(error.response.data)}`, true, error);
589+
exports.debug(`EXCEPTION IN ${event_api_url !== requestQueueHandler.eventUrl ? log_tag : 'Batch-Queue'} REQUEST TO TEST OBSERVABILITY : ${error.response.status} ${error.response.statusText} ${JSON.stringify(error.response.data)}`, true, error);
590590
} else {
591-
exports.debug(`EXCEPTION IN ${event_api_url !== exports.requestQueueHandler.eventUrl ? log_tag : 'Batch-Queue'} REQUEST TO TEST OBSERVABILITY : ${error.message || error}`, true, error);
591+
exports.debug(`EXCEPTION IN ${event_api_url !== requestQueueHandler.eventUrl ? log_tag : 'Batch-Queue'} REQUEST TO TEST OBSERVABILITY : ${error.message || error}`, true, error);
592592
}
593593
exports.pending_test_uploads.count = Math.max(0,exports.pending_test_uploads.count - (event_api_url === 'api/v1/event' ? 1 : data.length));
594594
return {

bin/testObservability/helper/requestQueueHandler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,4 @@ class RequestQueueHandler {
9898
}
9999
}
100100

101-
module.exports = RequestQueueHandler;
101+
module.exports = new RequestQueueHandler();

bin/testObservability/plugin/ipcServer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const ipc = require('node-ipc');
22
const { consoleHolder } = require('../helper/constants');
3-
const { requestQueueHandler } = require('../helper/helper');
3+
const requestQueueHandler = require('../helper/requestQueueHandler');
44

55
exports.startIPCServer = (subscribeServerEvents, unsubscribeServerEvents) => {
66
if (ipc.server) {

bin/testObservability/reporter/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ const {
5252
mapTestHooks,
5353
debug,
5454
isBrowserstackInfra,
55-
requestQueueHandler,
5655
getHookSkippedTests,
5756
getOSDetailsFromSystem,
5857
findGitConfig,

0 commit comments

Comments
 (0)