Skip to content

Commit 19b9e0f

Browse files
tests: add playwright E2E test (#5)
* feat: add playwright setup * feat: add playwright baseline image tests * feat: add lib build before preview * feat: run tests on macbook * feat: increase pixel ratio
1 parent 58e2b1f commit 19b9e0f

File tree

9 files changed

+454
-9
lines changed

9 files changed

+454
-9
lines changed

.github/workflows/playwright.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Playwright Tests
2+
on:
3+
push:
4+
branches: [main]
5+
pull_request:
6+
branches: [main]
7+
jobs:
8+
test:
9+
timeout-minutes: 60
10+
runs-on: macos-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- uses: actions/setup-node@v4
14+
with:
15+
node-version: lts/*
16+
- name: Install dependencies
17+
run: npm install -g pnpm && pnpm install
18+
- name: Install Playwright Browsers
19+
run: pnpm exec playwright install --with-deps
20+
- name: Run Playwright tests
21+
run: pnpm exec playwright test
22+
- uses: actions/upload-artifact@v4
23+
if: ${{ !cancelled() }}
24+
with:
25+
name: playwright-report
26+
path: playwright-report/
27+
retention-days: 30

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ node_modules
22
dist
33
.DS_Store
44
dump
5-
static
5+
static
6+
/test-results/
7+
/playwright-report/
8+
/blob-report/
9+
/playwright/.cache/

__tests__/example.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// test.spec.ts
2+
import { test, expect, Page } from "@playwright/test";
3+
4+
function sleep(ms: number): Promise<void> {
5+
return new Promise((resolve) => setTimeout(resolve, ms));
6+
}
7+
8+
test.describe.parallel("bsky-widget - Image Snapshot Test", () => {
9+
test("should render the bsky-widget component", async ({
10+
page,
11+
}: {
12+
page: Page;
13+
}) => {
14+
await page.goto("/test.html"); // Update with your test page path
15+
16+
await expect(page).toHaveTitle("E2E Test Page");
17+
18+
// Locate the bsky-widget component
19+
const widgetElement = page.locator('bsky-widget[data-rendered="true"]');
20+
const followers = widgetElement.locator(".followers");
21+
22+
// Check if the widget is visible
23+
await expect(widgetElement).toBeVisible();
24+
25+
await sleep(2000);
26+
await expect(page).toHaveScreenshot("baseline.png", {
27+
mask: [followers],
28+
maxDiffPixelRatio: 0.1,
29+
});
30+
});
31+
});
90.8 KB
Loading
81 KB
Loading

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
"homepage": "https://bsky-widget.saurabhdaware.in/",
99
"scripts": {
1010
"dev": "vite",
11-
"build": "vite build:lib && vite build:playground",
11+
"build": "pnpm build:lib && pnpm build:playground",
1212
"build:lib": "LIB=true vite build",
1313
"build:playground": "vite build",
14-
"test": "echo \"Error: no test specified\" && exit 1",
14+
"preview": "pnpm build:lib && http-server ./ -p 8080",
15+
"test": "vitest",
16+
"test:once": "vitest run once",
17+
"test:e2e": "playwright test ",
1518
"prepublishOnly": "pnpm build:lib"
1619
},
1720
"files": [
@@ -21,9 +24,13 @@
2124
"author": "",
2225
"license": "MIT",
2326
"devDependencies": {
27+
"@playwright/test": "^1.48.2",
2428
"@stackblitz/sdk": "^1.11.0",
29+
"@types/node": "^22.9.0",
2530
"abell": "1.0.0-beta.5",
2631
"highlight.js": "^11.10.0",
32+
"http-server": "^14.1.1",
33+
"playwright": "^1.48.2",
2734
"vite": "^5.4.10"
2835
}
2936
}

playwright.config.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { defineConfig, devices } from "@playwright/test";
2+
3+
/**
4+
* Read environment variables from file.
5+
* https://github.com/motdotla/dotenv
6+
*/
7+
// import dotenv from 'dotenv';
8+
// import path from 'path';
9+
// dotenv.config({ path: path.resolve(__dirname, '.env') });
10+
11+
/**
12+
* See https://playwright.dev/docs/test-configuration.
13+
*/
14+
export default defineConfig({
15+
testDir: "./__tests__",
16+
/* Run tests in files in parallel */
17+
fullyParallel: true,
18+
/* Fail the build on CI if you accidentally left test.only in the source code. */
19+
forbidOnly: !!process.env.CI,
20+
/* Retry on CI only */
21+
retries: process.env.CI ? 2 : 0,
22+
/* Opt out of parallel tests on CI. */
23+
workers: process.env.CI ? 1 : undefined,
24+
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
25+
reporter: "html",
26+
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
27+
use: {
28+
/* Base URL to use in actions like `await page.goto('/')`. */
29+
baseURL: "http://127.0.0.1:8080",
30+
31+
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
32+
trace: "on-first-retry",
33+
},
34+
35+
webServer: {
36+
command: "pnpm preview",
37+
url: "http://127.0.0.1:8080/test.html",
38+
reuseExistingServer: !process.env.CI,
39+
stdout: "ignore",
40+
stderr: "pipe",
41+
},
42+
43+
/* Configure projects for major browsers */
44+
projects: [
45+
{
46+
name: "chromium",
47+
use: { ...devices["Desktop Chrome"] },
48+
},
49+
{
50+
name: "Mobile Safari",
51+
use: { ...devices["iPhone 12"] },
52+
},
53+
54+
/* Test against mobile viewports. */
55+
// {
56+
// name: 'Mobile Chrome',
57+
// use: { ...devices['Pixel 5'] },
58+
// },
59+
60+
/* Test against branded browsers. */
61+
// {
62+
// name: 'Microsoft Edge',
63+
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
64+
// },
65+
// {
66+
// name: 'Google Chrome',
67+
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
68+
// },
69+
],
70+
71+
/* Run your local dev server before starting the tests */
72+
// webServer: {
73+
// command: 'npm run start',
74+
// url: 'http://127.0.0.1:3000',
75+
// reuseExistingServer: !process.env.CI,
76+
// },
77+
});

0 commit comments

Comments
 (0)