@iqual/playwright-vrt 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/runner.ts DELETED
@@ -1,241 +0,0 @@
1
- #!/usr/bin/env bun
2
-
3
- import { spawn } from 'child_process';
4
- import * as path from 'path';
5
- import * as fs from 'fs';
6
- import type { VRTConfig } from './config';
7
-
8
- export interface TestResults {
9
- passed: number;
10
- failed: number;
11
- total: number;
12
- exitCode: number;
13
- }
14
-
15
- export interface RunnerOptions {
16
- config: VRTConfig;
17
- outputDir: string;
18
- verbose?: boolean;
19
- project?: string;
20
- updateBaseline?: boolean;
21
- hasExplicitReference?: boolean;
22
- headed?: boolean;
23
- }
24
-
25
- /**
26
- * Check if baseline snapshots already exist and are valid
27
- */
28
- export function hasExistingSnapshots(snapshotDir: string): boolean {
29
- if (!fs.existsSync(snapshotDir)) {
30
- return false;
31
- }
32
-
33
- // Recursively check for any .png files
34
- function hasSnapshotFiles(dir: string): boolean {
35
- const entries = fs.readdirSync(dir, { withFileTypes: true });
36
-
37
- for (const entry of entries) {
38
- const fullPath = path.join(dir, entry.name);
39
-
40
- if (entry.isDirectory()) {
41
- if (hasSnapshotFiles(fullPath)) {
42
- return true;
43
- }
44
- } else if (entry.name.endsWith('.png')) {
45
- return true;
46
- }
47
- }
48
-
49
- return false;
50
- }
51
-
52
- return hasSnapshotFiles(snapshotDir);
53
- }
54
-
55
- /**
56
- * Run Playwright tests using the shipped config and test files
57
- * Much simpler than the old approach - just exec playwright
58
- */
59
- export async function runVisualTests(options: RunnerOptions): Promise<TestResults> {
60
- const { config, outputDir, verbose, project, updateBaseline, hasExplicitReference, headed } = options;
61
-
62
- // Find the playwright-vrt package directory
63
- const packageDir = path.join(__dirname, '..');
64
- const playwrightConfigPath = path.join(packageDir, 'playwright.config.ts');
65
- const tsconfigPath = path.join(packageDir, 'tsconfig.json');
66
- const snapshotDir = path.join(process.cwd(), 'playwright-snapshots');
67
-
68
- // Check if baseline snapshots already exist (look for any .png files in snapshots)
69
- const hasBaseline = !updateBaseline && hasExistingSnapshots(snapshotDir);
70
-
71
- if (hasBaseline) {
72
- console.log('\n📸 Using existing baseline snapshots');
73
- if (verbose) {
74
- console.log(' (Use --update-baseline to regenerate from reference URL)');
75
- }
76
- } else {
77
- if (updateBaseline) {
78
- console.log('\n🔄 Updating baseline snapshots...');
79
- } else {
80
- console.log('\n📸 Creating baseline snapshots (first run)...');
81
- }
82
- console.log(` Source: ${config.referenceUrl}`);
83
-
84
- // Step 1: Create baseline screenshots
85
- await runPlaywright({
86
- configPath: playwrightConfigPath,
87
- tsconfigPath,
88
- baseURL: config.referenceUrl,
89
- vrtConfig: config,
90
- outputDir,
91
- updateSnapshots: true,
92
- verbose: true,
93
- project,
94
- headed,
95
- });
96
-
97
- console.log('✓ Baseline created');
98
- }
99
-
100
- console.log(`\n🧪 Testing ${config.testUrl}`);
101
-
102
- // Step 2: Run tests against test URL
103
- const exitCode = await runPlaywright({
104
- configPath: playwrightConfigPath,
105
- tsconfigPath,
106
- baseURL: config.testUrl,
107
- vrtConfig: config,
108
- outputDir,
109
- updateSnapshots: false,
110
- verbose: true,
111
- project,
112
- headed,
113
- });
114
-
115
- // Parse results
116
- const results = await parseResults(outputDir);
117
- results.exitCode = exitCode;
118
-
119
- return results;
120
- }
121
-
122
- interface PlaywrightRunOptions {
123
- configPath: string;
124
- tsconfigPath: string;
125
- baseURL: string;
126
- vrtConfig: VRTConfig;
127
- outputDir: string;
128
- updateSnapshots: boolean;
129
- verbose?: boolean;
130
- project?: string;
131
- headed?: boolean;
132
- }
133
-
134
- async function runPlaywright(options: PlaywrightRunOptions): Promise<number> {
135
- return new Promise((resolve, reject) => {
136
- const args = [
137
- 'playwright',
138
- 'test',
139
- '--config', options.configPath,
140
- '--tsconfig', options.tsconfigPath
141
- ];
142
-
143
- if (options.updateSnapshots) {
144
- args.push('--update-snapshots');
145
- }
146
-
147
- if (options.project) {
148
- args.push('--project', options.project);
149
- }
150
-
151
- if (options.headed) {
152
- args.push('--headed');
153
- }
154
-
155
- const env = {
156
- ...process.env,
157
- BASE_URL: options.baseURL,
158
- VRT_CONFIG: JSON.stringify(options.vrtConfig),
159
- OUTPUT_DIR: options.outputDir,
160
- };
161
-
162
- const proc = spawn('bunx', args, {
163
- env,
164
- stdio: options.verbose ? 'inherit' : 'pipe',
165
- shell: true,
166
- cwd: process.cwd(),
167
- }); let stdout = '';
168
- let stderr = '';
169
-
170
- if (!options.verbose) {
171
- proc.stdout?.on('data', (data) => {
172
- stdout += data.toString();
173
- });
174
- proc.stderr?.on('data', (data) => {
175
- stderr += data.toString();
176
- });
177
- }
178
-
179
- proc.on('close', (code) => {
180
- const exitCode = code || 0;
181
-
182
- // For baseline creation (update-snapshots), always succeed
183
- if (options.updateSnapshots) {
184
- resolve(0);
185
- } else {
186
- // For actual tests, return the exit code
187
- resolve(exitCode);
188
- }
189
- });
190
-
191
- proc.on('error', (error) => {
192
- reject(new Error(`Failed to run Playwright: ${error.message}`));
193
- });
194
- });
195
- }
196
-
197
- async function parseResults(outputDir: string): Promise<TestResults> {
198
- const resultsPath = path.join(outputDir, 'results.json');
199
-
200
- try {
201
- const file = Bun.file(resultsPath);
202
- const results = await file.json();
203
-
204
- let passed = 0;
205
- let failed = 0;
206
- let total = 0;
207
-
208
- // Parse Playwright JSON results
209
- if (results.suites) {
210
- for (const suite of results.suites) {
211
- if (suite.specs) {
212
- for (const spec of suite.specs) {
213
- total++;
214
- if (spec.ok) {
215
- passed++;
216
- } else {
217
- failed++;
218
- }
219
- }
220
- }
221
- }
222
- }
223
-
224
- return { passed, failed, total, exitCode: 0 };
225
- } catch {
226
- return { passed: 0, failed: 0, total: 0, exitCode: 1 };
227
- }
228
- }
229
-
230
- export function printResults(results: TestResults, config: VRTConfig): void {
231
- console.log('\n📊 Test Results:');
232
- console.log(` Total: ${results.total}`);
233
- console.log(` Passed: ${results.passed}`);
234
- console.log(` Failed: ${results.failed}`);
235
-
236
- if (results.failed > 0) {
237
- console.log(`\n❌ ${results.failed} visual difference(s) detected`);
238
- } else if (results.total > 0) {
239
- console.log('\n✅ All visual tests passed');
240
- }
241
- }
package/src/workspace.ts DELETED
@@ -1,64 +0,0 @@
1
- #!/usr/bin/env bun
2
-
3
- import * as fs from 'fs';
4
- import * as path from 'path';
5
-
6
- /**
7
- * Workspace manages the .playwright-vrt/ directory
8
- * This is persistent between runs for debugging and caching
9
- */
10
- export class Workspace {
11
- private readonly dir: string;
12
-
13
- constructor(baseDir?: string) {
14
- // Use .playwright-vrt in current directory (or specified base)
15
- this.dir = path.join(baseDir || process.cwd(), '.playwright-vrt');
16
-
17
- // Create workspace directory
18
- fs.mkdirSync(this.dir, { recursive: true });
19
- }
20
-
21
- getPath(file: string = ''): string {
22
- return file ? path.join(this.dir, file) : this.dir;
23
- }
24
-
25
- writeFile(filename: string, content: string): void {
26
- const filePath = this.getPath(filename);
27
- const dir = path.dirname(filePath);
28
-
29
- if (!fs.existsSync(dir)) {
30
- fs.mkdirSync(dir, { recursive: true });
31
- }
32
-
33
- fs.writeFileSync(filePath, content, 'utf-8');
34
- }
35
-
36
- readFile(filename: string): string {
37
- const filePath = this.getPath(filename);
38
- return fs.readFileSync(filePath, 'utf-8');
39
- }
40
-
41
- exists(filename: string): boolean {
42
- return fs.existsSync(this.getPath(filename));
43
- }
44
-
45
- writeJSON(filename: string, data: any): void {
46
- this.writeFile(filename, JSON.stringify(data, null, 2));
47
- }
48
-
49
- readJSON(filename: string): any {
50
- return JSON.parse(this.readFile(filename));
51
- }
52
-
53
- // Manual cleanup - workspace is persistent by default
54
- clean(): void {
55
- if (fs.existsSync(this.dir)) {
56
- fs.rmSync(this.dir, { recursive: true, force: true });
57
- console.log(`🗑️ Cleaned workspace: ${this.dir}`);
58
- }
59
- }
60
-
61
- info(): void {
62
- console.log(`📁 Workspace: ${this.dir}`);
63
- }
64
- }
package/tsconfig.json DELETED
@@ -1,19 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2022",
4
- "module": "ESNext",
5
- "lib": ["ES2022"],
6
- "moduleResolution": "bundler",
7
- "types": ["bun-types", "@types/node"],
8
- "strict": true,
9
- "esModuleInterop": true,
10
- "skipLibCheck": true,
11
- "forceConsistentCasingInFileNames": true,
12
- "resolveJsonModule": true,
13
- "allowSyntheticDefaultImports": true,
14
- "outDir": "./dist",
15
- "rootDir": "./src"
16
- },
17
- "include": ["src/**/*"],
18
- "exclude": ["node_modules", "dist"]
19
- }
File without changes