Skip to content

Commit

Permalink
Fix bad console assignment
Browse files Browse the repository at this point in the history
Follows-up 826d2c7.

Before that commit, the reason global "console" was globbered is that the
`var console` part of the inner `var console = {}` assignment was hosted by
the JavaScript engine. Thus the variable always existed in the local scope as
type "undefined", and so the conditional check never saw the real console, and
always created a custom one. Thus it always deleted the original console reference,
as well as any other it contained.

In commit 826d2c7, this was incorrectly fixed by assigning the unchecked
expression referring to `console`, thus no longer having a fallback to `{}` in older
browsers, because an unchecked reference like that throws an Uncaught ReferenceError.

As a result, browserstack-runner was unable to run in IE 9 or older.

Fixes #161.
Fixes #164.
Fixes #212.
  • Loading branch information
Krinkle authored and francisf committed Apr 21, 2021
1 parent d75cbb4 commit d76b3df
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/_patch/browserstack.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@
req.send(data);
}

var browserstack_console = console || window.console || {};
// Change some console method to capture the logs.
// This must not replace the console object itself so that other console methods
// from the browser or added by the tested application remain unaffected.
// https://github.com/browserstack/browserstack-runner/pull/199
var browserstack_console = window.console || {};
browserstack_console.log = function () {
var args = BrowserStack.util.toArray(arguments).map(BrowserStack.util.inspect);
post('/_log/', { arguments: args }, function () {});
Expand All @@ -54,6 +58,7 @@
BrowserStack.worker_uuid = getParameterByName('_worker_key');

window.BrowserStack = BrowserStack;
// If the browser didn't have a console object (old IE), then this will create it.
// Otherwise this is a no-op as it will assign the same object it already held.
window.console = browserstack_console;
console = browserstack_console;
})();

0 comments on commit d76b3df

Please sign in to comment.