Skip to content
Closed
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
15 changes: 15 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ jobs:
- name: Build default app
run: npm run build

- name: Install Playwright browser
run: npx playwright install --with-deps chromium

- name: Export PDF
run: npm run test:pdf
env:
PDF_EXPORT_DIR: pdf-export

- name: Upload PDF export
uses: actions/upload-artifact@v4
with:
name: pdf-export
path: pdf-export/*.pdf
if-no-files-found: error

- name: Build GitHub Pages app
run: npm run build
env:
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,8 @@ dist
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
.vite/

# Playwright and generated PDF export artifacts
playwright-report/
test-results/
pdf-export/
64 changes: 64 additions & 0 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"dev": "vite --host 0.0.0.0",
"build": "vite build",
"preview": "vite preview --host 0.0.0.0"
"preview": "vite preview --host 0.0.0.0",
"test:pdf": "playwright test tests/pdf-export.spec.js"
},
"dependencies": {
"html2canvas": "^1.4.1",
Expand All @@ -16,6 +17,7 @@
"react-dom": "^19.0.0"
},
"devDependencies": {
"@playwright/test": "^1.60.0",
"@vitejs/plugin-react": "^5.0.0",
"vite": "^6.0.0"
}
Expand Down
26 changes: 26 additions & 0 deletions playwright.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { defineConfig, devices } from "@playwright/test";

export default defineConfig({
testDir: "./tests",
timeout: 120_000,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The global test timeout (120s) is identical to the specific timeout for the download event in the test script. This can cause the test to fail prematurely due to the overhead of browser startup and navigation. Increasing the global timeout provides a necessary buffer for teardown and reporting.

Suggested change
timeout: 120_000,
timeout: 150_000,

expect: {
timeout: 10_000,
},
use: {
acceptDownloads: true,
baseURL: "http://127.0.0.1:4173",
trace: "retain-on-failure",
},
webServer: {
command: "npm run preview -- --host 127.0.0.1 --port 4173",
reuseExistingServer: !process.env.CI,
timeout: 120_000,
url: "http://127.0.0.1:4173",
},
projects: [
{
name: "chromium",
use: { ...devices["Desktop Chrome"] },
},
],
});
30 changes: 30 additions & 0 deletions tests/pdf-export.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect, test } from "@playwright/test";
import { mkdir, readFile } from "node:fs/promises";
import path from "node:path";

const PDF_HEADER = "%PDF-";
const PDF_EXPORT_DIR = process.env.PDF_EXPORT_DIR || "pdf-export";
const EXPECTED_FILENAME = "minimal-web-slides-zh.pdf";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Hardcoding the expected filename to include -zh makes the test dependent on the environment's default locale. It's more robust to use a pattern check for the filename to ensure the test passes regardless of the browser's language settings.

Suggested change
const EXPECTED_FILENAME = "minimal-web-slides-zh.pdf";
const EXPECTED_FILENAME_PATTERN = /^minimal-web-slides-.*\.pdf$/;


test("exports the default deck as a PDF", async ({ page }) => {
await page.goto("/");

const exportButton = page.getByRole("button", { name: "Export PDF" });
await expect(exportButton).toBeEnabled();

const [download] = await Promise.all([
page.waitForEvent("download", { timeout: 120_000 }),
exportButton.click(),
]);

expect(download.suggestedFilename()).toBe(EXPECTED_FILENAME);

const outputDir = path.resolve(PDF_EXPORT_DIR);
const outputPath = path.join(outputDir, EXPECTED_FILENAME);
await mkdir(outputDir, { recursive: true });
Comment on lines +20 to +24

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Instead of relying on a hardcoded filename for both assertion and saving, use the filename suggested by the browser. This ensures the test correctly verifies the application's output and saves the file with the actual name generated, making the test more resilient to changes in naming logic or locale.

Suggested change
expect(download.suggestedFilename()).toBe(EXPECTED_FILENAME);
const outputDir = path.resolve(PDF_EXPORT_DIR);
const outputPath = path.join(outputDir, EXPECTED_FILENAME);
await mkdir(outputDir, { recursive: true });
const suggestedFilename = download.suggestedFilename();
expect(suggestedFilename).toMatch(EXPECTED_FILENAME_PATTERN);
const outputDir = path.resolve(PDF_EXPORT_DIR);
const outputPath = path.join(outputDir, suggestedFilename);
await mkdir(outputDir, { recursive: true });

await download.saveAs(outputPath);

const pdf = await readFile(outputPath);
expect(pdf.subarray(0, PDF_HEADER.length).toString()).toBe(PDF_HEADER);
expect(pdf.length).toBeGreaterThan(10_000);
});
Loading