diff --git a/packages/e2e-test-utils/package.json b/packages/e2e-test-utils/package.json index 7f6b180f43..ee18258768 100644 --- a/packages/e2e-test-utils/package.json +++ b/packages/e2e-test-utils/package.json @@ -6,9 +6,21 @@ "types": "src/index.ts", "license": "Apache-2.0", "publishConfig": { - "access": "public", - "main": "dist/index.cjs.js", - "types": "dist/index.d.ts" + "access": "public" + }, + "exports": { + "./playwright": "./src/playwright/index.ts", + "./package.json": "./package.json" + }, + "typesVersions": { + "*": { + "playwright": [ + "src/playwright/index.ts" + ], + "package.json": [ + "package.json" + ] + } }, "backstage": { "role": "node-library" @@ -22,8 +34,15 @@ "prepack": "backstage-cli package prepack", "postpack": "backstage-cli package postpack" }, + "dependencies": { + "@manypkg/get-packages": "^1.1.3", + "@playwright/test": "^1.32.3", + "fs-extra": "^10.1.0" + }, "devDependencies": { - "@backstage/cli": "workspace:^" + "@backstage/cli": "workspace:^", + "@backstage/cli-node": "workspace:^", + "@types/fs-extra": "^9.0.1" }, "files": [ "dist" diff --git a/packages/e2e-test-utils/src/playwright/failOnBrowserErrors.ts b/packages/e2e-test-utils/src/playwright/failOnBrowserErrors.ts new file mode 100644 index 0000000000..af7baa9540 --- /dev/null +++ b/packages/e2e-test-utils/src/playwright/failOnBrowserErrors.ts @@ -0,0 +1,37 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { test } from '@playwright/test'; + +/** + * Calling this at the top of your test file will ensure that tests fail if the browser + * logs any errors or if there are any uncaught exceptions. + * + * @public + */ +export function failOnBrowserErrors(): void { + test.beforeEach(async ({ page }) => { + page.on('pageerror', error => { + throw new Error(`Uncaught exception on page, ${error}`); + }); + + page.on('console', msg => { + if (msg.type() === 'error') { + throw new Error(`Unexpected console error message "${msg.text()}"`); + } + }); + }); +} diff --git a/packages/e2e-test-utils/src/playwright/generateProjects.ts b/packages/e2e-test-utils/src/playwright/generateProjects.ts new file mode 100644 index 0000000000..b6f3d159c5 --- /dev/null +++ b/packages/e2e-test-utils/src/playwright/generateProjects.ts @@ -0,0 +1,42 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import fs from 'fs-extra'; +import { resolve as resolvePath } from 'path'; +import { PlaywrightTestConfig } from '@playwright/test'; +import { getPackagesSync } from '@manypkg/get-packages'; +import type { BackstagePackage } from '@backstage/cli-node'; + +/** + * Generates a list of playwright projects by scanning the monorepo for packages with an `e2e-tests/` folder. + * + * @public + */ +export function generateProjects(): PlaywrightTestConfig['projects'] { + // TODO(Rugvip): Switch this over to use @backstage/cli-node once released, and support SINCE=origin/main + const { root, packages } = getPackagesSync(process.cwd()); + const e2eTestPackages = [...(root ? [root] : []), ...packages].filter(pkg => { + return fs.pathExistsSync(resolvePath(pkg.dir, 'e2e-tests')); + }) as BackstagePackage[]; + + return e2eTestPackages.map(pkg => ({ + name: pkg.packageJson.name, + testDir: resolvePath(pkg.dir, 'e2e-tests'), + use: { + channel: 'chrome', + }, + })); +} diff --git a/packages/e2e-test-utils/src/playwright/index.ts b/packages/e2e-test-utils/src/playwright/index.ts new file mode 100644 index 0000000000..c5750581d7 --- /dev/null +++ b/packages/e2e-test-utils/src/playwright/index.ts @@ -0,0 +1,18 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export { generateProjects } from './generateProjects'; +export { failOnBrowserErrors } from './failOnBrowserErrors'; diff --git a/yarn.lock b/yarn.lock index 04a2950f3f..c70add6790 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4185,6 +4185,11 @@ __metadata: resolution: "@backstage/e2e-test-utils@workspace:packages/e2e-test-utils" dependencies: "@backstage/cli": "workspace:^" + "@backstage/cli-node": "workspace:^" + "@manypkg/get-packages": ^1.1.3 + "@playwright/test": ^1.32.3 + "@types/fs-extra": ^9.0.1 + fs-extra: ^10.1.0 languageName: unknown linkType: soft @@ -14218,6 +14223,17 @@ __metadata: languageName: node linkType: hard +"@playwright/test@npm:^1.32.3": + version: 1.38.1 + resolution: "@playwright/test@npm:1.38.1" + dependencies: + playwright: 1.38.1 + bin: + playwright: cli.js + checksum: c5ec0b23261fe1ef163b6234f69263bc10e7e5a3fb676c7773ffc70b87459a7ab225f57c03b9de649475771638a04c2e00d9b2739304a4dcf5d3edf20a7a4a82 + languageName: node + linkType: hard + "@pmmmwh/react-refresh-webpack-plugin@npm:^0.5.7": version: 0.5.11 resolution: "@pmmmwh/react-refresh-webpack-plugin@npm:0.5.11" @@ -26752,7 +26768,7 @@ __metadata: languageName: node linkType: hard -"fs-extra@npm:10.1.0, fs-extra@npm:^10.0.0, fs-extra@npm:^10.0.1": +"fs-extra@npm:10.1.0, fs-extra@npm:^10.0.0, fs-extra@npm:^10.0.1, fs-extra@npm:^10.1.0": version: 10.1.0 resolution: "fs-extra@npm:10.1.0" dependencies: @@ -26829,7 +26845,7 @@ __metadata: languageName: node linkType: hard -"fsevents@npm:^2.3.2, fsevents@npm:~2.3.2": +"fsevents@npm:2.3.2, fsevents@npm:^2.3.2, fsevents@npm:~2.3.2": version: 2.3.2 resolution: "fsevents@npm:2.3.2" dependencies: @@ -26839,7 +26855,7 @@ __metadata: languageName: node linkType: hard -"fsevents@patch:fsevents@^2.3.2#~builtin, fsevents@patch:fsevents@~2.3.2#~builtin": +"fsevents@patch:fsevents@2.3.2#~builtin, fsevents@patch:fsevents@^2.3.2#~builtin, fsevents@patch:fsevents@~2.3.2#~builtin": version: 2.3.2 resolution: "fsevents@patch:fsevents@npm%3A2.3.2#~builtin::version=2.3.2&hash=18f3a7" dependencies: @@ -35546,6 +35562,30 @@ __metadata: languageName: node linkType: hard +"playwright-core@npm:1.38.1": + version: 1.38.1 + resolution: "playwright-core@npm:1.38.1" + bin: + playwright-core: cli.js + checksum: 66e83fe040f309b13ad94ba39dea40ac207bfcbbc22de13141af88dbdedd64e1c4e3ce1d0cb070d4efd8050d7e579953ec3681dd8a0acf2c1cc738d9c50e545e + languageName: node + linkType: hard + +"playwright@npm:1.38.1": + version: 1.38.1 + resolution: "playwright@npm:1.38.1" + dependencies: + fsevents: 2.3.2 + playwright-core: 1.38.1 + dependenciesMeta: + fsevents: + optional: true + bin: + playwright: cli.js + checksum: 4e01d4ee52d9ccf75a80d8492829106802590721d56bff7c5957ff1f21eb3c328ee5bc3c1784a59c4b515df1b98d08ef92e4a35a807f454cd00dc481d30fadc2 + languageName: node + linkType: hard + "pluralize@npm:^8.0.0": version: 8.0.0 resolution: "pluralize@npm:8.0.0"