From 3ebbf125ff3332f5d9aff204cd20025b3a6fb9a6 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Wed, 11 Jul 2018 21:05:16 -0700 Subject: [PATCH] fix(coverage): report anonymous scripts with debugger:// urls (#2875) Chrome DevTools shows anonymous scripts with yellow background and names them with `debugger://VM` prefix. This patch starts reporting the same debugger:// urls for anonymous scripts in puppeteer's JS coverage. This might simplify debugging, e.g. using `debugger;` statement to reveal the script in DevTools and later matching it against the one in the coverage. --- lib/Coverage.js | 4 +++- test/coverage.spec.js | 41 ++++++++++++++++++++--------------------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/lib/Coverage.js b/lib/Coverage.js index 756e8c53..b6f30bc2 100644 --- a/lib/Coverage.js +++ b/lib/Coverage.js @@ -144,7 +144,9 @@ class JSCoverage { const coverage = []; for (const entry of profileResponse.result) { - const url = this._scriptURLs.get(entry.scriptId); + let url = this._scriptURLs.get(entry.scriptId); + if (!url && this._reportAnonymousScripts) + url = 'debugger://VM' + entry.scriptId; const text = this._scriptSources.get(entry.scriptId); if (text === undefined || url === undefined) continue; diff --git a/test/coverage.spec.js b/test/coverage.spec.js index ede30769..37263aa7 100644 --- a/test/coverage.spec.js +++ b/test/coverage.spec.js @@ -38,27 +38,26 @@ module.exports.addTests = function({testRunner, expect}) { expect(coverage.length).toBe(1); expect(coverage[0].url).toBe('nicename.js'); }); - describe('anonymous scripts coverage', function() { - it('should ignore eval() scripts by default', async function({page, server}) { - await page.coverage.startJSCoverage(); - await page.goto(server.PREFIX + '/jscoverage/eval.html'); - const coverage = await page.coverage.stopJSCoverage(); - expect(coverage.length).toBe(1); - }); - it('shouldn\'t ignore eval() scripts if reportAnonymousScripts is true', async function({page, server}) { - await page.coverage.startJSCoverage({reportAnonymousScripts: true}); - await page.goto(server.PREFIX + '/jscoverage/eval.html'); - const coverage = await page.coverage.stopJSCoverage(); - expect(coverage.length).toBe(2); - }); - it('should ignore injected scripts regardless of reportAnonymousScripts', async function({page, server}) { - await page.coverage.startJSCoverage({reportAnonymousScripts: true}); - await page.goto(server.EMPTY_PAGE); - await page.evaluate('console.log("foo")'); - await page.evaluate(() => console.log('bar')); - const coverage = await page.coverage.stopJSCoverage(); - expect(coverage.length).toBe(0); - }); + it('should ignore eval() scripts by default', async function({page, server}) { + await page.coverage.startJSCoverage(); + await page.goto(server.PREFIX + '/jscoverage/eval.html'); + const coverage = await page.coverage.stopJSCoverage(); + expect(coverage.length).toBe(1); + }); + it('shouldn\'t ignore eval() scripts if reportAnonymousScripts is true', async function({page, server}) { + await page.coverage.startJSCoverage({reportAnonymousScripts: true}); + await page.goto(server.PREFIX + '/jscoverage/eval.html'); + const coverage = await page.coverage.stopJSCoverage(); + expect(coverage.find(entry => entry.url.startsWith('debugger://'))).not.toBe(null); + expect(coverage.length).toBe(2); + }); + it('should ignore pptr internal scripts if reportAnonymousScripts is true', async function({page, server}) { + await page.coverage.startJSCoverage({reportAnonymousScripts: true}); + await page.goto(server.EMPTY_PAGE); + await page.evaluate('console.log("foo")'); + await page.evaluate(() => console.log('bar')); + const coverage = await page.coverage.stopJSCoverage(); + expect(coverage.length).toBe(0); }); it('should report multiple scripts', async function({page, server}) { await page.coverage.startJSCoverage();