Skip to content

Commit 9364dd1

Browse files
committed
chore add eslint rule tests
1 parent 6fd931a commit 9364dd1

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import path from "path";
2+
import { RuleTester } from "eslint";
3+
import { describe, it } from "vitest";
4+
import tsParser from "@typescript-eslint/parser";
5+
import rule from "./enforce-zod-v4.js";
6+
7+
const ROOT = process.cwd();
8+
const resolve = (p) => path.resolve(ROOT, p);
9+
10+
const ruleTester = new RuleTester({
11+
languageOptions: {
12+
parser: tsParser,
13+
parserOptions: { ecmaVersion: 2022, sourceType: "module" },
14+
},
15+
});
16+
17+
describe("enforce-zod-v4", () => {
18+
it("should allow zod/v4 imports in config.ts", () => {
19+
ruleTester.run("enforce-zod-v4", rule, {
20+
valid: [
21+
{
22+
filename: resolve("src/common/config.ts"),
23+
code: 'import { z } from "zod/v4";\n',
24+
},
25+
{
26+
filename: resolve("src/common/config.ts"),
27+
code: 'import * as z from "zod/v4";\n',
28+
},
29+
{
30+
filename: resolve("src/common/config.ts"),
31+
code: 'import type { ZodType } from "zod/v4";\n',
32+
},
33+
],
34+
invalid: [],
35+
});
36+
});
37+
38+
it("should allow regular zod imports in other files", () => {
39+
ruleTester.run("enforce-zod-v4", rule, {
40+
valid: [
41+
{
42+
filename: resolve("src/tools/tool.ts"),
43+
code: 'import { z } from "zod";\n',
44+
},
45+
{
46+
filename: resolve("src/resources/resource.ts"),
47+
code: 'import * as z from "zod";\n',
48+
},
49+
{
50+
filename: resolve("src/some/module.ts"),
51+
code: 'import type { ZodType } from "zod";\n',
52+
},
53+
],
54+
invalid: [],
55+
});
56+
});
57+
58+
it("should allow non-zod imports in any file", () => {
59+
ruleTester.run("enforce-zod-v4", rule, {
60+
valid: [
61+
{
62+
filename: resolve("src/tools/tool.ts"),
63+
code: 'import { something } from "some-package";\n',
64+
},
65+
{
66+
filename: resolve("src/common/config.ts"),
67+
code: 'import path from "path";\n',
68+
},
69+
{
70+
filename: resolve("src/resources/resource.ts"),
71+
code: 'import { Logger } from "./logger.js";\n',
72+
},
73+
],
74+
invalid: [],
75+
});
76+
});
77+
78+
it("should report error when zod/v4 is imported in files other than config.ts", () => {
79+
ruleTester.run("enforce-zod-v4", rule, {
80+
valid: [],
81+
invalid: [
82+
{
83+
filename: resolve("src/tools/tool.ts"),
84+
code: 'import { z } from "zod/v4";\n',
85+
errors: [
86+
{
87+
messageId: "enforceZodV4",
88+
data: { importPath: "zod/v4" },
89+
},
90+
],
91+
},
92+
{
93+
filename: resolve("src/resources/resource.ts"),
94+
code: 'import * as z from "zod/v4";\n',
95+
errors: [
96+
{
97+
messageId: "enforceZodV4",
98+
data: { importPath: "zod/v4" },
99+
},
100+
],
101+
},
102+
{
103+
filename: resolve("src/some/module.ts"),
104+
code: 'import type { ZodType } from "zod/v4";\n',
105+
errors: [
106+
{
107+
messageId: "enforceZodV4",
108+
data: { importPath: "zod/v4" },
109+
},
110+
],
111+
},
112+
{
113+
filename: resolve("tests/unit/toolBase.test.ts"),
114+
code: 'import { z } from "zod/v4";\n',
115+
errors: [
116+
{
117+
messageId: "enforceZodV4",
118+
data: { importPath: "zod/v4" },
119+
},
120+
],
121+
},
122+
],
123+
});
124+
});
125+
126+
it("should handle multiple imports in a single file", () => {
127+
ruleTester.run("enforce-zod-v4", rule, {
128+
valid: [
129+
{
130+
filename: resolve("src/common/config.ts"),
131+
code: `import { z } from "zod/v4";
132+
import path from "path";
133+
import type { UserConfig } from "./types.js";
134+
`,
135+
},
136+
],
137+
invalid: [
138+
{
139+
filename: resolve("src/tools/tool.ts"),
140+
code: `import { z } from "zod/v4";
141+
import path from "path";
142+
`,
143+
errors: [
144+
{
145+
messageId: "enforceZodV4",
146+
data: { importPath: "zod/v4" },
147+
},
148+
],
149+
},
150+
],
151+
});
152+
});
153+
});

0 commit comments

Comments
 (0)