-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
99 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,98 @@ | ||
import fs from 'node:fs'; | ||
import v8toIstanbul from 'v8-to-istanbul'; | ||
import {test as base} from '@playwright/test'; | ||
import {type Page, test as base} from '@playwright/test'; | ||
|
||
// Declare the types of your fixtures. | ||
type MyFixtures = { | ||
_coveredPage: undefined; | ||
coverage: {newPage: () => Promise<Page>}; | ||
}; | ||
|
||
export const test = base.extend<MyFixtures>({ | ||
async _coveredPage({page}, use, testInfo) { | ||
async coverage({page, context}, use, testInfo) { | ||
// eslint-disable-next-line n/prefer-global/process | ||
if (process.env.COVERAGE) { | ||
await page.coverage.startJSCoverage(); | ||
} | ||
|
||
await use(undefined); | ||
const pages: Page[] = [page]; | ||
|
||
// eslint-disable-next-line n/prefer-global/process | ||
if (!process.env.COVERAGE) { | ||
return; | ||
async function newPage() { | ||
const newPage = await context.newPage(); | ||
|
||
// eslint-disable-next-line n/prefer-global/process | ||
if (process.env.COVERAGE) { | ||
await newPage.coverage.startJSCoverage(); | ||
} | ||
|
||
pages.push(newPage); | ||
return newPage; | ||
} | ||
|
||
const coverage = await page.coverage.stopJSCoverage(); | ||
|
||
// Absolute path to src folder "/Users/.../slidr/src" | ||
const srcPath = new URL('..', import.meta.url); | ||
|
||
const coverageEntries = await Promise.all( | ||
coverage | ||
.filter((entry) => { | ||
const coveragePath = new URL( | ||
`../..${new URL(entry.url).pathname}`, | ||
import.meta.url, | ||
); | ||
|
||
// Determine if the file is in the src folder | ||
const isInSrcFolder = coveragePath.pathname.startsWith( | ||
srcPath.pathname, | ||
); | ||
// Determine of the file is a CSS file | ||
const isCssFile = coveragePath.pathname.endsWith('.css'); | ||
|
||
return isInSrcFolder && !isCssFile; | ||
}) | ||
.map(async (entry) => { | ||
const coveragePath = new URL( | ||
`../..${new URL(entry.url).pathname}`, | ||
import.meta.url, | ||
); | ||
|
||
const converter = v8toIstanbul(coveragePath.pathname, 0, { | ||
source: entry.source!, | ||
}); | ||
|
||
await converter.load(); | ||
converter.applyCoverage(entry.functions); | ||
|
||
return converter.toIstanbul(); | ||
}), | ||
); | ||
await use({newPage}); | ||
|
||
let report: Record<string, unknown> = {}; | ||
for (const entry of coverageEntries) { | ||
report = {...report, ...entry}; | ||
// eslint-disable-next-line n/prefer-global/process | ||
if (!process.env.COVERAGE) { | ||
return; | ||
} | ||
|
||
fs.mkdirSync('coverage/tmp', {recursive: true}); | ||
fs.writeFileSync( | ||
`coverage/tmp/${testInfo.testId}.json`, | ||
JSON.stringify(report, null, 2), | ||
await Promise.all( | ||
pages.map(async (pageToCover, index) => | ||
saveCoverage(pageToCover, `${testInfo.testId}_${index}`), | ||
), | ||
); | ||
}, | ||
}); | ||
|
||
async function saveCoverage(page: Page, testId: string) { | ||
const coverage = await page.coverage.stopJSCoverage(); | ||
|
||
// Absolute path to src folder "/Users/.../slidr/src" | ||
const srcPath = new URL('..', import.meta.url); | ||
|
||
const coverageEntries = await Promise.all( | ||
coverage | ||
.filter((entry) => { | ||
const coveragePath = new URL( | ||
`../..${new URL(entry.url).pathname}`, | ||
import.meta.url, | ||
); | ||
|
||
// Determine if the file is in the src folder | ||
const isInSrcFolder = coveragePath.pathname.startsWith( | ||
srcPath.pathname, | ||
); | ||
// Determine of the file is a CSS file | ||
const isCssFile = coveragePath.pathname.endsWith('.css'); | ||
|
||
return isInSrcFolder && !isCssFile; | ||
}) | ||
.map(async (entry) => { | ||
const coveragePath = new URL( | ||
`../..${new URL(entry.url).pathname}`, | ||
import.meta.url, | ||
); | ||
|
||
const converter = v8toIstanbul(coveragePath.pathname, 0, { | ||
source: entry.source!, | ||
}); | ||
|
||
await converter.load(); | ||
converter.applyCoverage(entry.functions); | ||
|
||
return converter.toIstanbul(); | ||
}), | ||
); | ||
|
||
let report: Record<string, unknown> = {}; | ||
for (const entry of coverageEntries) { | ||
report = {...report, ...entry}; | ||
} | ||
|
||
fs.mkdirSync('coverage/tmp', {recursive: true}); | ||
fs.writeFileSync( | ||
`coverage/tmp/${testId}.json`, | ||
JSON.stringify(report, null, 2), | ||
); | ||
} | ||
|
||
export {expect} from '@playwright/test'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters