All files / scripts convert-tests-to-vitest.js

0% Statements 0/138
0% Branches 0/1
0% Functions 0/1
0% Lines 0/138

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167                                                                                                                                                                                                                                                                                                                                             
#!/usr/bin/env node
 
import { readFileSync, writeFileSync, readdirSync, statSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
 
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
 
function convertTestFile(filePath) {
  const content = readFileSync(filePath, 'utf-8');
  
  // Skip if already converted
  if (content.includes('from \'vitest\'') || content.includes('from "vitest"')) {
    console.log(`โญ๏ธ Already converted: ${filePath}`);
    return false;
  }
 
  console.log(`๐Ÿ”„ Converting: ${filePath}`);
 
  let converted = content;
 
  // Replace imports
  if (content.includes('import { test } from \'node:test\';')) {
    converted = converted.replace(
      'import { test } from \'node:test\';',
      'import { describe, it, test, expect, vi } from \'vitest\';'
    );
  } else if (content.includes('from \'node:test\'')) {
    converted = converted.replace(
      /import { test.*} from 'node:test';/g,
      'import { describe, it, test, expect, vi } from \'vitest\';'
    );
  } else {
    // Add vitest import at the top after existing imports
    const lines = converted.split('\n');
    let insertIndex = 0;
    for (let i = 0; i < lines.length; i++) {
      if (lines[i].startsWith('import ') || lines[i].startsWith('/**') || lines[i].startsWith(' *') || lines[i].startsWith(' */')) {
        insertIndex = i + 1;
      } else if (lines[i].trim() === '') {
        continue;
      } else {
        break;
      }
    }
    lines.splice(insertIndex, 0, 'import { describe, it, test, expect, vi } from \'vitest\';', '');
    converted = lines.join('\n');
  }
 
  // Replace assert import
  converted = converted.replace(/import assert from 'node:assert';?\n?/g, '');
 
  // Convert console.log test descriptions to describe blocks
  const describeMatches = converted.match(/console\.log\(['"`]๐Ÿงช (.+?)['"`]\);?/g);
  if (describeMatches) {
    const testTitle = describeMatches[0].match(/console\.log\(['"`]๐Ÿงช (.+?)['"`]\);?/)[1];
    
    // Wrap the main test content in a describe block
    converted = converted.replace(/console\.log\(['"`]๐Ÿงช .+?['"`]\);?\n?/g, '');
    
    // Find where to start the describe block
    const lines = converted.split('\n');
    let startIndex = -1;
    for (let i = 0; i < lines.length; i++) {
      if (lines[i].includes('import ') || lines[i].startsWith('/**') || lines[i].startsWith(' *') || lines[i].trim() === '') {
        continue;
      } else {
        startIndex = i;
        break;
      }
    }
    
    if (startIndex !== -1) {
      lines.splice(startIndex, 0, '', `describe('${testTitle}', () => {`);
      lines.push('});');
      converted = lines.join('\n');
    }
  }
 
  // Convert console.assert statements to expect statements
  converted = converted.replace(
    /console\.assert\((.+?),\s*['"`](.+?)['"`]\);?/g,
    'expect($1).toBeTruthy(); // $2'
  );
 
  // Convert more specific console.assert patterns
  converted = converted.replace(
    /console\.assert\(typeof (.+?) === ['"`](.+?)['"`], ['"`](.+?)['"`]\);?/g,
    'expect(typeof $1).toBe(\'$2\'); // $3'
  );
 
  converted = converted.replace(
    /console\.assert\((.+?) === (.+?), ['"`](.+?)['"`]\);?/g,
    'expect($1).toBe($2); // $3'
  );
 
  converted = converted.replace(
    /console\.assert\((.+?) !== (.+?), ['"`](.+?)['"`]\);?/g,
    'expect($1).not.toBe($2); // $3'
  );
 
  converted = converted.replace(
    /console\.assert\((.+?)\.length === (.+?), ['"`](.+?)['"`]\);?/g,
    'expect($1.length).toBe($2); // $3'
  );
 
  // Convert test success logs to it blocks
  converted = converted.replace(
    /console\.log\(['"`]โœ… (.+?)['"`]\);?/g,
    ''
  );
 
  // Wrap loose test code in it blocks
  const itBlockRegex = /(?:\/\/ Test .+|.*test.+)/g;
  
  // Clean up console.log statements that are just status messages
  converted = converted.replace(/console\.log\(['"`][โœ…๐Ÿงช๐ŸŽ‰].+?['"`]\);?\n?/g, '');
 
  // Basic cleanup
  converted = converted.replace(/\n\n\n+/g, '\n\n');
  converted = converted.replace(/^\n+/, '');
 
  // Write the converted file
  writeFileSync(filePath, converted);
  return true;
}
 
function convertAllTests() {
  const packagesDir = './packages';
  const packages = readdirSync(packagesDir).filter(dir => 
    statSync(join(packagesDir, dir)).isDirectory()
  );
 
  let totalConverted = 0;
 
  packages.forEach(packageName => {
    const testDir = join(packagesDir, packageName, 'test');
    
    if (!statSync(testDir).isDirectory()) {
      return;
    }
 
    const testFiles = readdirSync(testDir, { recursive: true })
      .filter(file => file.endsWith('.test.js'))
      .map(file => join(testDir, file));
 
    testFiles.forEach(testFile => {
      try {
        if (convertTestFile(testFile)) {
          totalConverted++;
        }
      } catch (error) {
        console.error(`โŒ Failed to convert ${testFile}:`, error.message);
      }
    });
  });
 
  console.log(`\n๐ŸŽ‰ Conversion complete! Converted ${totalConverted} test files.`);
}
 
// Run if called directly
if (import.meta.url === `file://${process.argv[1]}`) {
  convertAllTests();
}
 
export { convertTestFile, convertAllTests };