Skip to content

Commit e81e750

Browse files
committed
Add Playwright tests and GitHub Actions.
1 parent 7ae5afc commit e81e750

File tree

9 files changed

+312
-17
lines changed

9 files changed

+312
-17
lines changed

.github/workflows/playwright.yml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
name: CI - Build, Test, Deploy
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
build-and-test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v4
15+
16+
- name: Setup Node.js
17+
uses: actions/setup-node@v4
18+
with:
19+
node-version: "20"
20+
21+
- name: Install dependencies
22+
run: npm ci
23+
24+
- name: TypeScript check
25+
run: npm run typecheck
26+
27+
- name: Build app for validation
28+
run: npm run build
29+
30+
- name: Install Playwright browsers
31+
run: npx playwright install --with-deps
32+
33+
- name: Run Playwright tests
34+
run: npx playwright test
35+
36+
- name: Upload Playwright report
37+
uses: actions/upload-artifact@v4
38+
if: ${{ !cancelled() }}
39+
with:
40+
name: playwright-report
41+
path: playwright-report/
42+
retention-days: 7
43+
44+
deploy:
45+
needs: build-and-test
46+
runs-on: ubuntu-latest
47+
if: github.ref == 'refs/heads/main'
48+
steps:
49+
- name: Checkout code
50+
uses: actions/checkout@v4
51+
52+
- name: Setup Node.js
53+
uses: actions/setup-node@v4
54+
with:
55+
node-version: "20"
56+
57+
- name: Install dependencies
58+
run: npm ci
59+
60+
- name: Build React app
61+
run: npm run build
62+
63+
- name: Deploy to GitHub Pages
64+
uses: peaceiris/actions-gh-pages@v4
65+
with:
66+
github_token: ${{ secrets.GITHUB_TOKEN }}
67+
publish_dir: ./dist

.gitignore

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
/node_modules
1+
# Playwright
2+
node_modules/
3+
/test-results/
4+
/playwright-report/
5+
/blob-report/
6+
/playwright/.cache/
7+
8+
# Vite build output
9+
dist/

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ CSS Styling and layout
2424
│ └── images/
2525
│ └── quarter.png
2626
│ └── money/
27-
── images/
28-
│ ├── coke.png
29-
│ └── pepsi.png
27+
── images/
28+
| ├── coke.png
29+
| └── pepsi.png
3030
├── src/
3131
│ ├── components/
3232
│ │ ├── ErrorMessage.tsx

package-lock.json

Lines changed: 66 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
{
2-
"name": "VendingMachine",
2+
"name": "vending-machine",
33
"version": "1.0.0",
44
"private": true,
55
"scripts": {
66
"dev": "vite",
77
"build": "vite build",
8-
"preview": "vite preview"
8+
"preview": "vite preview",
9+
"typecheck": "tsc --noEmit"
910
},
1011
"dependencies": {
1112
"react": "^19.1.0",
12-
"react-dom": "^19.1.0",
13-
"react-scripts": "^0.0.0"
13+
"react-dom": "^19.1.0"
1414
},
1515
"devDependencies": {
16+
"@playwright/test": "^1.54.1",
17+
"@types/node": "^24.1.0",
1618
"@types/react": "^19.1.8",
1719
"@types/react-dom": "^19.1.6",
1820
"@vitejs/plugin-react": "^4.7.0",

playwright.config.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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://localhost:3000',
30+
31+
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
32+
trace: 'on-first-retry',
33+
},
34+
35+
/* Configure projects for major browsers */
36+
projects: [
37+
{
38+
name: 'chromium',
39+
use: { ...devices['Desktop Chrome'] },
40+
},
41+
42+
{
43+
name: 'firefox',
44+
use: { ...devices['Desktop Firefox'] },
45+
},
46+
47+
{
48+
name: 'webkit',
49+
use: { ...devices['Desktop Safari'] },
50+
},
51+
52+
/* Test against mobile viewports. */
53+
// {
54+
// name: 'Mobile Chrome',
55+
// use: { ...devices['Pixel 5'] },
56+
// },
57+
// {
58+
// name: 'Mobile Safari',
59+
// use: { ...devices['iPhone 12'] },
60+
// },
61+
62+
/* Test against branded browsers. */
63+
// {
64+
// name: 'Microsoft Edge',
65+
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
66+
// },
67+
// {
68+
// name: 'Google Chrome',
69+
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
70+
// },
71+
],
72+
73+
/* Run your local dev server before starting the tests */
74+
webServer: {
75+
command: 'npm run dev',
76+
url: 'http://localhost:5173/',
77+
reuseExistingServer: !process.env.CI,
78+
},
79+
});

src/components/MoneySlot.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const MoneySlot = ({ amountInserted, onInsertQuarter }: Props) => {
2323

2424
{/* Display the current amount of money inserted */}
2525
<div className="money-display" data-testid="money-display">
26-
${amountInserted}
26+
${amountInserted.toFixed(2)}
2727
</div>
2828
</div>
2929
);

src/components/ProductList.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ const ProductList = ({ products, onPurchase }: Props) => {
2121
<table data-testid="product-table"> {/* Table element for displaying products */}
2222
<thead> {/* Table header row */}
2323
<tr>
24-
<th>Product</th>
25-
<th>Price</th>
26-
<th>Remaining</th>
24+
<th data-testid="product-header">Product</th>
25+
<th data-testid="price-header">Price</th>
26+
<th data-testid="remaining-header">Remaining</th>
2727
</tr>
2828
</thead>
2929
<tbody>

0 commit comments

Comments
 (0)