AFFiNE/tests/kit/playwright.ts

64 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-04-28 09:40:44 +03:00
import { ok } from 'node:assert';
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';
2023-04-28 09:40:44 +03:00
import path, { resolve } from 'node:path';
2023-02-05 13:41:18 +03:00
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
2023-04-28 09:40:44 +03:00
export const rootDir = resolve(__dirname, '..', '..');
// assert that the rootDir is the root of the project
// eslint-disable-next-line @typescript-eslint/no-var-requires
ok(require(resolve(rootDir, 'package.json')).name.toLowerCase() === 'affine');
export const testResultDir = resolve(rootDir, 'test-results');
export const istanbulTempDir = process.env.ISTANBUL_TEMP_DIR
2023-02-05 13:41:18 +03:00
? path.resolve(process.env.ISTANBUL_TEMP_DIR)
: path.join(rootDir, '.nyc_output');
2023-02-05 13:41:18 +03:00
function generateUUID() {
return crypto.randomUUID();
}
export const enableCoverage = !!process.env.CI || !!process.env.COVERAGE;
2023-02-05 13:41:18 +03:00
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
);
}
);
}
2023-02-05 13:41:18 +03:00
await use(context);
2023-02-05 13:41:18 +03:00
if (enableCoverage) {
for (const page of context.pages()) {
await page.evaluate(() =>
// @ts-expect-error
window.collectIstanbulCoverage(JSON.stringify(window.__coverage__))
);
}
}
},
});