2023-02-17 10:33:32 +03:00
|
|
|
import crypto from 'node:crypto';
|
2023-02-05 13:41:18 +03:00
|
|
|
import fs from 'node:fs';
|
|
|
|
import path from 'node:path';
|
|
|
|
import process from 'node:process';
|
2023-02-17 10:33:32 +03:00
|
|
|
|
|
|
|
import { test as baseTest } from '@playwright/test';
|
2023-02-05 13:41:18 +03:00
|
|
|
|
|
|
|
const istanbulTempDir = process.env.ISTANBUL_TEMP_DIR
|
|
|
|
? path.resolve(process.env.ISTANBUL_TEMP_DIR)
|
|
|
|
: path.join(process.cwd(), '.nyc_output');
|
|
|
|
|
|
|
|
function generateUUID() {
|
|
|
|
return crypto.randomUUID();
|
|
|
|
}
|
|
|
|
|
|
|
|
const enableCoverage = !!process.env.CI || !!process.env.COVERAGE;
|
|
|
|
|
|
|
|
export const test = baseTest.extend({
|
|
|
|
context: async ({ context }, use) => {
|
|
|
|
if (enableCoverage) {
|
|
|
|
await context.addInitScript(() =>
|
|
|
|
window.addEventListener('beforeunload', () =>
|
|
|
|
// @ts-expect-error
|
|
|
|
window.collectIstanbulCoverage(JSON.stringify(window.__coverage__))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
await fs.promises.mkdir(istanbulTempDir, { recursive: true });
|
|
|
|
await context.exposeFunction(
|
|
|
|
'collectIstanbulCoverage',
|
|
|
|
(coverageJSON?: string) => {
|
|
|
|
if (coverageJSON)
|
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(
|
|
|
|
istanbulTempDir,
|
|
|
|
`playwright_coverage_${generateUUID()}.json`
|
|
|
|
),
|
|
|
|
coverageJSON
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
await use(context);
|
|
|
|
if (enableCoverage) {
|
|
|
|
for (const page of context.pages()) {
|
|
|
|
await page.evaluate(() =>
|
|
|
|
// @ts-expect-error
|
|
|
|
window.collectIstanbulCoverage(JSON.stringify(window.__coverage__))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|