Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✅test: add playwright #336

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/cleanup-cashes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
paths:
- "app/**"
- "public/**"
- "test/**"
- ".dockerignore"
- "bun.lockb"
- "Dockerfile"
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
paths:
- "app/**"
- "public/**"
- "test/**"
- ".dockerignore"
- "bun.lockb"
- "Dockerfile"
Expand All @@ -19,6 +20,7 @@ on:
paths:
- "app/**"
- "public/**"
- "test/**"
- ".dockerignore"
- "bun.lockb"
- "Dockerfile"
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/lambda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:
paths:
- "app/**"
- "public/**"
- "test/**"
- ".dockerignore"
- "bun.lockb"
- "Dockerfile"
Expand Down
64 changes: 64 additions & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Playwright Tests

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

on:
pull_request:
paths:
- "app/**"
- "public/**"
- "test/**"
- ".dockerignore"
- "bun.lockb"
- "Dockerfile"
- "package.json"
- "*config.*"
push:
paths:
- "app/**"
- "public/**"
- "test/**"
- ".dockerignore"
- "bun.lockb"
- "Dockerfile"
- "package.json"
- "*config.*"
workflow_dispatch:

jobs:
test:
timeout-minutes: 30
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
ref: ${{ env.SHA }}
env:
SHA: ${{ github.event.pull_request.head.sha || github.sha }}

- name: Install bun
uses: oven-sh/setup-bun@v2
with:
bun-version: canary

- name: Install dependencies
run: bun i

- name: Build app
run: bun run build

- name: Install Playwright Browsers
run: bunx playwright install --with-deps

- name: Run Playwright tests
run: bunx playwright test

- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: playwright-report
path: playwright-report/
retention-days: 30
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
paths:
- "app/**"
- "public/**"
- "test/**"
- ".dockerignore"
- "bun.lockb"
- "Dockerfile"
Expand All @@ -18,6 +19,7 @@ on:
paths:
- "app/**"
- "public/**"
- "test/**"
- ".dockerignore"
- "bun.lockb"
- "Dockerfile"
Expand Down Expand Up @@ -46,7 +48,7 @@ jobs:

- name: Test app
run: |
bun test &> >(tee -p test.log) || echo STATUS=$? >> $GITHUB_ENV
bun test:app &> >(tee -p test.log) || echo STATUS=$? >> $GITHUB_ENV
{
echo 'RESULT<<EOF'
cat test.log
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
!.yarn/versions

# testing
/blob-report/
/coverage
/playwright/.cache/
/playwright-report/
/test-results/

# next.js
/.next/
Expand Down
2 changes: 1 addition & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"recommendations": ["biomejs.biome"]
"recommendations": ["biomejs.biome", "ms-playwright.playwright"]
}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RUN --mount=type=bind,source=package.json,target=package.json \
--mount=type=cache,target=/root/.bun \
bun i --frozen-lockfile
COPY . .
RUN bun test
RUN bun test:app
RUN bun run build

FROM gcr.io/distroless/nodejs22-debian12:nonroot
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ $ cd blog && bun i
$ bun dev

# Test the app
$ bun test
$ bun test:app

# E2E Test
$ bun test:e2e

# Build the app
$ bun run build
Expand Down
Binary file modified bun.lockb
Binary file not shown.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"dev-win": "WATCHPACK_POLLING=true next dev --turbopack",
"build": "next build",
"start": "next start",
"lint": "bunx biome check --write"
"lint": "bunx biome check --write",
"test:app": "bun test test/app",
"test:e2e": "bun playwright test",
"test:report": "bunx playwright show-report"
},
"dependencies": {
"next": "canary",
Expand All @@ -17,6 +20,7 @@
"devDependencies": {
"@biomejs/biome": "^1.9.4",
"@happy-dom/global-registrator": "^15.7.4",
"@playwright/test": "next",
"@testing-library/dom": "^10.4.0",
"@testing-library/react": "^16.0.1",
"@types/bun": "^1.1.12",
Expand Down
79 changes: 79 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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({

Check warning on line 14 in playwright.config.ts

View workflow job for this annotation

GitHub Actions / quality

lint/style/noDefaultExport

Avoid default exports.
testDir: "./test/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://localhost:3000",

Check warning on line 29 in playwright.config.ts

View workflow job for this annotation

GitHub Actions / quality

lint/style/useNamingConvention

Two consecutive uppercase characters are not allowed in camelCase because strictCase is set to `true`.

/* 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: "bun .next/standalone/server.js",
url: "http://localhost:3000",
reuseExistingServer: !process.env.CI,
},
})
2 changes: 1 addition & 1 deletion app/page.test.tsx → test/app/page.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from "bun:test"
import Home from "@/app/page.tsx"
import { render } from "@testing-library/react"
import Home from "./page.tsx"

render(<Home />)

Expand Down
10 changes: 10 additions & 0 deletions test/e2e/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { expect, test } from "@playwright/test"

const title: RegExp = /Create Next App/

test("has title", async ({ page }) => {
await page.goto("/")

// Expect a title "to contain" a substring.
await expect(page).toHaveTitle(title)
})
Loading