Skip to content

Commit

Permalink
test: add first e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
shootermv committed Jul 26, 2024
1 parent f7aabcb commit 799cc0e
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 5 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, master, add-first-e2e-test]
pull_request:
branches: [main, master]
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-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: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 30
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
2 changes: 0 additions & 2 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx pretty-quick --staged
35 changes: 35 additions & 0 deletions e2e/can-run-quiz.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { test, expect } from "@playwright/test";

const CATEGORIES = [
"HTML",
"CSS",
"JavaScript",
"Accessibility",
"General CS",
"IT",
"Linux",
"Python",
"SQL",
"Random"
] as const;

test.beforeEach(async ({ page }) => {
await page.goto("http://localhost:3000/#/quizzes");
});

test("has full list of categories", async ({ page }) => {
// Make sure the list has all the categores.
await expect(page.getByRole("button")).toHaveText(CATEGORIES);
});

test("clicking HTML category should lead to select-questions-number page ", async ({
page
}) => {
await page.getByRole("button", { name: "HTML" }).click();

expect(page.url()).toContain("HTML");

await expect(
page.getByRole("heading", { name: "Choose a length for the Quiz" })
).toBeVisible();
});
23 changes: 23 additions & 0 deletions e2e/sanity.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { test, expect } from "@playwright/test";

test.beforeEach(async ({ page }) => {
await page.goto("http://localhost:3000/");
});

test("has title", async ({ page }) => {
// Expect a title "to contain" a substring.
await expect(page).toHaveTitle(/Developer Quiz/);
});

test("pressing get started link should lead to select categories page", async ({
page
}) => {
// Click the get started link.
await page.getByRole("link", { name: "Get started (it's free)" }).click();

expect(page.url()).toContain("quizzes");
// Expects page to have a heading that says 'Choose a Category'.
await expect(
page.getByRole("heading", { name: "Choose a Category" })
).toBeVisible();
});
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
"start:local-docker": "docker container run --rm -it -p 3000:3000 developer-quiz-site",
"build": "vite build",
"test": "vitest",
"e2e:ui": "playwright test --ui",
"e2e:ci": "playwright test",
"lint": "eslint .",
"pretty-quick": "pretty-quick",
"fix-style": "pnpm run lint --fix",
Expand All @@ -50,6 +52,7 @@
]
},
"devDependencies": {
"@playwright/test": "^1.45.3",
"@types/node": "^20.0.0",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.0.4",
Expand Down
78 changes: 78 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { defineConfig, devices } from "@playwright/test";

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

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: "./e2e",
/* 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:3000',

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

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

{
name: "firefox",
use: { ...devices["Desktop Firefox"] }
},

{
name: "webkit",
use: { ...devices["Desktop Safari"] }
}

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

/* 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
}
});
38 changes: 38 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export default defineConfig({
plugins: [react()],
server: {
watch: {
usePolling: true, // required for container hot reloading
},
usePolling: true // required for container hot reloading
},
port: 3000,
host: true, // fixes container xdg-open issues
open: true
Expand All @@ -18,6 +18,11 @@ export default defineConfig({
test: {
globals: true,
environment: "happy-dom",
setupFiles: "./testSetup.ts"
setupFiles: "./testSetup.ts",
exclude: [
"**/node_modules/**",
"**/dist/**",
"**/e2e/*" /* do not include playwright files */
]
}
});

0 comments on commit 799cc0e

Please sign in to comment.