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
101 lines (80 loc) • 3.27 kB
JavaScript
/**
* Test Conversion Script
* Automatically converts Node.js test files to Vitest format
*/
const fs = require('fs').promises;
const path = require('path');
class TestConverter {
constructor() {
this.projectRoot = path.resolve(__dirname, '../..');
}
async convertAllTests() {
console.log('🔄 Converting test files to Vitest format...');
const testFiles = [
'src/utils/Calculator.test.js',
'src/core/MathEngine.test.js',
'src/core/ScientificCalculator.test.js'
];
for (const testFile of testFiles) {
await this.convertTestFile(testFile);
}
console.log('✅ All test files converted successfully');
}
async convertTestFile(relativePath) {
const filePath = path.join(this.projectRoot, relativePath);
try {
let content = await fs.readFile(filePath, 'utf8');
console.log(`📝 Converting ${relativePath}...`);
// Convert 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,
'// Assert replaced with Vitest expect'
);
// Remove require statements for our modules (they're global now)
content = content.replace(
/const \w+ = require\('\.\/?\w+\.js'\);/g,
'// Module available globally from test-setup.js'
);
content = content.replace(
/const { \w+[^}]* } = require\('[^']*'\);/g,
'// Constants available globally from test-setup.js'
);
// Convert assert statements to expect
content = this.convertAssertions(content);
await fs.writeFile(filePath, content);
console.log(`✅ Converted ${relativePath}`);
} catch (error) {
console.error(`❌ Error converting ${relativePath}:`, error.message);
}
}
convertAssertions(content) {
// 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();'
);
// Handle any remaining assert patterns
content = content.replace(/assert\./g, 'expect');
return content;
}
}
// Auto-execute if run directly
if (require.main === module) {
const converter = new TestConverter();
converter.convertAllTests()
.then(() => console.log('✅ Test conversion completed'))
.catch(error => {
console.error('❌ Test conversion failed:', error);
process.exit(1);
});
}
module.exports = TestConverter;