From a71f287c6b397389787f838b9be87f8a016d0e88 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Mon, 11 Sep 2017 16:59:38 -0700 Subject: [PATCH] Fix evaluation in case of first "undefind" argument (#732) It turns out that [undefined, 1].join(',') results in ",1" instead of "undefined,1". This causes a syntax error when trying to pass undefined as a first argument to `page.evaluate` method. Fixes #572. --- lib/helper.js | 12 +++++++++++- test/test.js | 4 ++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/helper.js b/lib/helper.js index 192707b1..18ef2b7b 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -28,7 +28,17 @@ class Helper { console.assert(args.length === 0, 'Cannot evaluate a string with arguments'); return fun; } - return `(${fun})(${args.map(x => JSON.stringify(x)).join(',')})`; + return `(${fun})(${args.map(serializeArgument).join(',')})`; + + /** + * @param {*} arg + * @return {string} + */ + function serializeArgument(arg) { + if (Object.is(arg, undefined)) + return 'undefined'; + return JSON.stringify(arg); + } } /** diff --git a/test/test.js b/test/test.js index 888592cc..01634278 100644 --- a/test/test.js +++ b/test/test.js @@ -246,6 +246,10 @@ describe('Page', function() { const result = await page.evaluate(() => -Infinity); expect(Object.is(result, -Infinity)).toBe(true); })); + it('should accept "undefined" as one of multiple parameters', SX(async function() { + const result = await page.evaluate((a, b) => Object.is(a, undefined) && Object.is(b, 'foo'), undefined, 'foo'); + expect(result).toBe(true); + })); it('should not fail for window object', SX(async function() { const result = await page.evaluate(() => window); expect(result).toBe('Window');