|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +// Find all test files in the package |
| 7 | +const findTestFiles = (dir) => { |
| 8 | + let results = []; |
| 9 | + const files = fs.readdirSync(dir); |
| 10 | + |
| 11 | + for (const file of files) { |
| 12 | + const filePath = path.join(dir, file); |
| 13 | + const stat = fs.statSync(filePath); |
| 14 | + |
| 15 | + if (stat.isDirectory()) { |
| 16 | + results = results.concat(findTestFiles(filePath)); |
| 17 | + } else if (file.endsWith('.test.ts')) { |
| 18 | + results.push(filePath); |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + return results; |
| 23 | +}; |
| 24 | + |
| 25 | +// Convert Jest syntax to Vitest syntax |
| 26 | +const convertJestToVitest = (filePath) => { |
| 27 | + console.log(`Converting ${filePath}`); |
| 28 | + let content = fs.readFileSync(filePath, 'utf8'); |
| 29 | + |
| 30 | + // Add import for Vitest functions if it doesn't already exist |
| 31 | + if (!content.includes('import { describe, it, expect, beforeEach, vi } from \'vitest\';')) { |
| 32 | + // Find the last import statement |
| 33 | + const lastImportIndex = content.lastIndexOf('import '); |
| 34 | + if (lastImportIndex !== -1) { |
| 35 | + const endOfImportIndex = content.indexOf(';', lastImportIndex); |
| 36 | + if (endOfImportIndex !== -1) { |
| 37 | + content = |
| 38 | + content.slice(0, endOfImportIndex + 1) + |
| 39 | + '\nimport { describe, it, expect, beforeEach, vi } from \'vitest\';\n' + |
| 40 | + content.slice(endOfImportIndex + 1); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // Replace Jest specific functions with Vitest equivalents |
| 46 | + content = content.replace(/jest\./g, 'vi.'); |
| 47 | + content = content.replace(/jest\(/g, 'vi('); |
| 48 | + |
| 49 | + // Replace test() with it() |
| 50 | + content = content.replace(/test\(/g, 'it('); |
| 51 | + |
| 52 | + // Replace mocked with vi.mocked |
| 53 | + if (content.includes('import { mocked } from \'jest-mock\';')) { |
| 54 | + content = content.replace('import { mocked } from \'jest-mock\';', ''); |
| 55 | + content = content.replace(/mocked\(/g, 'vi.mocked('); |
| 56 | + } |
| 57 | + |
| 58 | + fs.writeFileSync(filePath, content, 'utf8'); |
| 59 | + console.log(`Converted ${filePath}`); |
| 60 | +}; |
| 61 | + |
| 62 | +// Create a custom matcher utility function if it doesn't exist |
| 63 | +const createTestUtilsFile = () => { |
| 64 | + const utilsPath = path.join(__dirname, 'src', 'test-utils.ts'); |
| 65 | + |
| 66 | + // Check if directory exists, create if not |
| 67 | + const dir = path.dirname(utilsPath); |
| 68 | + if (!fs.existsSync(dir)) { |
| 69 | + fs.mkdirSync(dir, { recursive: true }); |
| 70 | + } |
| 71 | + |
| 72 | + // If file doesn't exist, create it |
| 73 | + if (!fs.existsSync(utilsPath)) { |
| 74 | + console.log(`Creating test utilities file at ${utilsPath}`); |
| 75 | + const content = `import { AwsClientStubSpy } from 'aws-sdk-client-mock'; |
| 76 | +import { expect } from 'vitest'; |
| 77 | +
|
| 78 | +/** |
| 79 | + * Helper function to check if a command was received with specific input. |
| 80 | + * This provides similar functionality to toHaveReceivedCommandWith from aws-sdk-client-mock-jest. |
| 81 | + */ |
| 82 | +export function expectCommandCalledWith(mockClient: AwsClientStubSpy, command: any, expectedInput: any) { |
| 83 | + const calls = mockClient.commandCalls(command); |
| 84 | + expect(calls.length).toBeGreaterThan(0); |
| 85 | + expect(calls[0].args[0].input).toEqual(expectedInput); |
| 86 | +} |
| 87 | +
|
| 88 | +/** |
| 89 | + * Helper function to check if a command was called a specific number of times. |
| 90 | + * This provides similar functionality to toHaveReceivedCommandTimes from aws-sdk-client-mock-jest. |
| 91 | + */ |
| 92 | +export function expectCommandCalledTimes(mockClient: AwsClientStubSpy, command: any, times: number) { |
| 93 | + const calls = mockClient.commandCalls(command); |
| 94 | + expect(calls.length).toBe(times); |
| 95 | +} |
| 96 | +
|
| 97 | +/** |
| 98 | + * Helper function to check if a command was called at all. |
| 99 | + * This provides similar functionality to toHaveReceivedCommand from aws-sdk-client-mock-jest. |
| 100 | + */ |
| 101 | +export function expectCommandCalled(mockClient: AwsClientStubSpy, command: any) { |
| 102 | + const calls = mockClient.commandCalls(command); |
| 103 | + expect(calls.length).toBeGreaterThan(0); |
| 104 | +} |
| 105 | +
|
| 106 | +/** |
| 107 | + * Helper function to check if a command was not called. |
| 108 | + */ |
| 109 | +export function expectCommandNotCalled(mockClient: AwsClientStubSpy, command: any) { |
| 110 | + const calls = mockClient.commandCalls(command); |
| 111 | + expect(calls.length).toBe(0); |
| 112 | +}`; |
| 113 | + |
| 114 | + fs.writeFileSync(utilsPath, content, 'utf8'); |
| 115 | + console.log(`Created test utilities file at ${utilsPath}`); |
| 116 | + } else { |
| 117 | + console.log(`Test utilities file already exists at ${utilsPath}`); |
| 118 | + } |
| 119 | + |
| 120 | + return utilsPath; |
| 121 | +}; |
| 122 | + |
| 123 | +// Main function |
| 124 | +const main = () => { |
| 125 | + // Create test utilities file |
| 126 | + createTestUtilsFile(); |
| 127 | + |
| 128 | + const rootDir = path.join(__dirname, 'src'); |
| 129 | + const testFiles = findTestFiles(rootDir); |
| 130 | + |
| 131 | + console.log(`Found ${testFiles.length} test files to convert`); |
| 132 | + |
| 133 | + let processed = 0; |
| 134 | + let failed = 0; |
| 135 | + |
| 136 | + for (const file of testFiles) { |
| 137 | + try { |
| 138 | + convertJestToVitest(file); |
| 139 | + processed++; |
| 140 | + } catch (error) { |
| 141 | + console.error(`Error processing ${file}:`, error); |
| 142 | + failed++; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + console.log(`\nSummary:`); |
| 147 | + console.log(`- Total: ${testFiles.length} files`); |
| 148 | + console.log(`- Processed: ${processed} files`); |
| 149 | + console.log(`- Failed: ${failed} files`); |
| 150 | +}; |
| 151 | + |
| 152 | +main(); |
0 commit comments