|
1
|
import { createRuleTester } from "../../../test-utils" |
|
2
|
import { rule } from "../rule" |
|
3
|
|
|
4
|
const ruleTester = createRuleTester() |
|
5
|
|
|
6
|
const SUBSCRIBER = "src/subscribers/order-placed.ts" |
|
7
|
|
|
8
|
ruleTester.run("subscriber-config-export-required", rule, { |
|
9
|
valid: [ |
|
10
|
// Inline config object with a string event. |
|
11
|
{ |
|
12
|
code: `export default async function () {} |
|
13
|
export const config = { event: "order.placed" }`, |
|
14
|
filename: SUBSCRIBER, |
|
15
|
}, |
|
16
|
// Event as an array of strings. |
|
17
|
{ |
|
18
|
code: `export const config = { event: ["order.placed", "order.updated"] }`, |
|
19
|
filename: SUBSCRIBER, |
|
20
|
}, |
|
21
|
// Typed config export. |
|
22
|
{ |
|
23
|
code: `import type { SubscriberConfig } from "@medusajs/framework" |
|
24
|
export const config: SubscriberConfig = { event: "order.placed" }`, |
|
25
|
filename: SUBSCRIBER, |
|
26
|
}, |
|
27
|
// Config declared separately, then exported by specifier. |
|
28
|
{ |
|
29
|
code: `const config = { event: "order.placed" } |
|
30
|
export { config }`, |
|
31
|
filename: SUBSCRIBER, |
|
32
|
}, |
|
33
|
// Exported under an alias. |
|
34
|
{ |
|
35
|
code: `const subscriberConfig = { event: "order.placed" } |
|
36
|
export { subscriberConfig as config }`, |
|
37
|
filename: SUBSCRIBER, |
|
38
|
}, |
|
39
|
// Config resolved through an identifier initializer. |
|
40
|
{ |
|
41
|
code: `const base = { event: "order.placed" } |
|
42
|
export const config = base`, |
|
43
|
filename: SUBSCRIBER, |
|
44
|
}, |
|
45
|
// Re-export from another module — can't resolve the object, skip event check. |
|
46
|
{ |
|
47
|
code: `export { config } from "./shared-config"`, |
|
48
|
filename: SUBSCRIBER, |
|
49
|
}, |
|
50
|
// Object spreads another value — `event` might come from the spread, skip. |
|
51
|
{ |
|
52
|
code: `export const config = { ...base }`, |
|
53
|
filename: SUBSCRIBER, |
|
54
|
}, |
|
55
|
// Config initialized via a call we can't resolve — skip event check. |
|
56
|
{ |
|
57
|
code: `export const config = buildConfig()`, |
|
58
|
filename: SUBSCRIBER, |
|
59
|
}, |
|
60
|
], |
|
61
|
invalid: [ |
|
62
|
// No config export at all. |
|
63
|
{ |
|
64
|
code: `export default async function () {}`, |
|
65
|
filename: SUBSCRIBER, |
|
66
|
errors: [{ messageId: "missingConfigExport" }], |
|
67
|
}, |
|
68
|
// Empty config object — no event. |
|
69
|
{ |
|
70
|
code: `export const config = {}`, |
|
71
|
filename: SUBSCRIBER, |
|
72
|
errors: [{ messageId: "missingEventProperty" }], |
|
73
|
}, |
|
74
|
// Config object with other properties but no event. |
|
75
|
{ |
|
76
|
code: `export const config = { context: { subscriberId: "x" } }`, |
|
77
|
filename: SUBSCRIBER, |
|
78
|
errors: [{ messageId: "missingEventProperty" }], |
|
79
|
}, |
|
80
|
// Config declared separately without event, exported by specifier. |
|
81
|
{ |
|
82
|
code: `const config = { context: {} } |
|
83
|
export { config }`, |
|
84
|
filename: SUBSCRIBER, |
|
85
|
errors: [{ messageId: "missingEventProperty" }], |
|
86
|
}, |
|
87
|
// Aliased export of an event-less config object. |
|
88
|
{ |
|
89
|
code: `const subscriberConfig = {} |
|
90
|
export { subscriberConfig as config }`, |
|
91
|
filename: SUBSCRIBER, |
|
92
|
errors: [{ messageId: "missingEventProperty" }], |
|
93
|
}, |
|
94
|
], |
|
95
|
}) |