Skip to content

Commit

Permalink
tests: add playwright E2E test (saurabhdaware#5)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
saurabhdaware authored Nov 11, 2024
1 parent 58e2b1f commit 19b9e0f
Show file tree
Hide file tree
Showing 9 changed files with 454 additions and 9 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Playwright Tests
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
timeout-minutes: 60
runs-on: macos-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: lts/*
- name: Install dependencies
run: npm install -g pnpm && pnpm install
- name: Install Playwright Browsers
run: pnpm exec playwright install --with-deps
- name: Run Playwright tests
run: pnpm exec playwright test
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: playwright-report
path: playwright-report/
retention-days: 30
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ node_modules
dist
.DS_Store
dump
static
static
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
31 changes: 31 additions & 0 deletions __tests__/example.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// test.spec.ts
import { test, expect, Page } from "@playwright/test";

function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

test.describe.parallel("bsky-widget - Image Snapshot Test", () => {
test("should render the bsky-widget component", async ({
page,
}: {
page: Page;
}) => {
await page.goto("/test.html"); // Update with your test page path

await expect(page).toHaveTitle("E2E Test Page");

// Locate the bsky-widget component
const widgetElement = page.locator('bsky-widget[data-rendered="true"]');
const followers = widgetElement.locator(".followers");

// Check if the widget is visible
await expect(widgetElement).toBeVisible();

await sleep(2000);
await expect(page).toHaveScreenshot("baseline.png", {
mask: [followers],
maxDiffPixelRatio: 0.1,
});
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
"homepage": "https://bsky-widget.saurabhdaware.in/",
"scripts": {
"dev": "vite",
"build": "vite build:lib && vite build:playground",
"build": "pnpm build:lib && pnpm build:playground",
"build:lib": "LIB=true vite build",
"build:playground": "vite build",
"test": "echo \"Error: no test specified\" && exit 1",
"preview": "pnpm build:lib && http-server ./ -p 8080",
"test": "vitest",
"test:once": "vitest run once",
"test:e2e": "playwright test ",
"prepublishOnly": "pnpm build:lib"
},
"files": [
Expand All @@ -21,9 +24,13 @@
"author": "",
"license": "MIT",
"devDependencies": {
"@playwright/test": "^1.48.2",
"@stackblitz/sdk": "^1.11.0",
"@types/node": "^22.9.0",
"abell": "1.0.0-beta.5",
"highlight.js": "^11.10.0",
"http-server": "^14.1.1",
"playwright": "^1.48.2",
"vite": "^5.4.10"
}
}
77 changes: 77 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { defineConfig, devices } from "@playwright/test";

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// import dotenv from 'dotenv';
// import path from 'path';
// dotenv.config({ path: path.resolve(__dirname, '.env') });

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: "./__tests__",
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: "html",
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
baseURL: "http://127.0.0.1:8080",

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: "on-first-retry",
},

webServer: {
command: "pnpm preview",
url: "http://127.0.0.1:8080/test.html",
reuseExistingServer: !process.env.CI,
stdout: "ignore",
stderr: "pipe",
},

/* Configure projects for major browsers */
projects: [
{
name: "chromium",
use: { ...devices["Desktop Chrome"] },
},
{
name: "Mobile Safari",
use: { ...devices["iPhone 12"] },
},

/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },

/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],

/* Run your local dev server before starting the tests */
// webServer: {
// command: 'npm run start',
// url: 'http://127.0.0.1:3000',
// reuseExistingServer: !process.env.CI,
// },
});
Loading

0 comments on commit 19b9e0f

Please sign in to comment.