Skip to content

Generating code coverage on Azure pipelines #19

@tchanias

Description

@tchanias

System info

● Playwright Version: Latest
● Operating System: Win 11
● Browser: MS Edge

Steps
npm i && npx playwright install --with-deps
npm i vite-plugin-istanbul
npm i nyc
npx playwright test --project "e2e tests on pipeline"
npx nyc report --reporter=text

Configs:
vite.config.ts

image

baseFixtures.ts

`import * as fs from 'fs'
import * as path from 'path'
import * as crypto from 'crypto'
import { fileURLToPath } from 'url'
import { test as baseTest } from '@playwright/test'

const filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(filename)
const istanbulCLIOutput = path.join(__dirname, '..', '.nyc_output')

export function generateUUID(): string {
return crypto.randomBytes(16).toString('hex')
}

export const test = baseTest.extend({
context: async ({ context }, use) => {
await context.addInitScript(() =>
window.addEventListener('beforeunload', () =>
(window as any).collectIstanbulCoverage(JSON.stringify((window as any).coverage)),
),
)
await fs.promises.mkdir(istanbulCLIOutput, { recursive: true })
await context.exposeFunction('collectIstanbulCoverage', (coverageJSON: string) => {
if (coverageJSON)
fs.writeFileSync(path.join(istanbulCLIOutput, playwright_coverage_${generateUUID()}.json), coverageJSON)
})
await use(context)
for (const page of context.pages()) {
await page.evaluate(() => (window as any).collectIstanbulCoverage(JSON.stringify((window as any).coverage)))
}
},
})

export const expect = test.expect`

Expected
When running the tests locally, the coverage json files are generated under the nyc_report folder as expected and the report can be displayed normally either in text or html form.

On the azure pipeline environment, all tests are still passing, the nyc_folder is created but there are no json files created under it.
Thus, this is the output I get from the npx nyc report --reporter=text command.

image

I am not really sure what I have done incorrectly or whether this tool was never supposed to be able to work on azure pipelines at all though, Some input or tips towards the right direction would be greatly appreciated. Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions