github.com/minio/console@v1.4.1/web-app/e2e/fixtures/baseFixture.ts (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2023 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  import * as fs from "fs";
    17  import * as path from "path";
    18  import * as crypto from "crypto";
    19  import { test as baseTest } from "@playwright/test";
    20  
    21  const istanbulCLIOutput = path.join(process.cwd(), ".nyc_output");
    22  
    23  export function generateUUID(): string {
    24    return crypto.randomBytes(16).toString("hex");
    25  }
    26  
    27  export const test = baseTest.extend({
    28    context: async ({ context }, use) => {
    29      await context.addInitScript(() =>
    30        window.addEventListener("beforeunload", () =>
    31          (window as any).collectIstanbulCoverage(
    32            JSON.stringify((window as any).__coverage__),
    33          ),
    34        ),
    35      );
    36      await fs.promises.mkdir(istanbulCLIOutput, { recursive: true });
    37      await context.exposeFunction(
    38        "collectIstanbulCoverage",
    39        (coverageJSON: string) => {
    40          if (coverageJSON)
    41            fs.writeFileSync(
    42              path.join(
    43                istanbulCLIOutput,
    44                `playwright_coverage_${generateUUID()}.json`,
    45              ),
    46              coverageJSON,
    47            );
    48        },
    49      );
    50      await use(context);
    51      for (const page of context.pages()) {
    52        await page.evaluate(() =>
    53          (window as any).collectIstanbulCoverage(
    54            JSON.stringify((window as any).__coverage__),
    55          ),
    56        );
    57      }
    58    },
    59  });
    60  
    61  export const expect = test.expect;