Inject file with sourceURL (#102)

This patch starts adding a sourceURL trailing comment to make stack traces
readable.
This commit is contained in:
JoelEinbinder 2017-07-23 09:56:35 -07:00 committed by Andrey Lushnikov
parent c4904b4e10
commit fdaaa2c0e6
4 changed files with 11 additions and 5 deletions

View File

@ -140,12 +140,13 @@ class Page extends EventEmitter {
* @return {!Promise} * @return {!Promise}
*/ */
async injectFile(filePath) { async injectFile(filePath) {
const contents = await new Promise((resolve, reject) => { let contents = await new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf8', (err, data) => { fs.readFile(filePath, 'utf8', (err, data) => {
if (err) return reject(err); if (err) return reject(err);
resolve(data); resolve(data);
}); });
}); });
contents += `//# sourceURL=` + filePath.replace(/\n/g,'');
return this._client.send('Runtime.evaluate', { expression: contents, returnByValue: true }); return this._client.send('Runtime.evaluate', { expression: contents, returnByValue: true });
} }

View File

@ -1 +1,2 @@
__injected = 42; __injected = 42;
__injectedError = new Error('hi');

View File

@ -145,6 +145,12 @@ describe('Puppeteer', function() {
const result = await page.evaluate(() => __injected); const result = await page.evaluate(() => __injected);
expect(result).toBe(42); 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() { describe('Frame.evaluate', function() {

View File

@ -1,5 +1,3 @@
//! unsupported
// A SyntaxError leaks to phantom.onError, despite the try-catch. // A SyntaxError leaks to phantom.onError, despite the try-catch.
setup({ allow_uncaught_exception: true }); setup({ allow_uncaught_exception: true });
@ -7,8 +5,8 @@ test(function () {
var helperFile = "../fixtures/parse-error-helper.js"; var helperFile = "../fixtures/parse-error-helper.js";
try { try {
phantom.injectJs(helperFile); phantom.injectJs(helperFile);
assert_is_true(false);
} catch (e) { } catch (e) {
assert_equals(e.stack[0].file, helperFile); assert_is_true(e.stack.indexOf('fixtures/parse-error-helper.js:2') !== -1);
assert_equals(e.stack[0].line, 2);
} }
}, "stack trace from syntax error in injected file"); }, "stack trace from syntax error in injected file");