Skip to content

Commit

Permalink
feat(SPV-1063): setup visual test done by playwright
Browse files Browse the repository at this point in the history
  • Loading branch information
swierzbicki committed Sep 26, 2024
1 parent c7e7b9d commit f0d026d
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 14 deletions.
37 changes: 35 additions & 2 deletions .github/workflows/on-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
run: |
yarn lint
test:
unit-test:
runs-on: ubuntu-latest
steps:
- name: Checkout ${{ github.ref }}
Expand All @@ -75,8 +75,41 @@ jobs:

- name: Unit test
run: |
yarn test
yarn test:unit
visual-test:
runs-on: ubuntu-latest
steps:
- name: Checkout ${{ github.ref }}
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version-file: ${{ env.NODE_VERSION_FILE }}
cache: ${{ env.PACKAGE_MANGER }}
cache-dependency-path: ${{ env.PACKAGE_MANGER_LOCK_FILE }}

- name: Install dependencies
run: yarn install --frozen-lockfile --immutable

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

- name: Run Playwright tests
run: yarn test:visual

- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: test-results
path: |
test-results/
!**/*.zip
retention-days: 3

- name: Publish Test Summary Results
run: npx github-actions-ctrf playwright-report/test-results.json
compile:
runs-on: ubuntu-latest
steps:
Expand Down
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ dist
dist-ssr
*.local

# tests
test-results
playwright-report
/playwright/.cache/


# Editor directories and files
.vscode/*
!.vscode/extensions.json
Expand All @@ -23,3 +29,4 @@ dist-ssr
*.njsproj
*.sln
*.sw?
/blob-report/
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint --report-unused-disable-directives --max-warnings 0",
"test": "vitest run",
"test:unit": "vitest --config ./vitest.config.unit.ts",
"test:visual": "playwright test -c playwright.config.ts",
"test:visual:update": "playwright test --update-snapshots",
"coverage": "vitest run --coverage",
"preview": "vite preview",
"prettier:check": "prettier --check 'src/**/*.{ts,tsx,js,jsx,json,css}'",
Expand All @@ -21,6 +23,7 @@
"@4chain-ag/react-configuration": "^1.0.4",
"@bsv/sdk": "^1.0.37",
"@bsv/spv-wallet-js-client": "^1.0.0-beta.20",
"@estruyf/github-actions-reporter": "^1.9.2",
"@heroicons/react": "^2.1.3",
"@hookform/resolvers": "^3.9.0",
"@radix-ui/react-accordion": "^1.1.2",
Expand Down Expand Up @@ -58,6 +61,7 @@
"devDependencies": {
"@eslint/compat": "^1.0.1",
"@eslint/js": "^9.3.0",
"@playwright/test": "^1.47.2",
"@tanstack/eslint-plugin-query": "^5.35.6",
"@tanstack/router-devtools": "^1.33.4",
"@tanstack/router-vite-plugin": "^1.32.17",
Expand Down
99 changes: 99 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { defineConfig, devices } from '@playwright/test';
import { GitHubActionOptions } from '@estruyf/github-actions-reporter';

/**
* 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: './src/__tests__',
snapshotPathTemplate: '{testDir}/visual/{testName}/{arg}{ext}',
expect: {
// Maximum time expect() should wait for the condition to be met.
timeout: 5000,

toHaveScreenshot: {
// An acceptable ratio of pixels that are different to the
// total amount of pixels, between 0 and 1.
maxDiffPixelRatio: 0.02,
},
},
/* 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 ? 1 : 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', { outputFolder: 'playwright-report/html' }],
[
'@estruyf/github-actions-reporter',
<GitHubActionOptions>{
title: 'Visual tests report',
useDetails: true,
},
],
],
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */

/* 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: 'yarn run dev',
port: 5173,
reuseExistingServer: !process.env.CI,
timeout: 60 * 1000,
},
use: {
baseURL: 'http://localhost:5173',
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions src/__tests__/visual/login-page.visual.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { expect, test } from '@playwright/test';

test.describe('login page', () => {
test('login form', async ({ page }) => {
await page.goto('/');
await page.waitForSelector('form#login-form');

await expect(page).toHaveScreenshot();
});
});
2 changes: 1 addition & 1 deletion src/routes/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export function LoginForm() {
<h1 className="text-2xl font-bold mb-16">SPV Wallet Admin</h1>
<Card className="w-full max-w-sm">
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)}>
<form id="login-form" onSubmit={form.handleSubmit(onSubmit)}>
<CardHeader>
<CardTitle className="text-2xl">Login</CardTitle>
</CardHeader>
Expand Down
File renamed without changes.
21 changes: 11 additions & 10 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'
import { TanStackRouterVite } from '@tanstack/router-vite-plugin'
import path from "path";
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react-swc';
import { TanStackRouterVite } from '@tanstack/router-vite-plugin';
import path from 'path';

export const srcAlias = {
'@': path.resolve(__dirname, './src'),
};

// https://vitejs.dev/config/
export default defineConfig({
plugins: [TanStackRouterVite(), react() ],
plugins: [TanStackRouterVite(), react()],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
}
})
alias: srcAlias,
},
});
13 changes: 13 additions & 0 deletions vitest.config.unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from 'vitest/config';
import { srcAlias } from './vite.config';

export default defineConfig({
resolve: {
alias: srcAlias,
},
test: {
globals: true,
environment: 'node',
include: ['**/*.unit.test.ts'],
},
});
Loading

0 comments on commit f0d026d

Please sign in to comment.