@@ -16,13 +16,13 @@ function migrateTestFile(filePath) {
1616
1717 // Read the file
1818 let content = fs . readFileSync ( filePath , "utf8" ) ;
19-
19+
2020 // Create the new filename
2121 const dir = path . dirname ( filePath ) ;
2222 const basename = path . basename ( filePath ) ;
2323 const newBasename = basename . replace ( / \. t e s t \. ( j s | t s ) $ / , ".vitest.test.$1" ) ;
2424 const newFilePath = path . join ( dir , newBasename ) ;
25-
25+
2626 // Check if already migrated
2727 if ( fs . existsSync ( newFilePath ) ) {
2828 console . error ( `Vitest file already exists: ${ newFilePath } ` ) ;
@@ -35,15 +35,18 @@ function migrateTestFile(filePath) {
3535 const replacements = [
3636 // Update test helper imports
3737 [ / @ t e s t s \/ j e s t \/ h e l p e r s / g, "@tests/vitest/helpers" ] ,
38-
38+
3939 // Add vitest imports if not present
40- [ / ^ ( i m p o r t .* f r o m [ ' " ] @ v u e \/ t e s t - u t i l s [ ' " ] ; ? ) $ / m, ( match ) => {
41- if ( ! content . includes ( "from \"vitest\"" ) ) {
42- return match + "\nimport { describe, it, expect, beforeEach, afterEach, vi } from \"vitest\";" ;
43- }
44- return match ;
45- } ] ,
46-
40+ [
41+ / ^ ( i m p o r t .* f r o m [ ' " ] @ v u e \/ t e s t - u t i l s [ ' " ] ; ? ) $ / m,
42+ ( match ) => {
43+ if ( ! content . includes ( 'from "vitest"' ) ) {
44+ return match + '\nimport { describe, it, expect, beforeEach, afterEach, vi } from "vitest";' ;
45+ }
46+ return match ;
47+ } ,
48+ ] ,
49+
4750 // Replace jest with vi
4851 [ / \b j e s t \. f n \( / g, "vi.fn(" ] ,
4952 [ / \b j e s t \. s p y O n \( / g, "vi.spyOn(" ] ,
@@ -55,12 +58,15 @@ function migrateTestFile(filePath) {
5558 [ / \b j e s t \. c l e a r A l l M o c k s \( / g, "vi.clearAllMocks(" ] ,
5659 [ / \b j e s t \. r e s e t A l l M o c k s \( / g, "vi.resetAllMocks(" ] ,
5760 [ / \b j e s t \. r e s t o r e A l l M o c k s \( / g, "vi.restoreAllMocks(" ] ,
58-
61+
5962 // Handle module mocking at top level
60- [ / ^ ( j e s t \. m o c k \( .+ \) ; ? ) $ / gm, ( match ) => {
61- console . log ( ` ⚠️ Found module mock that may need manual review: ${ match } ` ) ;
62- return match . replace ( "jest.mock" , "vi.mock" ) ;
63- } ] ,
63+ [
64+ / ^ ( j e s t \. m o c k \( .+ \) ; ? ) $ / gm,
65+ ( match ) => {
66+ console . log ( ` ⚠️ Found module mock that may need manual review: ${ match } ` ) ;
67+ return match . replace ( "jest.mock" , "vi.mock" ) ;
68+ } ,
69+ ] ,
6470 ] ;
6571
6672 // Apply replacements
@@ -75,14 +81,14 @@ function migrateTestFile(filePath) {
7581 // Update migration tracker
7682 const trackerPath = path . join ( process . cwd ( ) , ".vitest-migrated.json" ) ;
7783 let tracker = { migrated : [ ] , notes : { } } ;
78-
84+
7985 if ( fs . existsSync ( trackerPath ) ) {
8086 tracker = JSON . parse ( fs . readFileSync ( trackerPath , "utf8" ) ) ;
8187 }
82-
88+
8389 const relativePath = path . relative ( process . cwd ( ) , newFilePath ) . replace ( / \\ / g, "/" ) ;
8490 const originalRelativePath = path . relative ( process . cwd ( ) , filePath ) . replace ( / \\ / g, "/" ) ;
85-
91+
8692 if ( ! tracker . migrated . includes ( relativePath ) ) {
8793 tracker . migrated . push ( relativePath ) ;
8894 tracker . lastUpdated = new Date ( ) . toISOString ( ) . split ( "T" ) [ 0 ] ;
@@ -91,7 +97,7 @@ function migrateTestFile(filePath) {
9197 migratedDate : new Date ( ) . toISOString ( ) . split ( "T" ) [ 0 ] ,
9298 changes : "Automated migration with jest->vi replacements" ,
9399 } ;
94-
100+
95101 fs . writeFileSync ( trackerPath , JSON . stringify ( tracker , null , 2 ) + "\n" ) ;
96102 console . log ( "✅ Updated .vitest-migrated.json" ) ;
97103 }
@@ -115,4 +121,4 @@ if (args.length === 0) {
115121}
116122
117123const testFile = args [ 0 ] ;
118- migrateTestFile ( testFile ) ;
124+ migrateTestFile ( testFile ) ;
0 commit comments