cenfun / monocart-coverage-reports

A code coverage tool to generate native V8 reports or Istanbul reports.
MIT License
69 stars 6 forks source link

[Question] Building coverage for multiple apps within one playwright run #62

Closed cedric-talbot closed 3 months ago

cedric-talbot commented 3 months ago

Hello,

I currently have the following repository architecture for 3 separate applications.

apps
└───app1
└───app2
└───app3

These 3 applications are launched separately, meaning the build files are not aware of the application they belong to. I need to test how the applications interact with each other, meaning I need to have playwright test suites that will go through all 3 applications, and I can't have separate playwright runs. However, I need the final coverage results to have the correct file structure, with the files labeled depending on their application (for example I need the index.tsx file in app1 to have apps/app1/index.tsx as its url in my json report).

I tried rewriting the url from the coverage data returned by playwright's stopJsCoverage, however the coverage returned by MCR has all files prefixed by _N_E and has lost the apps/appX information I added.

const jsCoverage = await page.coverage.stopJSCoverage();
const updatedCoverage = jsCoverage
    .map((item) => ({
    ...item,
    url: item.url
        .replace('(app-pages-browser)/xxx', 'apps/appX/xxx'),
    }));
const mcr = MCR(coverageOptions);
await mcr.add(updatedCoverage);

I dug a bit and noticed that it's coming from the unpackDistFile function that adds an originalList argument that is then used while ignoring the data initially provided. Do you have any idea if there's a way to avoid this behaviour ? If not, do you know of a way to solve my issue otherwise ?

mcr.config.ts

import { CoverageReportOptions } from 'monocart-coverage-reports';

const coverageOptions: CoverageReportOptions = {
    name: 'My Playwright Coverage Report',
    reports: [
        ['json', { file: 'playwright-coverage.json' }],
    ],
    sourceFilter: {
        '**/node_modules/**': false,
    },
    outputDir: './coverage/playwright',
};

export default coverageOptions;

playwright.config.ts, global-teardown.ts & global.setup.ts were build following https://github.com/cenfun/playwright-coverage

cenfun commented 3 months ago

You may try option sourcePath to normalize the full path of the source file:

const path = require("path")

const coverageOptions = {
    sourcePath: (filePath, info)=> {
        if (info.distFile) {
            return `${path.dirname(info.distFile)}/${filePath}`;
        }
        return filePath;
    }
}
cedric-talbot commented 3 months ago

Thanks a lot ! I had tried playing with sourcePath but did not notice I could get the distFile here.