claude-code-automation
Version:
🚀 Generic project automation system with anti-compaction protection and recovery capabilities. Automatically detects project type (React, Node.js, Python, Rust, Go, Java) and provides intelligent analysis. Claude Code optimized - run 'welcome' after inst
100 lines (81 loc) • 3.46 kB
JavaScript
/**
* Test Fixing Script
* Properly converts all test files to clean Vitest format
*/
const fs = require('fs').promises;
const path = require('path');
class TestFixer {
async fixAllTests() {
console.log('🔧 Fixing all test files...');
const testFiles = [
'src/utils/Calculator.test.js',
'src/core/MathEngine.test.js',
'src/core/ScientificCalculator.test.js'
];
for (const testFile of testFiles) {
await this.fixTestFile(testFile);
}
console.log('✅ All test files fixed successfully');
}
async fixTestFile(relativePath) {
const filePath = path.join(process.cwd(), relativePath);
try {
let content = await fs.readFile(filePath, 'utf8');
console.log(`🔧 Fixing ${relativePath}...`);
// Clean imports
content = content.replace(
/const { describe, test, beforeEach.*?} = require\('node:test'\);/g,
"import { describe, test, beforeEach, expect } from 'vitest';"
);
content = content.replace(
/const assert = require\('node:assert'\);/g,
'// Using Vitest expect instead of assert'
);
// Remove module requires - they're loaded globally
content = content.replace(
/const \w+ = require\('\.\/.*?'\);/g,
'// Module loaded globally from test setup'
);
content = content.replace(
/const { \w+.*? } = require\('\.\.\/.*?'\);/g,
'// Constants loaded globally from test setup'
);
// Fix all malformed expect statements
content = content.replace(/expectstrictEqual/g, 'expect');
content = content.replace(/expectthrows/g, 'expect');
content = content.replace(/expectok/g, 'expect');
// Convert assert.strictEqual(a, b) to expect(a).toBe(b)
content = content.replace(
/expect\(([^,]+),\s*([^)]+)\);/g,
'expect($1).toBe($2);'
);
// Convert assert.ok(condition) to expect(condition).toBeTruthy()
content = content.replace(
/assert\.ok\(([^)]+)\);/g,
'expect($1).toBeTruthy();'
);
// Convert assert.throws patterns to expect().toThrow()
content = content.replace(
/assert\.throws\(\(\) => \{([^}]+)\}\);/g,
'expect(() => { $1 }).toThrow();'
);
// Handle specific expect().toThrow() patterns
content = content.replace(
/expect\(\(\) => \{([^}]+)\}\)\.toThrow\(\);/g,
'expect(() => { $1 }).toThrow();'
);
await fs.writeFile(filePath, content);
console.log(`✅ Fixed ${relativePath}`);
} catch (error) {
console.error(`❌ Error fixing ${relativePath}:`, error.message);
}
}
}
// Auto-execute
const fixer = new TestFixer();
fixer.fixAllTests()
.then(() => console.log('✅ Test fixing completed'))
.catch(error => {
console.error('❌ Test fixing failed:', error);
process.exit(1);
});