diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 202e929..07f24b5 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -11,6 +11,8 @@ on: jobs: build: + name: Build and Test ๐ ๏ธ ๐งช + runs-on: ubuntu-latest # Ref: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs#example-using-npm @@ -24,4 +26,16 @@ jobs: - name: Install dependencies run: npm install - run: npm run build --if-present - - run: npm test + - name: Run unit tests ๐งช ๐ โ๏ธ + run: npm run test + - name: Install Playwright Browsers ๐งช ๐ฃ + run: npx playwright install --with-deps + - name: Run Playwright tests ๐งช ๐ ๐ผ๏ธ + run: npm run test-ui + - name: Upload Playwright report ๐งช ๐ + uses: actions/upload-artifact@v4 + if: always() + with: + name: playwright-report + path: playwright-report/ + retention-days: 3 diff --git a/.gitignore b/.gitignore index 042e98f..10e71d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,9 @@ /node_modules public/build package-lock.json + +# Tests +/test-results/ +/playwright-report/ +/blob-report/ +/playwright/.cache/ diff --git a/README.md b/README.md index 9db06eb..188c2ee 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,31 @@ Format the code ### Test app -`npm run test` +Unit tests + +```bash +npm run test +``` + +UI tests + +- Precondition: Install browsers for UI tests + +```bash +npx playwright install --with-deps +``` + +Run tests headless mode + +```bash +npm run test-ui +``` + +Interactive mode + +```bash +npm run test-ui -- --ui +``` ### Run app ๐ diff --git a/package.json b/package.json index 0d44d0f..ac26b13 100644 --- a/package.json +++ b/package.json @@ -8,17 +8,20 @@ "build": "node build.js", "prettier": "prettier . --write", "start": "node dev-server.js", - "test": "node --test" + "test": "node --test", + "test-ui": "playwright test" }, "author": "", "license": "MIT", "description": "", "devDependencies": { "@fastify/static": "7.0.4", + "@playwright/test": "^1.45.1", "@rollup/plugin-commonjs": "26.0.1", "@rollup/plugin-node-resolve": "15.2.3", "@rollup/plugin-replace": "5.0.7", "@rollup/plugin-terser": "0.4.4", + "@types/node": "^20.14.10", "fastify": "4.28.0", "prettier": "3.3.2", "rollup": "4.18.0" diff --git a/playwright.config.js b/playwright.config.js new file mode 100644 index 0000000..b7912b7 --- /dev/null +++ b/playwright.config.js @@ -0,0 +1,79 @@ +import { defineConfig, devices } from "@playwright/test"; + +/** + * Read environment variables from file. + * https://github.com/motdotla/dotenv + */ +// require('dotenv').config({ path: path.resolve(__dirname, '.env') }); + +/** + * @see https://playwright.dev/docs/test-configuration + */ +const config = defineConfig({ + testDir: "./test-ui", + /* 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:4000", + + /* 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:4000", + reuseExistingServer: !process.env.CI, + }, +}); + +export default config; diff --git a/public/index.html b/public/index.html index cdd3d71..d218080 100644 --- a/public/index.html +++ b/public/index.html @@ -1,6 +1,6 @@
-