diff --git a/src/pages/audience.e2e.ts b/src/pages/audience.e2e.ts index 0a39cd8..b17db7e 100644 --- a/src/pages/audience.e2e.ts +++ b/src/pages/audience.e2e.ts @@ -13,10 +13,10 @@ test.beforeAll(async () => { presentationId = presentationSnapshot.id; }); -test('synchronizes slide from presentation', async ({context}, testInfo) => { - const presentationPage = await context.newPage(); - const speakerPage = await context.newPage(); - const audienceView = await context.newPage(); +test('synchronizes slide from presentation', async ({coverage}, testInfo) => { + const presentationPage = await coverage.newPage(); + const speakerPage = await coverage.newPage(); + const audienceView = await coverage.newPage(); await presentationPage.goto( `/p/${presentationId}?session=${testInfo.testId}`, @@ -48,11 +48,11 @@ test('synchronizes slide from presentation', async ({context}, testInfo) => { ).toBeVisible(); }); -test('can add reactions', async ({context}, testInfo) => { +test('can add reactions', async ({coverage}, testInfo) => { test.setTimeout(10_000); - const presentationPage = await context.newPage(); - const speakerPage = await context.newPage(); - const audienceView = await context.newPage(); + const presentationPage = await coverage.newPage(); + const speakerPage = await coverage.newPage(); + const audienceView = await coverage.newPage(); await presentationPage.goto( `/p/${presentationId}?session=${testInfo.testId}`, diff --git a/src/pages/presentation.e2e.ts b/src/pages/presentation.e2e.ts index 43da9ea..033fc2c 100644 --- a/src/pages/presentation.e2e.ts +++ b/src/pages/presentation.e2e.ts @@ -14,11 +14,11 @@ test.beforeAll(async () => { }); test('can navigate the presentation with the toolbar', async ({ - context, + coverage, }, testInfo) => { - const presentationPage = await context.newPage(); - const speakerPage = await context.newPage(); - const audienceView = await context.newPage(); + const presentationPage = await coverage.newPage(); + const speakerPage = await coverage.newPage(); + const audienceView = await coverage.newPage(); await presentationPage.goto( `/p/${presentationId}?session=${testInfo.testId}`, @@ -91,10 +91,10 @@ test('can navigate the presentation with the toolbar', async ({ ).toBeVisible(); }); -test('can add and clear reactions', async ({context}, testInfo) => { - const presentationPage = await context.newPage(); - const speakerPage = await context.newPage(); - const audienceView = await context.newPage(); +test('can add and clear reactions', async ({coverage}, testInfo) => { + const presentationPage = await coverage.newPage(); + const speakerPage = await coverage.newPage(); + const audienceView = await coverage.newPage(); await presentationPage.goto( `/p/${presentationId}?session=${testInfo.testId}`, diff --git a/src/pages/speaker.e2e.ts b/src/pages/speaker.e2e.ts index 8717ad7..137d198 100644 --- a/src/pages/speaker.e2e.ts +++ b/src/pages/speaker.e2e.ts @@ -14,11 +14,11 @@ test.beforeAll(async () => { }); test('can change the slide of the presentation', async ({ - context, + coverage, }, testInfo) => { - const presentationPage = await context.newPage(); - const speakerPage = await context.newPage(); - const audienceView = await context.newPage(); + const presentationPage = await coverage.newPage(); + const speakerPage = await coverage.newPage(); + const audienceView = await coverage.newPage(); await presentationPage.goto( `/p/${presentationId}?session=${testInfo.testId}`, @@ -50,10 +50,10 @@ test('can change the slide of the presentation', async ({ ).toBeVisible(); }); -test('can add and clear reactions', async ({context}) => { - const presentationPage = await context.newPage(); - const speakerPage = await context.newPage(); - const audienceView = await context.newPage(); +test('can add and clear reactions', async ({coverage}) => { + const presentationPage = await coverage.newPage(); + const speakerPage = await coverage.newPage(); + const audienceView = await coverage.newPage(); await presentationPage.goto(`/p/${presentationId}?session=speakertest`); await speakerPage.goto(`/s/${presentationId}?session=speakertest`); diff --git a/src/test/coverage-fixture.ts b/src/test/coverage-fixture.ts index 6e0d73f..5a51996 100644 --- a/src/test/coverage-fixture.ts +++ b/src/test/coverage-fixture.ts @@ -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}; }; export const test = base.extend({ - 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 = {}; - 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 = {}; + 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'; diff --git a/src/test/login-fixture.ts b/src/test/login-fixture.ts index 5f651eb..336cf64 100644 --- a/src/test/login-fixture.ts +++ b/src/test/login-fixture.ts @@ -3,7 +3,7 @@ import {loginPageFactory, type LoginPage} from './login-page'; export const test = base.extend<{loginPage: LoginPage}>({ // @ts-expect-error the fixture has to be declared for it to be used by playwright - async loginPage({page, _coveredPage}, use) { + async loginPage({page, coverage}, use) { const loginPage = loginPageFactory(page); await use(loginPage); },