|
| 1 | +import { execSync } from 'child_process' |
| 2 | +import * as fs from 'fs' |
| 3 | +import * as path from 'path' |
| 4 | + |
| 5 | +const SNAPSHOTS_DIR = path.resolve(__dirname, 'visual-regression-snapshots') |
| 6 | +const SCREENSHOTS_DIR = path.resolve(__dirname, 'visual-regression-screenshots') |
| 7 | + |
| 8 | +function run(cmd: string) { |
| 9 | + execSync(cmd, { stdio: 'inherit' }) |
| 10 | +} |
| 11 | + |
| 12 | +function runQuiet(cmd: string) { |
| 13 | + execSync(cmd, { stdio: 'pipe' }) |
| 14 | +} |
| 15 | + |
| 16 | +const branch = execSync('git rev-parse --abbrev-ref HEAD', { encoding: 'utf-8' }).trim() |
| 17 | + |
| 18 | +console.log(`Current branch: ${branch}`) |
| 19 | +console.log('Checking out main source code (keeping branch e2e tests)...') |
| 20 | + |
| 21 | +// Only checkout main's source code, not e2e/ test files. |
| 22 | +// This ensures both baseline and branch runs use the same test code. |
| 23 | +runQuiet('git checkout main -- web/ common/') |
| 24 | + |
| 25 | +try { |
| 26 | + console.log('Running E2E on main source with VISUAL_REGRESSION=1...') |
| 27 | + if (fs.existsSync(SCREENSHOTS_DIR)) { |
| 28 | + fs.rmSync(SCREENSHOTS_DIR, { recursive: true }) |
| 29 | + } |
| 30 | + run('cross-env VISUAL_REGRESSION=1 npm run test') |
| 31 | + |
| 32 | + console.log('Copying screenshots to baselines...') |
| 33 | + if (fs.existsSync(SNAPSHOTS_DIR)) { |
| 34 | + fs.rmSync(SNAPSHOTS_DIR, { recursive: true }) |
| 35 | + } |
| 36 | + fs.cpSync(SCREENSHOTS_DIR, SNAPSHOTS_DIR, { recursive: true }) |
| 37 | + |
| 38 | + const count = fs.readdirSync(SNAPSHOTS_DIR).filter((f) => f.endsWith('.png')).length |
| 39 | + console.log(`Captured ${count} baseline snapshots`) |
| 40 | +} finally { |
| 41 | + // Restore branch's source code |
| 42 | + console.log('Restoring branch source code...') |
| 43 | + runQuiet('git checkout -- web/ common/') |
| 44 | +} |
| 45 | + |
| 46 | +console.log('Baselines ready. Now run: VISUAL_REGRESSION=1 npm run test && npm run test:visual:compare') |
0 commit comments