Skip to content

Commit

Permalink
fix: console assert only logs when arg 0 is falsy (rrweb-io#1530)
Browse files Browse the repository at this point in the history
* fix: console assert only logs when arg 0 is falsy
  • Loading branch information
pauldambra authored and jxiwang committed Oct 22, 2024
1 parent b834f12 commit 586a9e1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-experts-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rrweb/rrweb-plugin-console-record": patch
---

corrects behaviour of console.assert logging to only capture logs when the provided assertion is falsey
12 changes: 11 additions & 1 deletion packages/plugins/rrweb-plugin-console-record/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ function initLogObserver(
(original: (...args: Array<unknown>) => void) => {
return (...args: Array<unknown>) => {
original.apply(this, args);

if (level === 'assert' && !!args[0]) {
// assert does not log if the first argument evaluates to true
return;
}

if (inStack) {
// If we are already in a stack this means something from the following code is calling a console method
// likely a proxy method called from stringify. We don't want to log this as it will cause an infinite loop
Expand All @@ -203,7 +209,11 @@ function initLogObserver(
const trace = ErrorStackParser.parse(new Error())
.map((stackFrame: StackFrame) => stackFrame.toString())
.splice(1); // splice(1) to omit the hijacked log function
const payload = args.map((s) =>

// assert does not log its first arg, that's only used for deciding whether to log
const argsForPayload = level === 'assert' ? args.slice(1) : args;

const payload = argsForPayload.map((s) =>
stringify(s, logOptions.stringifyOptions),
);
logCount++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,10 @@ exports[`rrweb-plugin-console-record > should record console messages 1`] = `
\\"payload\\": {
\\"level\\": \\"assert\\",
\\"trace\\": [
\\"__puppeteer_evaluation_script__:2:15\\"
\\"__puppeteer_evaluation_script__:3:15\\"
],
\\"payload\\": [
\\"true\\",
\\"\\\\\\"assert\\\\\\"\\"
\\"\\\\\\"should log assert\\\\\\"\\"
]
}
}
Expand All @@ -372,7 +371,7 @@ exports[`rrweb-plugin-console-record > should record console messages 1`] = `
\\"payload\\": {
\\"level\\": \\"count\\",
\\"trace\\": [
\\"__puppeteer_evaluation_script__:3:15\\"
\\"__puppeteer_evaluation_script__:4:15\\"
],
\\"payload\\": [
\\"\\\\\\"count\\\\\\"\\"
Expand All @@ -387,7 +386,7 @@ exports[`rrweb-plugin-console-record > should record console messages 1`] = `
\\"payload\\": {
\\"level\\": \\"countReset\\",
\\"trace\\": [
\\"__puppeteer_evaluation_script__:4:15\\"
\\"__puppeteer_evaluation_script__:5:15\\"
],
\\"payload\\": [
\\"\\\\\\"count\\\\\\"\\"
Expand All @@ -402,7 +401,7 @@ exports[`rrweb-plugin-console-record > should record console messages 1`] = `
\\"payload\\": {
\\"level\\": \\"debug\\",
\\"trace\\": [
\\"__puppeteer_evaluation_script__:5:15\\"
\\"__puppeteer_evaluation_script__:6:15\\"
],
\\"payload\\": [
\\"\\\\\\"debug\\\\\\"\\"
Expand All @@ -417,7 +416,7 @@ exports[`rrweb-plugin-console-record > should record console messages 1`] = `
\\"payload\\": {
\\"level\\": \\"dir\\",
\\"trace\\": [
\\"__puppeteer_evaluation_script__:6:15\\"
\\"__puppeteer_evaluation_script__:7:15\\"
],
\\"payload\\": [
\\"\\\\\\"dir\\\\\\"\\"
Expand All @@ -432,7 +431,7 @@ exports[`rrweb-plugin-console-record > should record console messages 1`] = `
\\"payload\\": {
\\"level\\": \\"dirxml\\",
\\"trace\\": [
\\"__puppeteer_evaluation_script__:7:15\\"
\\"__puppeteer_evaluation_script__:8:15\\"
],
\\"payload\\": [
\\"\\\\\\"dirxml\\\\\\"\\"
Expand All @@ -447,7 +446,7 @@ exports[`rrweb-plugin-console-record > should record console messages 1`] = `
\\"payload\\": {
\\"level\\": \\"group\\",
\\"trace\\": [
\\"__puppeteer_evaluation_script__:8:15\\"
\\"__puppeteer_evaluation_script__:9:15\\"
],
\\"payload\\": []
}
Expand All @@ -460,7 +459,7 @@ exports[`rrweb-plugin-console-record > should record console messages 1`] = `
\\"payload\\": {
\\"level\\": \\"groupCollapsed\\",
\\"trace\\": [
\\"__puppeteer_evaluation_script__:9:15\\"
\\"__puppeteer_evaluation_script__:10:15\\"
],
\\"payload\\": []
}
Expand All @@ -473,7 +472,7 @@ exports[`rrweb-plugin-console-record > should record console messages 1`] = `
\\"payload\\": {
\\"level\\": \\"info\\",
\\"trace\\": [
\\"__puppeteer_evaluation_script__:10:15\\"
\\"__puppeteer_evaluation_script__:11:15\\"
],
\\"payload\\": [
\\"\\\\\\"info\\\\\\"\\"
Expand All @@ -488,7 +487,7 @@ exports[`rrweb-plugin-console-record > should record console messages 1`] = `
\\"payload\\": {
\\"level\\": \\"log\\",
\\"trace\\": [
\\"__puppeteer_evaluation_script__:11:15\\"
\\"__puppeteer_evaluation_script__:12:15\\"
],
\\"payload\\": [
\\"\\\\\\"log\\\\\\"\\"
Expand All @@ -503,7 +502,7 @@ exports[`rrweb-plugin-console-record > should record console messages 1`] = `
\\"payload\\": {
\\"level\\": \\"table\\",
\\"trace\\": [
\\"__puppeteer_evaluation_script__:12:15\\"
\\"__puppeteer_evaluation_script__:13:15\\"
],
\\"payload\\": [
\\"\\\\\\"table\\\\\\"\\"
Expand All @@ -518,7 +517,7 @@ exports[`rrweb-plugin-console-record > should record console messages 1`] = `
\\"payload\\": {
\\"level\\": \\"time\\",
\\"trace\\": [
\\"__puppeteer_evaluation_script__:13:15\\"
\\"__puppeteer_evaluation_script__:14:15\\"
],
\\"payload\\": []
}
Expand All @@ -531,7 +530,7 @@ exports[`rrweb-plugin-console-record > should record console messages 1`] = `
\\"payload\\": {
\\"level\\": \\"timeEnd\\",
\\"trace\\": [
\\"__puppeteer_evaluation_script__:14:15\\"
\\"__puppeteer_evaluation_script__:15:15\\"
],
\\"payload\\": []
}
Expand All @@ -544,7 +543,7 @@ exports[`rrweb-plugin-console-record > should record console messages 1`] = `
\\"payload\\": {
\\"level\\": \\"timeLog\\",
\\"trace\\": [
\\"__puppeteer_evaluation_script__:15:15\\"
\\"__puppeteer_evaluation_script__:16:15\\"
],
\\"payload\\": []
}
Expand All @@ -557,7 +556,7 @@ exports[`rrweb-plugin-console-record > should record console messages 1`] = `
\\"payload\\": {
\\"level\\": \\"trace\\",
\\"trace\\": [
\\"__puppeteer_evaluation_script__:16:15\\"
\\"__puppeteer_evaluation_script__:17:15\\"
],
\\"payload\\": [
\\"\\\\\\"trace\\\\\\"\\"
Expand All @@ -572,7 +571,7 @@ exports[`rrweb-plugin-console-record > should record console messages 1`] = `
\\"payload\\": {
\\"level\\": \\"warn\\",
\\"trace\\": [
\\"__puppeteer_evaluation_script__:17:15\\"
\\"__puppeteer_evaluation_script__:18:15\\"
],
\\"payload\\": [
\\"\\\\\\"warn\\\\\\"\\"
Expand All @@ -587,7 +586,7 @@ exports[`rrweb-plugin-console-record > should record console messages 1`] = `
\\"payload\\": {
\\"level\\": \\"clear\\",
\\"trace\\": [
\\"__puppeteer_evaluation_script__:18:15\\"
\\"__puppeteer_evaluation_script__:19:15\\"
],
\\"payload\\": []
}
Expand All @@ -600,10 +599,10 @@ exports[`rrweb-plugin-console-record > should record console messages 1`] = `
\\"payload\\": {
\\"level\\": \\"log\\",
\\"trace\\": [
\\"__puppeteer_evaluation_script__:19:15\\"
\\"__puppeteer_evaluation_script__:20:15\\"
],
\\"payload\\": [
\\"\\\\\\"TypeError: a message\\\\\\\\n at __puppeteer_evaluation_script__:19:19\\\\\\\\nEnd of stack for Error object\\\\\\"\\"
\\"\\\\\\"TypeError: a message\\\\\\\\n at __puppeteer_evaluation_script__:20:19\\\\\\\\nEnd of stack for Error object\\\\\\"\\"
]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ describe('rrweb-plugin-console-record', () => {
await page.goto(`${serverUrl}test/html/log.html`);

await page.evaluate(() => {
console.assert(0 === 0, 'assert');
// truthy assert does not log
console.assert(0 === 0, 'should not log assert');
// falsy assert does log
console.assert(false, 'should log assert');
console.count('count');
console.countReset('count');
console.debug('debug');
Expand Down

0 comments on commit 586a9e1

Please sign in to comment.