|
1 | 1 | import path from "path"; |
2 | 2 | import { defineConfig } from "cypress"; |
3 | | -import ComparePdf, { ComparePdfConfig } from "compare-pdf"; |
| 3 | +import { comparePdfToSnapshot } from "pdf-visual-diff"; |
4 | 4 |
|
5 | | -const comparePDFConfig: ComparePdfConfig = { |
6 | | - paths: { |
7 | | - actualPdfRootFolder: path.join(process.cwd(), "cypress", "downloads"), |
8 | | - baselinePdfRootFolder: path.join(process.cwd(), "cypress", "baseline"), |
9 | | - actualPngRootFolder: path.join( |
10 | | - process.cwd(), |
11 | | - "cypress", |
12 | | - "downloads", |
13 | | - "png" |
14 | | - ), |
15 | | - baselinePngRootFolder: path.join( |
16 | | - process.cwd(), |
17 | | - "cypress", |
18 | | - "baseline", |
19 | | - "png" |
20 | | - ), |
21 | | - diffPngRootFolder: path.join(process.cwd(), "cypress", "baseline", "diff"), |
22 | | - }, |
23 | | - settings: { |
24 | | - imageEngine: "native", |
25 | | - density: 150, |
26 | | - quality: 80, |
27 | | - tolerance: 0, |
28 | | - threshold: 0.1, |
29 | | - cleanPngPaths: false, |
30 | | - matchPageCount: true, |
31 | | - disableFontFace: true, |
32 | | - verbosity: 0, |
33 | | - }, |
34 | | -}; |
| 5 | +interface ComparisonResult { |
| 6 | + status: "passed" | "failed"; |
| 7 | + message?: string; |
| 8 | +} |
35 | 9 |
|
36 | 10 | export default defineConfig({ |
37 | 11 | e2e: { |
38 | 12 | setupNodeEvents(on) { |
39 | 13 | on("task", { |
40 | | - async compareFile(filename: string) { |
41 | | - const comparisonResults = await new ComparePdf(comparePDFConfig) |
42 | | - .actualPdfFile(filename) |
43 | | - .baselinePdfFile(filename) |
44 | | - .compare(); |
45 | | - return comparisonResults; |
| 14 | + async compareFile(filename: string): Promise<ComparisonResult> { |
| 15 | + const actualPath = path.join(process.cwd(), "cypress", "downloads", filename); |
| 16 | + const snapshotDir = path.join(process.cwd(), "cypress", "baseline"); |
| 17 | + |
| 18 | + try { |
| 19 | + // comparePdfToSnapshot returns true if PDFs match, false if they don't |
| 20 | + const matches = await comparePdfToSnapshot( |
| 21 | + actualPath, |
| 22 | + snapshotDir, |
| 23 | + filename, |
| 24 | + { |
| 25 | + tolerance: 0.1, |
| 26 | + pdf2PngOptions: { |
| 27 | + dpi: 150, |
| 28 | + }, |
| 29 | + } |
| 30 | + ); |
| 31 | + |
| 32 | + if (matches) { |
| 33 | + return { status: "passed" }; |
| 34 | + } else { |
| 35 | + return { |
| 36 | + status: "failed", |
| 37 | + message: `PDF comparison failed: ${filename} does not match baseline`, |
| 38 | + }; |
| 39 | + } |
| 40 | + } catch (error) { |
| 41 | + return { |
| 42 | + status: "failed", |
| 43 | + message: `PDF comparison error: ${error instanceof Error ? error.message : String(error)}`, |
| 44 | + }; |
| 45 | + } |
46 | 46 | }, |
47 | 47 | }); |
48 | 48 | }, |
|
0 commit comments