@web/test-runner-chrome
Version:
Chrome browser launcher for Web Test Runner
76 lines • 3.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChromeLauncherPage = void 0;
const test_runner_coverage_v8_1 = require("@web/test-runner-coverage-v8");
class ChromeLauncherPage {
config;
testFiles;
browser;
puppeteerPage;
nativeInstrumentationEnabledOnPage = false;
patchAdded = false;
resolvers = {};
constructor(config, testFiles, product, puppeteerPage) {
this.config = config;
this.testFiles = testFiles;
this.browser = product;
this.puppeteerPage = puppeteerPage;
}
async runSession(url, coverage) {
if (coverage &&
this.config.coverageConfig?.nativeInstrumentation !== false &&
this.browser === 'chromium') {
if (this.nativeInstrumentationEnabledOnPage) {
await this.puppeteerPage.coverage.stopJSCoverage();
}
this.nativeInstrumentationEnabledOnPage = true;
await this.puppeteerPage.coverage.startJSCoverage({
includeRawScriptCoverage: true,
});
}
await this.puppeteerPage.setViewport({ height: 600, width: 800 });
await this.puppeteerPage.goto(url);
}
async stopSession() {
const testCoverage = await this.collectTestCoverage(this.config, this.testFiles);
// navigate to an empty page to kill any running code on the page, stopping timers and
// breaking a potential endless reload loop
await this.puppeteerPage.goto('about:blank');
return { testCoverage };
}
async collectTestCoverage(config, testFiles) {
const userAgentPromise = this.puppeteerPage
.browser()
.userAgent()
.catch(() => undefined);
try {
const coverageFromBrowser = await this.puppeteerPage.evaluate(() => window.__coverage__);
if (coverageFromBrowser) {
// coverage was generated by JS, return that
return coverageFromBrowser;
}
}
catch {
// evaluate throws when the test navigates in the browser
}
if (config.coverageConfig?.nativeInstrumentation === false) {
throw new Error('Coverage is enabled with nativeInstrumentation disabled. ' +
'Expected coverage provided in the browser as a global __coverage__ variable.' +
'Use a plugin like babel-plugin-istanbul to generate the coverage, or enable native instrumentation.');
}
if (!this.nativeInstrumentationEnabledOnPage) {
return undefined;
}
const [userAgent, coverageResult] = await Promise.all([
userAgentPromise,
this.puppeteerPage.coverage?.stopJSCoverage(),
]);
const v8Coverage = coverageResult
?.map(entry => entry.rawScriptCoverage)
.filter((cov) => cov !== undefined);
this.nativeInstrumentationEnabledOnPage = false;
return (0, test_runner_coverage_v8_1.v8ToIstanbul)(config, testFiles, v8Coverage, userAgent);
}
}
exports.ChromeLauncherPage = ChromeLauncherPage;
//# sourceMappingURL=ChromeLauncherPage.js.map