Skip to content

Commit 8a0481b

Browse files
committed
Rewrite vitest helpers for Vue 3 Test Utils v2
Uses createRouter/createMemoryHistory instead of Vue 2 patterns. This confirms vitest properly imports Vue Router 4 functions without the Jest/Babel transformation issues.
1 parent 70dd810 commit 8a0481b

File tree

6 files changed

+157
-322
lines changed

6 files changed

+157
-322
lines changed

client/scripts/migrate-to-vitest.js

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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(/\.test\.(js|ts)$/, ".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
[/@tests\/jest\/helpers/g, "@tests/vitest/helpers"],
38-
38+
3939
// Add vitest imports if not present
40-
[/^(import .* from ['"]@vue\/test-utils['"];?)$/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+
/^(import .* from ['"]@vue\/test-utils['"];?)$/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
[/\bjest\.fn\(/g, "vi.fn("],
4952
[/\bjest\.spyOn\(/g, "vi.spyOn("],
@@ -55,12 +58,15 @@ function migrateTestFile(filePath) {
5558
[/\bjest\.clearAllMocks\(/g, "vi.clearAllMocks("],
5659
[/\bjest\.resetAllMocks\(/g, "vi.resetAllMocks("],
5760
[/\bjest\.restoreAllMocks\(/g, "vi.restoreAllMocks("],
58-
61+
5962
// Handle module mocking at top level
60-
[/^(jest\.mock\(.+\);?)$/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+
/^(jest\.mock\(.+\);?)$/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

117123
const testFile = args[0];
118-
migrateTestFile(testFile);
124+
migrateTestFile(testFile);

client/src/components/Common/BreadcrumbHeading.vitest.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,4 @@ describe("BreadcrumbHeading.vue", () => {
114114

115115
expect(wrapper.html()).toContain("Additional content");
116116
});
117-
});
117+
});

0 commit comments

Comments
 (0)