Playwright Istanbul Coverage, using both baseFixture and adminFixture (so two Fixture files)

41 views Asked by At

I am attempting to get Istanbul Code Coverage working for Playwright E2E test cases.

Advice is to implement a base Fixture:

import * as fs from 'fs';
import * as path from 'path';
import * as crypto from 'crypto';
import { test as baseTest } from '@playwright/test';

const istanbulCLIOutput = path.join(process.cwd(), '.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__)))
      await page.close();
    }
  }
});

export const expect = test.expect;

The test already uses another Fixture (adminFixture.ts) which contains beforeAll.

I have tried to:

  1. add the above code into beforeAll
  2. add the above code directly into a E2E test
  3. 'chaining' the fixture files together (adminFixture.ts > baseFixture.ts > HistoryTMZDownload.spec.ts)

But have experienced errors each time (which I can provide if needed)

Any ideas what I might be able to do here?

0

There are 0 answers