1 import fs from "fs"
2 import path from "path"
3 import { afterEach, beforeEach, describe, expect, it } from "@jest/globals"
4 import replaceZodImports from "../replace-zod-imports"
5
6 describe("replace-zod-imports codemod", () => {
7 const tempDir = path.join(__dirname, "temp-test-codemod")
8 let originalCwd: string
9
10 beforeEach(() => {
11 // Save original working directory
12 originalCwd = process.cwd()
13
14 // Create temp directory for test files
15 if (fs.existsSync(tempDir)) {
16 fs.rmSync(tempDir, { recursive: true, force: true })
17 }
18 fs.mkdirSync(tempDir, { recursive: true })
19
20 // Change to temp directory so codemod runs there
21 process.chdir(tempDir)
22 })
23
24 afterEach(() => {
25 // Restore original working directory
26 process.chdir(originalCwd)
27
28 // Clean up temp directory
29 if (fs.existsSync(tempDir)) {
30 fs.rmSync(tempDir, { recursive: true, force: true })
31 }
32 })
33
34 describe("codemod metadata", () => {
35 it("should have correct name and description", () => {
36 expect(replaceZodImports.name).toBe("replace-zod-imports")
37 expect(replaceZodImports.description).toBeTruthy()
38 expect(typeof replaceZodImports.run).toBe("function")
39 })
40 })
41
42 describe("named import transformations", () => {
43 it("should transform named imports from zod", async () => {
44 const testFile = path.join(tempDir, "test1.ts")
45 fs.writeFileSync(testFile, `import { z, ZodSchema } from "zod"`)
46
47 await replaceZodImports.run({ dryRun: false })
48
49 const result = fs.readFileSync(testFile, "utf8")
50 expect(result).toBe(
51 `import { z, ZodSchema } from "@medusajs/framework/zod"`
52 )
53 })
54
55 it("should transform named imports with single quotes", async () => {
56 const testFile = path.join(tempDir, "test2.ts")
57 fs.writeFileSync(testFile, `import { z } from 'zod'`)
58
59 await replaceZodImports.run({ dryRun: false })
60
61 const result = fs.readFileSync(testFile, "utf8")
62 expect(result).toBe(`import { z } from "@medusajs/framework/zod"`)
63 })
64 })
65
66 describe("default import transformations", () => {
67 it("should transform default imports with identifier zod to aliased named imports", async () => {
68 const testFile = path.join(tempDir, "test3.ts")
69 fs.writeFileSync(testFile, `import zod from "zod"`)
70
71 await replaceZodImports.run({ dryRun: false })
72
73 const result = fs.readFileSync(testFile, "utf8")
74 expect(result).toBe(`import { z as zod } from "@medusajs/framework/zod"`)
75 })
76
77 it("should transform default imports with identifier z to named imports", async () => {
78 const testFile = path.join(tempDir, "test3b.ts")
79 fs.writeFileSync(testFile, `import z from "zod"`)
80
81 await replaceZodImports.run({ dryRun: false })
82
83 const result = fs.readFileSync(testFile, "utf8")
84 expect(result).toBe(`import { z } from "@medusajs/framework/zod"`)
85 })
86 })
87
88 describe("namespace import transformations", () => {
89 it("should transform namespace imports with identifier z", async () => {
90 const testFile = path.join(tempDir, "test4.ts")
91 fs.writeFileSync(testFile, `import * as z from "zod"`)
92
93 await replaceZodImports.run({ dryRun: false })
94
95 const result = fs.readFileSync(testFile, "utf8")
96 expect(result).toBe(`import { z as z } from "@medusajs/framework/zod"`)
97 })
98
99 it("should transform namespace imports with custom identifier", async () => {
100 const testFile = path.join(tempDir, "test4b.ts")
101 fs.writeFileSync(testFile, `import * as validator from "zod"`)
102
103 await replaceZodImports.run({ dryRun: false })
104
105 const result = fs.readFileSync(testFile, "utf8")
106 expect(result).toBe(
107 `import { z as validator } from "@medusajs/framework/zod"`
108 )
109 })
110
111 it("should transform namespace imports with zod identifier", async () => {
112 const testFile = path.join(tempDir, "test4c.ts")
113 fs.writeFileSync(testFile, `import * as zod from "zod"`)
114
115 await replaceZodImports.run({ dryRun: false })
116
117 const result = fs.readFileSync(testFile, "utf8")
118 expect(result).toBe(`import { z as zod } from "@medusajs/framework/zod"`)
119 })
120 })
121
122 describe("type import transformations", () => {
123 it("should transform type imports", async () => {
124 const testFile = path.join(tempDir, "test5.ts")
125 fs.writeFileSync(testFile, `import type { ZodSchema } from "zod"`)
126
127 await replaceZodImports.run({ dryRun: false })
128
129 const result = fs.readFileSync(testFile, "utf8")
130 expect(result).toBe(
131 `import type { ZodSchema } from "@medusajs/framework/zod"`
132 )
133 })
134 })
135
136 describe("require statement transformations", () => {
137 it("should transform require statements", async () => {
138 const testFile = path.join(tempDir, "test6.js")
139 fs.writeFileSync(testFile, `const zod = require("zod")`)
140
141 await replaceZodImports.run({ dryRun: false })
142
143 const result = fs.readFileSync(testFile, "utf8")
144 expect(result).toBe(`const zod = require("@medusajs/framework/zod")`)
145 })
146
147 it("should transform require with single quotes", async () => {
148 const testFile = path.join(tempDir, "test7.js")
149 fs.writeFileSync(testFile, `const z = require('zod')`)
150
151 await replaceZodImports.run({ dryRun: false })
152
153 const result = fs.readFileSync(testFile, "utf8")
154 expect(result).toBe(`const z = require("@medusajs/framework/zod")`)
155 })
156 })
157
158 describe("multiple imports in one file", () => {
159 it("should handle multiple zod imports", async () => {
160 const testFile = path.join(tempDir, "test8.ts")
161 const content = `import { z } from "zod"
162 import { something } from "other-package"
163 import type { ZodSchema } from "zod"
164 const zodRequire = require("zod")`
165 fs.writeFileSync(testFile, content)
166
167 await replaceZodImports.run({ dryRun: false })
168
169 const result = fs.readFileSync(testFile, "utf8")
170 const expected = `import { z } from "@medusajs/framework/zod"
171 import { something } from "other-package"
172 import type { ZodSchema } from "@medusajs/framework/zod"
173 const zodRequire = require("@medusajs/framework/zod")`
174 expect(result).toBe(expected)
175 })
176 })
177
178 describe("dry-run mode", () => {
179 it("should not modify files in dry-run mode", async () => {
180 const testFile = path.join(tempDir, "test9.ts")
181 const originalContent = `import { z } from "zod"`
182 fs.writeFileSync(testFile, originalContent)
183
184 const result = await replaceZodImports.run({ dryRun: true })
185
186 const afterContent = fs.readFileSync(testFile, "utf8")
187 expect(afterContent).toBe(originalContent)
188 expect(result.filesModified).toBeGreaterThan(0)
189 expect(result.errors).toBe(0)
190 })
191 })
192
193 describe("files without zod imports", () => {
194 it("should not modify files without zod imports", async () => {
195 const testFile = path.join(tempDir, "test10.ts")
196 const originalContent = `import { something } from "other-package"`
197 fs.writeFileSync(testFile, originalContent)
198
199 await replaceZodImports.run({ dryRun: false })
200
201 const afterContent = fs.readFileSync(testFile, "utf8")
202 expect(afterContent).toBe(originalContent)
203 })
204
205 it("should not modify partial matches like zodiac", async () => {
206 const testFile = path.join(tempDir, "test11.ts")
207 const originalContent = `import { something } from "zodiac"`
208 fs.writeFileSync(testFile, originalContent)
209
210 await replaceZodImports.run({ dryRun: false })
211
212 const afterContent = fs.readFileSync(testFile, "utf8")
213 expect(afterContent).toBe(originalContent)
214 })
215 })
216
217 describe("multiple files", () => {
218 it("should handle multiple files with different extensions", async () => {
219 const file1 = path.join(tempDir, "file1.ts")
220 const file2 = path.join(tempDir, "file2.js")
221 const file3 = path.join(tempDir, "file3.tsx")
222
223 fs.writeFileSync(file1, `import { z } from "zod"`)
224 fs.writeFileSync(file2, `const z = require("zod")`)
225 fs.writeFileSync(file3, `import type { ZodType } from "zod"`)
226
227 const result = await replaceZodImports.run({ dryRun: false })
228
229 expect(fs.readFileSync(file1, "utf8")).toBe(
230 `import { z } from "@medusajs/framework/zod"`
231 )
232 expect(fs.readFileSync(file2, "utf8")).toBe(
233 `const z = require("@medusajs/framework/zod")`
234 )
235 expect(fs.readFileSync(file3, "utf8")).toBe(
236 `import type { ZodType } from "@medusajs/framework/zod"`
237 )
238 expect(result.filesModified).toBeGreaterThanOrEqual(3)
239 expect(result.errors).toBe(0)
240 })
241 })
242
243 describe("result reporting", () => {
244 it("should return correct counts", async () => {
245 const file1 = path.join(tempDir, "count1.ts")
246 const file2 = path.join(tempDir, "count2.ts")
247 const file3 = path.join(tempDir, "no-zod.ts")
248
249 fs.writeFileSync(file1, `import { z } from "zod"`)
250 fs.writeFileSync(file2, `import { z } from "zod"`)
251 fs.writeFileSync(file3, `import { x } from "other"`)
252
253 const result = await replaceZodImports.run({ dryRun: false })
254
255 expect(result.filesScanned).toBeGreaterThanOrEqual(2)
256 expect(result.filesModified).toBeGreaterThanOrEqual(2)
257 expect(result.errors).toBe(0)
258 })
259
260 it("should return zero counts when no files have zod imports", async () => {
261 const testFile = path.join(tempDir, "empty.ts")
262 fs.writeFileSync(testFile, `import { x } from "other"`)
263
264 const result = await replaceZodImports.run({ dryRun: false })
265
266 expect(result.filesScanned).toBe(0)
267 expect(result.filesModified).toBe(0)
268 expect(result.errors).toBe(0)
269 })
270 })
271
272 describe("file formatting preservation", () => {
273 it("should preserve whitespace and comments", async () => {
274 const testFile = path.join(tempDir, "formatted.ts")
275 const content = `// Header comment
276 import { z } from "zod"
277
278 // Function comment
279 export function validate() {
280 return z.string()
281 }`
282 fs.writeFileSync(testFile, content)
283
284 await replaceZodImports.run({ dryRun: false })
285
286 const result = fs.readFileSync(testFile, "utf8")
287 const expected = `// Header comment
288 import { z } from "@medusajs/framework/zod"
289
290 // Function comment
291 export function validate() {
292 return z.string()
293 }`
294 expect(result).toBe(expected)
295 })
296 })
297
298 describe("directory exclusions", () => {
299 it("should ignore files in src/admin directories", async () => {
300 const adminDir = path.join(tempDir, "src", "admin")
301 fs.mkdirSync(adminDir, { recursive: true })
302
303 const adminFile = path.join(adminDir, "admin-component.tsx")
304 const originalContent = `import { z } from "zod"`
305 fs.writeFileSync(adminFile, originalContent)
306
307 const regularFile = path.join(tempDir, "regular-file.ts")
308 fs.writeFileSync(regularFile, `import { z } from "zod"`)
309
310 const result = await replaceZodImports.run({ dryRun: false })
311
312 // Admin file should not be modified
313 const adminContent = fs.readFileSync(adminFile, "utf8")
314 expect(adminContent).toBe(originalContent)
315
316 // Regular file should be modified
317 const regularContent = fs.readFileSync(regularFile, "utf8")
318 expect(regularContent).toBe(`import { z } from "@medusajs/framework/zod"`)
319
320 // Result should only count the regular file
321 expect(result.filesModified).toBe(1)
322 })
323 })
324 })