@@ -202,11 +202,18 @@ Cypress.on('command:end', (command) => {
202202} ) ;
203203
204204/*
205- * cy.log capture must happen at command-enqueue time, not command-execute time.
206- * If a test body throws synchronously (e.g. a failing chai assertion) before the
207- * Cypress queue drains, queued commands are dropped — so an execute-time wrapper
208- * never fires and pre-throw cy.log calls are lost from the timeline. The
209- * command:enqueued event runs synchronously at the user's cy.log() call site.
205+ * cy.log capture must happen at command-enqueue time AND must bypass Cypress's
206+ * test command queue. When a test body throws synchronously (e.g. a failing
207+ * chai assertion), Cypress drops every pending command in the test queue —
208+ * which means an execute-time wrapper on cy.log never runs, and even a
209+ * deferred cy.task(...) inside the SDK's afterEach is just another queued
210+ * command that won't survive the drop.
211+ *
212+ * Cypress.backend('task', ...) emits directly over the runner-to-Node
213+ * websocket (see Cypress driver), bypassing the queue entirely. Combined
214+ * with command:enqueued (which fires synchronously at the user's cy.log()
215+ * call site, before the throw), the log reaches the Node-side task handler
216+ * regardless of whether the test passes or fails.
210217 */
211218Cypress . on ( 'command:enqueued' , ( attrs ) => {
212219 if ( ! Cypress . env ( 'BROWSERSTACK_O11Y_LOGS' ) ) return ;
@@ -219,14 +226,16 @@ Cypress.on('command:enqueued', (attrs) => {
219226 }
220227 return [ result , logItem ? logItem . toString ( ) : '' ] . join ( ' ' ) ;
221228 } , '' ) ;
222- eventsQueue . push ( {
229+ Cypress . backend ( 'task' , {
223230 task : 'test_observability_log' ,
224- data : {
231+ arg : {
225232 level : 'info' ,
226233 message,
227234 timestamp : new Date ( ) . toISOString ( )
228235 } ,
229- options : { log : false }
236+ timeout : 60000
237+ } ) . catch ( ( ) => {
238+ /* Don't let observability failures bubble into the test */
230239 } ) ;
231240} ) ;
232241
0 commit comments