1 import plugin from "../index"
2
3 describe("@medusajs/eslint-plugin", () => {
4 it("exposes a meta block with the plugin name", () => {
5 expect(plugin.meta).toEqual({ name: "@medusajs/eslint-plugin" })
6 })
7
8 it("registers the no-async-workflow-constructor rule", () => {
9 expect(Object.keys(plugin.rules)).toContain("no-async-workflow-constructor")
10 })
11
12 it("ships recommended, strict, and modules flat configs", () => {
13 expect(Object.keys(plugin.configs)).toEqual([
14 "recommended",
15 "strict",
16 "modules",
17 ])
18 expect(Array.isArray(plugin.configs.recommended)).toBe(true)
19 expect(Array.isArray(plugin.configs.strict)).toBe(true)
20 expect(Array.isArray(plugin.configs.modules)).toBe(true)
21 })
22
23 it("modules preset registers the plugin and applies module rules", () => {
24 const modules = plugin.configs.modules as Array<Record<string, unknown>>
25
26 const pluginBlock = modules.find((block) => block.plugins && !block.files)
27 expect(pluginBlock).toBeDefined()
28 expect((pluginBlock!.plugins as Record<string, unknown>)["@medusajs"]).toBe(
29 plugin
30 )
31
32 // Rules are split across blocks scoped by concern (services, module
33 // definition, data models); collect them all to assert coverage.
34 const allRules = Object.assign(
35 {},
36 ...modules
37 .filter((block) => block.rules)
38 .map((block) => block.rules as Record<string, unknown>)
39 )
40 expect(allRules).toHaveProperty("@medusajs/service-methods-must-be-async")
41 expect(allRules).toHaveProperty("@medusajs/module-name-snake-case")
42 expect(allRules).toHaveProperty(
43 "@medusajs/data-model-table-name-snake-case"
44 )
45
46 // Module-definition rule is scoped to the entry file, not the broad block.
47 const moduleDefBlock = modules.find(
48 (block) =>
49 block.rules &&
50 (block.rules as Record<string, unknown>)[
51 "@medusajs/module-name-snake-case"
52 ]
53 )
54 expect(moduleDefBlock!.files).toContain("src/index.{ts,js}")
55
56 // Data-model rules are scoped to models directories.
57 const modelsBlock = modules.find(
58 (block) =>
59 block.rules &&
60 (block.rules as Record<string, unknown>)[
61 "@medusajs/primary-key-required"
62 ]
63 )
64 expect(modelsBlock!.files).toContain("**/models/**/*.{ts,js}")
65 })
66
67 it("recommended preset includes a global-ignores block, a global plugin registration, and a TS parser block", () => {
68 const recommended = plugin.configs.recommended as Array<
69 Record<string, unknown>
70 >
71 const ignoresOnly = recommended.find(
72 (block) => block.ignores && !block.files && !block.rules
73 )
74 expect(ignoresOnly).toBeDefined()
75
76 // The plugin is registered in a `files`-less global block so that EVERY
77 // linted file (including `.js` files matched by rule blocks) can resolve
78 // `@medusajs/*` rule references — it is intentionally not scoped to TS.
79 const pluginBlock = recommended.find(
80 (block) => block.plugins && !block.files
81 )
82 expect(pluginBlock).toBeDefined()
83 expect((pluginBlock!.plugins as Record<string, unknown>)["@medusajs"]).toBe(
84 plugin
85 )
86
87 // The TypeScript parser stays scoped to TS files.
88 const parserBlock = recommended.find(
89 (block) =>
90 Array.isArray(block.files) &&
91 (block.languageOptions as Record<string, unknown> | undefined)?.parser
92 )
93 expect(parserBlock).toBeDefined()
94 })
95 })