Skip to content

Commit

Permalink
chore: add coverage for playwright tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DipperTheDan committed Jan 20, 2025
1 parent fcf8d1b commit 01d2f32
Show file tree
Hide file tree
Showing 97 changed files with 837 additions and 234 deletions.
2 changes: 1 addition & 1 deletion contributing/testing-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ A typical `*.pw.tsx` file may look like the following:

```tsx
// inside src/components/button/button.pw.tsx
import { test, expect } from "@playwright/experimental-ct-react17";
import { test, expect } from "../../__spec_helper__/base-test";
import Button from "./button.component";
import { buttonComponent } from "../../../playwright/component/button/index";

Expand Down
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const config: Config = {
"lib",
"esm",
],
coverageReporters: ["text-summary", "html"],
coverageReporters: ["text-summary", "html", "json"],
coverageDirectory: "<rootDir>/coverage",
coverageThreshold: isCI ? undefined : coverageThresholds,
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "mjs"],
Expand Down
771 changes: 634 additions & 137 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"lib",
"esm",
"scripts/check_rfcs/check_rfcs.js",
"scripts/check_rfcs/index.js"
"scripts/check_rfcs/index.js",
"scripts/copy_coverage_files/index.js"
],
"scripts": {
"start": "node ./scripts/check_node_version.mjs && dotenvx run -- storybook dev -p 9001 -c .storybook",
Expand Down Expand Up @@ -35,7 +36,10 @@
"test:ct": "playwright test -c playwright-ct.config.ts",
"test:ct:ui": "playwright test --ui -c playwright-ct.config.ts",
"test:ct:report": "npx playwright show-report",
"clear-playwright-cache": "rimraf ./playwright/.cache"
"clear-playwright-cache": "rimraf ./playwright/.cache",
"clear-playwright-coverage-json": "rimraf ./playwright/coverage/*.json",
"test:ct:coverage": "npm run clear-playwright-cache && npm run clear-playwright-coverage-json && rimraf ./playwright/coverage && playwright test -c playwright-ct.config.ts",
"pw:coverage:report": "nyc report --reporter=html --report-dir=./playwright/coverage --temp-dir=./playwright/coverage"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -191,6 +195,8 @@
"immutable": "~3.8.2",
"invariant": "^2.2.4",
"lodash": "^4.17.21",
"monocart-coverage-reports": "^2.11.4",
"nyc": "^17.1.0",
"polished": "^4.2.2",
"prop-types": "^15.8.1",
"react-day-picker": "^9.3.2",
Expand Down Expand Up @@ -243,4 +249,4 @@
"@semantic-release/github"
]
}
}
}
59 changes: 59 additions & 0 deletions playwright/helpers/base-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { test as ctBase, expect } from "@playwright/experimental-ct-react17";
import mcr from "monocart-coverage-reports";

const coverageOptions = {
reports: [
[
"json",
{
file: "coverage.json",
},
],
],
lcov: true,
name: "My Istanbul Report",
all: {
dir: ["./src"],
filter: {
"**/*.component.tsx": true,
"**/*.style.ts": true,
},
},
outputDir: "./playwright/coverage",
};

const test = ctBase.extend({
autoTestFixture: [
async ({ page }, use) => {
// coverage API is chromium only
if (test.info().project.name === "chromium") {
await Promise.all([
page.coverage.startJSCoverage({
resetOnNavigation: false,
}),
page.coverage.startCSSCoverage({
resetOnNavigation: false,
}),
]);
}

await use("autoTestFixture");

if (test.info().project.name === "chromium") {
const [jsCoverage, cssCoverage] = await Promise.all([
page.coverage.stopJSCoverage(),
page.coverage.stopCSSCoverage(),
]);
const coverageList = [...jsCoverage, ...cssCoverage];
await mcr(coverageOptions).add(coverageList);
await mcr(coverageOptions).generate();
}
},
{
scope: "test",
auto: true,
},
],
});

export { test, expect };
41 changes: 41 additions & 0 deletions scripts/copy_coverage_files/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const fs = require("fs");
const path = require("path");

const destinationDir = "./merged-coverage";

function copyJSONFile(dir) {
// Ensure the destination directory exists
if (!fs.existsSync(destinationDir)) {
fs.mkdirSync(destinationDir, { recursive: true });
}
// Read files in the source directory
fs.readdir(dir, (error, files) => {
if (error) {
console.error("Error reading source directory:", error);
return;
}

const jsonFiles = files.filter(
(file) => path.extname(file).toLowerCase() === ".json",
);

// Copy JSON files to destination directory
jsonFiles.forEach((file) => {
fs.copyFile(
path.join(dir, file),
path.join(destinationDir, file),
fs.constants.COPYFILE_FICLONE, // Optional flags (use fs.constants.COPYFILE_EXCL to prevent overwriting)
(err) => {
if (err) {
console.error(`Error copying file ${file}:`, err);
} else {
console.log(`Copied file ${file} to ${destinationDir}`);
}
},
);
});
});
}

copyJSONFile("./playwright/coverage");
copyJSONFile("./coverage");
2 changes: 1 addition & 1 deletion src/components/accordion/accordion.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";
import { test, expect } from "../../../playwright/helpers/base-test";
import {
accordion,
accordionIcon,
Expand Down
2 changes: 1 addition & 1 deletion src/components/action-popover/action-popover.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-await-in-loop */
import { expect, test } from "@playwright/experimental-ct-react17";
import React from "react";
import { test, expect } from "../../../playwright/helpers/base-test";
import { accordionDefaultTitle } from "../../../playwright/components/accordion";
import {
actionPopover,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";
import { test, expect } from "../../../playwright/helpers/base-test";
import { AdvancedColorPickerCustom } from "./components.test-pw";
import {
currentColorDescription,
Expand Down
2 changes: 1 addition & 1 deletion src/components/alert/alert.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";
import { test, expect } from "../../../playwright/helpers/base-test";
import {
alertTitle,
alertSubtitle,
Expand Down
2 changes: 1 addition & 1 deletion src/components/anchor-navigation/anchor-navigation.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";
import { test, expect } from "../../../playwright/helpers/base-test";

import {
AnchorNavigationComponent,
Expand Down
2 changes: 1 addition & 1 deletion src/components/badge/badge.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";
import { test, expect } from "../../../playwright/helpers/base-test";
import {
badge,
badgeCounter,
Expand Down
2 changes: 1 addition & 1 deletion src/components/batch-selection/batch-selection.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";
import { test, expect } from "../../../playwright/helpers/base-test";
import { BatchSelectionProps } from "../../../src/components/batch-selection";
import {
BatchSelectionComponent,
Expand Down
2 changes: 1 addition & 1 deletion src/components/box/box.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "@playwright/experimental-ct-react17";
import React from "react";
import { test, expect } from "../../../playwright/helpers/base-test";
import { getDataElementByValue } from "../../../playwright/components";
import {
assertCssValueIsApproximately,
Expand Down
2 changes: 1 addition & 1 deletion src/components/breadcrumbs/breadcrumbs.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";
import { test, expect } from "../../../playwright/helpers/base-test";
import {
breadcrumbsComponent,
allCrumbs,
Expand Down
2 changes: 1 addition & 1 deletion src/components/button-bar/button-bar.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";
import { test, expect } from "../../../playwright/helpers/base-test";
import {
Default as ButtonBarCustom,
DefaultWithWrapper as ButtonBarWithWrapper,
Expand Down
2 changes: 1 addition & 1 deletion src/components/button-minor/button-minor.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";
import { test, expect } from "../../../playwright/helpers/base-test";
import { ButtonMinorProps } from "./button-minor.component";
import {
Default as ButtonMinor,
Expand Down
2 changes: 1 addition & 1 deletion src/components/button-toggle/button-toggle.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";
import { test, expect } from "../../../playwright/helpers/base-test";
import { ButtonToggle, ButtonToggleGroupProps, ButtonToggleProps } from ".";
import {
ButtonToggleComponent,
Expand Down
2 changes: 1 addition & 1 deletion src/components/button/button.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";
import { test, expect } from "../../../playwright/helpers/base-test";

import {
buttonSubtextPreview,
Expand Down
2 changes: 1 addition & 1 deletion src/components/carbon-provider/carbon-provider.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";
import { test, expect } from "../../../playwright/helpers/base-test";
import { HooksConfig } from "../../../playwright";
import { SageTheme as SageThemeStory, AllThemes } from "./components.test-pw";
import Button from "../../../src/components/button";
Expand Down
2 changes: 1 addition & 1 deletion src/components/card/card.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "@playwright/experimental-ct-react17";
import React from "react";
import { test, expect } from "../../../playwright/helpers/base-test";
import "../../../playwright/components";
import {
card,
Expand Down
2 changes: 1 addition & 1 deletion src/components/checkbox/checkbox.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";
import { CommonCheckableInputProps } from "__internal__/checkable-input/checkable-input.component";
import { test, expect } from "../../../playwright/helpers/base-test";
import Box from "../box";
import { Checkbox, CheckboxProps, CheckboxGroupProps } from ".";
import {
Expand Down
2 changes: 1 addition & 1 deletion src/components/confirm/confirm.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";
import { test, expect } from "../../../playwright/helpers/base-test";
import Confirm, { ConfirmProps } from "./confirm.component";
import {
ConfirmComponent,
Expand Down
2 changes: 1 addition & 1 deletion src/components/content/content.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";
import { test, expect } from "../../../playwright/helpers/base-test";
import ContentComponent from "./components.test-pw";
import { CHARACTERS } from "../../../playwright/support/constants";
import {
Expand Down
2 changes: 1 addition & 1 deletion src/components/date-range/date-range.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "@playwright/experimental-ct-react17";
import React from "react";
import { test, expect } from "../../../playwright/helpers/base-test";
import {
getDataElementByValue,
icon,
Expand Down
2 changes: 1 addition & 1 deletion src/components/date/date.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import { expect, test } from "@playwright/experimental-ct-react17";
import dayjs from "dayjs";
import advancedFormat from "dayjs/plugin/advancedFormat";
import { test, expect } from "../../../playwright/helpers/base-test";
import {
DateInputCustom,
DateInputValidationNewDesign,
Expand Down
2 changes: 1 addition & 1 deletion src/components/decimal/decimal.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import { expect, test } from "@playwright/experimental-ct-react17";
import { Locator } from "@playwright/test";
import { test, expect } from "../../../playwright/helpers/base-test";

import {
commonDataElementInputPreview,
Expand Down
2 changes: 1 addition & 1 deletion src/components/definition-list/definition-list.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";
import { test, expect } from "../../../playwright/helpers/base-test";
import {
DLComponent,
DLReactFragment,
Expand Down
2 changes: 1 addition & 1 deletion src/components/detail/detail.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable react/no-unescaped-entities */
import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";
import { test, expect } from "../../../playwright/helpers/base-test";
import Detail from "./detail.component";
import {
childrenPreview,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import { expect, test } from "@playwright/experimental-ct-react17";
import type { Page } from "@playwright/test";
import { test, expect } from "../../../playwright/helpers/base-test";
import {
DialogFullScreenComponent,
NestedDialog,
Expand Down
2 changes: 1 addition & 1 deletion src/components/dialog/dialog.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { expect, test } from "@playwright/experimental-ct-react17";
import { test, expect } from "../../../playwright/helpers/base-test";

import {
DialogComponent,
Expand Down
4 changes: 2 additions & 2 deletions src/components/dismissible-box/dismissible-box.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";

import { DismissibleBoxProps } from "components/dismissible-box";
import { test, expect } from "../../../playwright/helpers/base-test";

import {
DefaultDismissibleBox,
DefaultDarkVariant,
Expand Down
2 changes: 1 addition & 1 deletion src/components/draggable/draggable.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "@playwright/experimental-ct-react17";
import React from "react";
import { test, expect } from "../../../playwright/helpers/base-test";
import { checkAccessibility } from "../../../playwright/support/helper";

import {
Expand Down
2 changes: 1 addition & 1 deletion src/components/drawer/drawer.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";
import { test, expect } from "../../../playwright/helpers/base-test";
import {
DrawerCustom,
DrawerCustomFooterHeader,
Expand Down
2 changes: 1 addition & 1 deletion src/components/duelling-picklist/duelling-picklist.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";
import { test, expect } from "../../../playwright/helpers/base-test";
import { PicklistItemProps } from "./picklist-item/picklist-item.component";
import {
DuellingPicklistComponent,
Expand Down
2 changes: 1 addition & 1 deletion src/components/fieldset/fieldset.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";
import { test, expect } from "../../../playwright/helpers/base-test";
import Fieldset from ".";
import Form, { FormProps } from "../form";
import FieldsetComponent from "./components.test-pw";
Expand Down
2 changes: 1 addition & 1 deletion src/components/file-input/file-input.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from "react";
import { Page } from "@playwright/test";
import { test, expect } from "@playwright/experimental-ct-react17";
import path from "path";
import { readFileSync } from "fs";
import { test, expect } from "../../../playwright/helpers/base-test";
import FileInputComponent from "./components.test-pw";
import {
hiddenInput,
Expand Down
2 changes: 1 addition & 1 deletion src/components/flat-table/flat-table.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";
import type { Locator } from "@playwright/test";
import { test, expect } from "../../../playwright/helpers/base-test";
import { FlatTableCheckboxProps } from ".";
import {
FlatTableComponent,
Expand Down
Loading

0 comments on commit 01d2f32

Please sign in to comment.