Skip to content

Commit

Permalink
fix missing coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
codyzu committed Feb 11, 2024
1 parent 4f25052 commit 6d3d86a
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 77 deletions.
16 changes: 8 additions & 8 deletions src/pages/audience.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down Expand Up @@ -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}`,
Expand Down
16 changes: 8 additions & 8 deletions src/pages/presentation.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down Expand Up @@ -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}`,
Expand Down
16 changes: 8 additions & 8 deletions src/pages/speaker.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down Expand Up @@ -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`);
Expand Down
126 changes: 74 additions & 52 deletions src/test/coverage-fixture.ts
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';
2 changes: 1 addition & 1 deletion src/test/login-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand Down

0 comments on commit 6d3d86a

Please sign in to comment.