super-pancake-automation
Version:
A lightweight DOM-based UI automation framework using Chrome DevTools Protocol
122 lines (104 loc) โข 3.96 kB
JavaScript
import fs from 'fs';
import path from 'path';
import { exec } from 'child_process';
import { promisify } from 'util';
const execAsync = promisify(exec);
// Clean previous results
const resultsDir = path.resolve('test-report/results');
if (fs.existsSync(resultsDir)) {
const files = fs.readdirSync(resultsDir);
for (const file of files) {
const filePath = path.join(resultsDir, file);
if (fs.statSync(filePath).isDirectory()) {
const nestedFiles = fs.readdirSync(filePath);
for (const nestedFile of nestedFiles) {
if (nestedFile.endsWith('.json')) {
fs.unlinkSync(path.join(filePath, nestedFile));
}
}
} else if (file.endsWith('.json')) {
fs.unlinkSync(filePath);
}
}
}
console.log('\n๐ Starting Vitest run...\n');
try {
// Use exec instead of spawn for better Windows compatibility
const { stdout, stderr } = await execAsync('npx vitest run', {
encoding: 'utf8',
maxBuffer: 1024 * 1024 * 10 // 10MB buffer
});
// Output the results
if (stdout) {
console.log(stdout);
}
if (stderr) {
console.error(stderr);
}
// Parse results for summary - extract actual test counts from Vitest output
const testLinePassedSkipped = stdout.match(/Tests\s+(\d+)\s+passed\s+\|\s+(\d+)\s+skipped/);
const testLinePassedOnly = stdout.match(/Tests\s+(\d+)\s+passed\s+\((\d+)\)/);
const testLineMatch = testLinePassedSkipped || testLinePassedOnly;
let passed, failed, skipped, broken, total;
if (testLineMatch) {
passed = parseInt(testLineMatch[1]) || 0;
if (testLinePassedSkipped) {
skipped = parseInt(testLineMatch[2]) || 0;
} else {
skipped = 0; // No skipped tests in this format
}
failed = 0; // From exit code 0, we know no tests failed
broken = 0;
total = passed + failed + skipped + broken;
console.log(`๐ Parsed test counts: ${passed} passed, ${skipped} skipped, ${total} total`);
} else {
// Fallback to old symbol counting method
passed = (stdout.match(/โ/g) || []).length;
failed = (stdout.match(/ร/g) || []).length;
skipped = (stdout.match(/โ/g) || []).length;
broken = (stdout.match(/โ/g) || []).length;
total = passed + failed + skipped + broken;
console.log(`๐ Using fallback symbol counting: ${passed} passed, ${skipped} skipped, ${total} total`);
}
const summaryTable = `
โโโโโโโโโโโโโโโโฌโโโโโโโโโ
โ Label โ Count โ
โโโโโโโโโโโโโโโโผโโโโโโโโโค
โ ๐งช Total โ ${String(total).padStart(6)} โ
โ โ
Passed โ ${String(passed).padStart(6)} โ
โ โ Failed โ ${String(failed).padStart(6)} โ
โ โ ๏ธ Skipped โ ${String(skipped).padStart(6)} โ
โ ๐ฅ Broken โ ${String(broken).padStart(6)} โ
โ ๐ Exit Code โ 0 โ
โโโโโโโโโโโโโโโโดโโโโโโโโโ
`.trim();
console.log('\n๐ --- Test Summary ---');
console.log(`\n${summaryTable}\n`);
if (failed > 0 || broken > 0) {
console.error('๐ฅ Some tests failed.');
process.exit(1);
} else {
console.log('๐ All tests completed successfully.');
process.exit(0);
}
} catch (error) {
console.error('\nโ Error running tests:');
console.error(error.message);
if (error.code === 'ENOENT') {
console.error('\n๐ง Troubleshooting:');
console.error('1. Make sure Node.js and npm are installed');
console.error('2. Try running: npm install -g npx');
console.error('3. Verify vitest is available: npx vitest --version');
console.error('4. Check if you have test files in your project');
}
if (error.stdout) {
console.log('\n๐ค Standard Output:');
console.log(error.stdout);
}
if (error.stderr) {
console.error('\n๐ฅ Standard Error:');
console.error(error.stderr);
}
process.exit(1);
}