fix: CSS coverage should work with empty stylesheets (#8570)

Closes #8535
This commit is contained in:
Alex Rudenko 2022-06-24 14:17:16 +02:00 committed by GitHub
parent 5e6b93f7e3
commit 383e855847
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 2 deletions

View File

@ -407,9 +407,15 @@ export class CSSCoverage {
const coverage: CoverageEntry[] = [];
for (const styleSheetId of this.#stylesheetURLs.keys()) {
const url = this.#stylesheetURLs.get(styleSheetId);
assert(url);
assert(
typeof url !== 'undefined',
`Stylesheet URL is undefined (styleSheetId=${styleSheetId})`
);
const text = this.#stylesheetSources.get(styleSheetId);
assert(text);
assert(
typeof text !== 'undefined',
`Stylesheet text is undefined (styleSheetId=${styleSheetId})`
);
const ranges = convertToDisjointRanges(
styleSheetIdToCoverage.get(styleSheetId) || []
);

View File

@ -0,0 +1,3 @@
<style></style>
<div>empty style tag</div>

View File

@ -270,6 +270,15 @@ describe('Coverage specs', function () {
JSON.stringify(coverage, null, 2).replace(/:\d{4}\//g, ':<PORT>/')
).toBeGolden('csscoverage-involved.txt');
});
it('should work with empty stylesheets', async () => {
const {page, server} = getTestState();
await page.coverage.startCSSCoverage();
await page.goto(server.PREFIX + '/csscoverage/empty.html');
const coverage = await page.coverage.stopCSSCoverage();
expect(coverage.length).toEqual(1);
expect(coverage[0]!.text).toEqual('');
});
it('should ignore injected stylesheets', async () => {
const {page} = getTestState();