Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions bin/testObservability/cypress/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@ const STEP_KEYWORDS = ['given', 'when', 'then', 'and', 'but', '*'];
let eventsQueue = [];
let testRunStarted = false;

/*
* Command args (command.attributes.args) and cy.log items are captured raw and can hold
* circular Cypress runtime objects (e.g. a config-like object whose `renderOptions.host`
* points back to itself). cy.task() JSON-serializes its payload to ship it from the browser
* to the Node plugin process, so a circular arg makes Cypress throw
* "Converting circular structure to JSON" and aborts the run. Decycle the payload before
* handing it to cy.task so o11y instrumentation can never break the customer's tests. [SDK-6016]
*/
const getCircularReplacer = () => {
Comment thread
xxshubhamxx marked this conversation as resolved.
const seen = new WeakSet();
return (key, value) => {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) return '[Circular]';
Comment thread
xxshubhamxx marked this conversation as resolved.
Comment thread
xxshubhamxx marked this conversation as resolved.
seen.add(value);
}
return value;
};
};

const sanitizeForTask = (data) => {
try {
return JSON.parse(JSON.stringify(data, getCircularReplacer()));
} catch (e) {
/* Never let serialization of o11y data break the user's test run (graceful degradation). */
return { serializationError: e && e.message ? e.message : String(e) };
Comment thread
xxshubhamxx marked this conversation as resolved.
Outdated
}
};

const browserStackLog = (message) => {

if (!Cypress.env('BROWSERSTACK_LOGS')) return;
Expand Down Expand Up @@ -208,7 +236,7 @@ Cypress.on('command:enqueued', (attrs) => {
if (args.includes('test_observability_log') || args.includes('test_observability_command')) return;
const message = args.reduce((result, logItem) => {
if (typeof logItem === 'object') {
return [result, JSON.stringify(logItem)].join(' ');
return [result, JSON.stringify(logItem, getCircularReplacer())].join(' ');
Comment thread
xxshubhamxx marked this conversation as resolved.
Outdated
}
return [result, logItem ? logItem.toString() : ''].join(' ');
}, '');
Expand Down Expand Up @@ -309,7 +337,7 @@ beforeEach(() => {

if (eventsQueue.length > 0) {
eventsQueue.forEach(event => {
cy.task(event.task, event.data, event.options);
cy.task(event.task, sanitizeForTask(event.data), event.options);
});
}
eventsQueue = [];
Expand All @@ -324,7 +352,7 @@ afterEach(function() {

if (eventsQueue.length > 0) {
eventsQueue.forEach(event => {
cy.task(event.task, event.data, event.options);
cy.task(event.task, sanitizeForTask(event.data), event.options);
});
}

Expand Down
Loading