1 import { createRuleTester } from "../../../test-utils"
2 import { rule } from "../rule"
3
4 const ruleTester = createRuleTester()
5
6 const JOB = "src/jobs/sync-products.ts"
7
8 ruleTester.run("scheduled-job-config-required", rule, {
9 valid: [
10 // Inline config object with name and schedule.
11 {
12 code: `export default async function () {}
13 export const config = { name: "sync-products", schedule: "0 0 * * *" }`,
14 filename: JOB,
15 },
16 // `index.ts` barrel in the jobs directory is a re-export, not a job — ignored.
17 {
18 code: `export * from "./sync-products"`,
19 filename: "src/jobs/index.ts",
20 },
21 // Typed config export.
22 {
23 code: `import type { MedusaContainer } from "@medusajs/framework/types"
24 export const config = { name: "daily", schedule: "* * * * *" }`,
25 filename: JOB,
26 },
27 // Interval-based schedule: `schedule` is an object with `interval`, not a
28 // cron string. The presence check is value-agnostic, so this is valid.
29 {
30 code: `export const config = {
31 name: "greeting-every-minute",
32 schedule: { interval: 60000 },
33 }`,
34 filename: JOB,
35 },
36 // Config declared separately, then exported by specifier.
37 {
38 code: `const config = { name: "daily", schedule: "* * * * *" }
39 export { config }`,
40 filename: JOB,
41 },
42 // Exported under an alias.
43 {
44 code: `const jobConfig = { name: "daily", schedule: "* * * * *" }
45 export { jobConfig as config }`,
46 filename: JOB,
47 },
48 // Config resolved through an identifier initializer.
49 {
50 code: `const base = { name: "daily", schedule: "* * * * *" }
51 export const config = base`,
52 filename: JOB,
53 },
54 // Re-export from another module — can't resolve the object, skip checks.
55 {
56 code: `export { config } from "./shared-config"`,
57 filename: JOB,
58 },
59 // Object spreads another value — name/schedule might come from the spread.
60 {
61 code: `export const config = { ...base }`,
62 filename: JOB,
63 },
64 // Spread alongside one property — the other might come from the spread.
65 {
66 code: `export const config = { ...base, name: "daily" }`,
67 filename: JOB,
68 },
69 // Config initialized via a call we can't resolve — skip checks.
70 {
71 code: `export const config = buildConfig()`,
72 filename: JOB,
73 },
74 ],
75 invalid: [
76 // No config export at all.
77 {
78 code: `export default async function () {}`,
79 filename: JOB,
80 errors: [{ messageId: "missingConfigExport" }],
81 },
82 // Empty config object — no name, no schedule.
83 {
84 code: `export const config = {}`,
85 filename: JOB,
86 errors: [
87 { messageId: "missingNameProperty" },
88 { messageId: "missingScheduleProperty" },
89 ],
90 },
91 // Missing schedule only.
92 {
93 code: `export const config = { name: "daily" }`,
94 filename: JOB,
95 errors: [{ messageId: "missingScheduleProperty" }],
96 },
97 // Missing name only.
98 {
99 code: `export const config = { schedule: "* * * * *" }`,
100 filename: JOB,
101 errors: [{ messageId: "missingNameProperty" }],
102 },
103 // Config declared separately without schedule, exported by specifier.
104 {
105 code: `const config = { name: "daily" }
106 export { config }`,
107 filename: JOB,
108 errors: [{ messageId: "missingScheduleProperty" }],
109 },
110 // Aliased export of an empty config object.
111 {
112 code: `const jobConfig = {}
113 export { jobConfig as config }`,
114 filename: JOB,
115 errors: [
116 { messageId: "missingNameProperty" },
117 { messageId: "missingScheduleProperty" },
118 ],
119 },
120 ],
121 })