e2e Code Coverage and all test fixes (#5328)
Add e2e code coverage and fix all tests
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
/* eslint-disable no-undef */
|
||||
/* This file extends the base functionality of the playwright test framework to enable
|
||||
* code coverage instrumentation, console log error detection and working with a 3rd
|
||||
* party Chrome-as-a-service extension called Browserless.
|
||||
*/
|
||||
|
||||
// This file extends the base functionality of the playwright test framework
|
||||
const base = require('@playwright/test');
|
||||
const { expect } = require('@playwright/test');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { v4: uuid } = require('uuid');
|
||||
|
||||
/**
|
||||
* Takes a `ConsoleMessage` and returns a formatted string
|
||||
@@ -16,7 +21,30 @@ function consoleMessageToString(msg) {
|
||||
at (${url} ${lineNumber}:${columnNumber})`;
|
||||
}
|
||||
|
||||
//The following is based on https://github.com/mxschmitt/playwright-test-coverage
|
||||
// eslint-disable-next-line no-undef
|
||||
const istanbulCLIOutput = path.join(process.cwd(), '.nyc_output');
|
||||
|
||||
// eslint-disable-next-line no-undef
|
||||
exports.test = base.test.extend({
|
||||
//The following is based on https://github.com/mxschmitt/playwright-test-coverage
|
||||
context: async ({ context }, use) => {
|
||||
await context.addInitScript(() =>
|
||||
window.addEventListener('beforeunload', () =>
|
||||
(window).collectIstanbulCoverage(JSON.stringify((window).__coverage__))
|
||||
)
|
||||
);
|
||||
await fs.promises.mkdir(istanbulCLIOutput, { recursive: true });
|
||||
await context.exposeFunction('collectIstanbulCoverage', (coverageJSON) => {
|
||||
if (coverageJSON) {
|
||||
fs.writeFileSync(path.join(istanbulCLIOutput, `playwright_coverage_${uuid()}.json`), coverageJSON);
|
||||
}
|
||||
});
|
||||
await use(context);
|
||||
for (const page of context.pages()) {
|
||||
await page.evaluate(() => (window).collectIstanbulCoverage(JSON.stringify((window).__coverage__)));
|
||||
}
|
||||
},
|
||||
page: async ({ baseURL, page }, use) => {
|
||||
const messages = [];
|
||||
page.on('console', (msg) => messages.push(msg));
|
||||
|
||||
Reference in New Issue
Block a user