diff --git a/lib/Page.js b/lib/Page.js index 6105fc878c4..2e41da22e4e 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -140,12 +140,13 @@ class Page extends EventEmitter { * @return {!Promise} */ async injectFile(filePath) { - const contents = await new Promise((resolve, reject) => { + let contents = await new Promise((resolve, reject) => { fs.readFile(filePath, 'utf8', (err, data) => { if (err) return reject(err); resolve(data); }); }); + contents += `//# sourceURL=` + filePath.replace(/\n/g,''); return this._client.send('Runtime.evaluate', { expression: contents, returnByValue: true }); } diff --git a/test/assets/injectedfile.js b/test/assets/injectedfile.js index 57b4464fcdd..88c9ba5f0fa 100644 --- a/test/assets/injectedfile.js +++ b/test/assets/injectedfile.js @@ -1 +1,2 @@ __injected = 42; +__injectedError = new Error('hi'); \ No newline at end of file diff --git a/test/test.js b/test/test.js index cac8fe46905..7c019a677b4 100644 --- a/test/test.js +++ b/test/test.js @@ -145,6 +145,12 @@ describe('Puppeteer', function() { const result = await page.evaluate(() => __injected); expect(result).toBe(42); })); + it('should include sourcemap', SX(async function() { + const helloPath = path.join(__dirname, './assets/injectedfile.js'); + await page.injectFile(helloPath); + const result = await page.evaluate(() => __injectedError.stack); + expect(result).toContain('assets/injectedfile.js'); + })); }); describe('Frame.evaluate', function() { diff --git a/third_party/phantomjs/test/basics/stacktrace.js b/third_party/phantomjs/test/basics/stacktrace.js index 916e8fdb48c..d195144e0e7 100644 --- a/third_party/phantomjs/test/basics/stacktrace.js +++ b/third_party/phantomjs/test/basics/stacktrace.js @@ -1,5 +1,3 @@ -//! unsupported - // A SyntaxError leaks to phantom.onError, despite the try-catch. setup({ allow_uncaught_exception: true }); @@ -7,8 +5,8 @@ test(function () { var helperFile = "../fixtures/parse-error-helper.js"; try { phantom.injectJs(helperFile); + assert_is_true(false); } catch (e) { - assert_equals(e.stack[0].file, helperFile); - assert_equals(e.stack[0].line, 2); + assert_is_true(e.stack.indexOf('fixtures/parse-error-helper.js:2') !== -1); } }, "stack trace from syntax error in injected file");