|
1 | | -import path from 'node:path'; |
| 1 | +import { isDevMode } from '../client'; |
2 | 2 |
|
3 | 3 | /** |
4 | 4 | * Replicates the outputHeaderToPrefix logic from generate/client.ts for testing. |
@@ -48,125 +48,39 @@ describe('outputHeaderToPrefix logic', () => { |
48 | 48 | }); |
49 | 49 |
|
50 | 50 | describe('isDevMode logic', () => { |
51 | | - const scenarios: ReadonlyArray<{ |
52 | | - description: string; |
53 | | - expected: boolean; |
54 | | - mockPath: string; |
55 | | - }> = [ |
56 | | - { |
57 | | - description: 'returns true in dev mode (src/generate)', |
58 | | - expected: true, |
59 | | - mockPath: ['', 'home', 'user', 'packages', 'openapi-ts', 'src', 'generate'].join(path.sep), |
60 | | - }, |
61 | | - { |
62 | | - description: 'returns false in prod mode (dist/generate)', |
63 | | - expected: false, |
64 | | - mockPath: ['', 'home', 'user', 'packages', 'openapi-ts', 'dist', 'generate'].join(path.sep), |
65 | | - }, |
66 | | - { |
67 | | - description: 'returns false when path contains /src/ but not in correct position', |
68 | | - expected: false, |
69 | | - mockPath: [ |
70 | | - '', |
71 | | - 'home', |
72 | | - 'user', |
73 | | - 'src', |
74 | | - 'project', |
75 | | - 'node_modules', |
76 | | - '@hey-api', |
77 | | - 'openapi-ts', |
78 | | - 'dist', |
79 | | - 'generate', |
80 | | - ].join(path.sep), |
81 | | - }, |
82 | | - { |
83 | | - description: 'returns false when src is in project path (pnpm case from issue)', |
84 | | - expected: false, |
85 | | - mockPath: [ |
86 | | - '', |
87 | | - 'home', |
88 | | - 'user', |
89 | | - 'src', |
90 | | - 'thcdb', |
91 | | - 'worktree', |
92 | | - 'schema-gen', |
93 | | - 'web', |
94 | | - 'node_modules', |
95 | | - '.pnpm', |
96 | | - '@hey-api+openapi-ts@0.91.1', |
97 | | - 'node_modules', |
98 | | - '@hey-api', |
99 | | - 'openapi-ts', |
100 | | - 'dist', |
101 | | - 'generate', |
102 | | - ].join(path.sep), |
103 | | - }, |
104 | | - { |
105 | | - description: |
106 | | - 'returns false when src is in project path with plugins path (exact error from issue)', |
107 | | - expected: false, |
108 | | - mockPath: [ |
109 | | - '', |
110 | | - 'home', |
111 | | - 'user', |
112 | | - 'src', |
113 | | - 'thcdb', |
114 | | - 'worktree', |
115 | | - 'schema-gen', |
116 | | - 'web', |
117 | | - 'node_modules', |
118 | | - '.pnpm', |
119 | | - '@hey-api+openapi-ts@0.91.1_magicast@0.5.1_typescript@5.9.3', |
120 | | - 'node_modules', |
121 | | - '@hey-api', |
122 | | - 'openapi-ts', |
123 | | - 'plugins', |
124 | | - '@hey-api', |
125 | | - 'client-core', |
126 | | - 'bundle', |
127 | | - ].join(path.sep), |
128 | | - }, |
129 | | - { |
130 | | - description: 'returns true only when ending with src/generate', |
131 | | - expected: true, |
132 | | - mockPath: [ |
133 | | - '', |
134 | | - 'home', |
135 | | - 'user', |
136 | | - 'src', |
137 | | - 'backup', |
138 | | - 'packages', |
139 | | - 'openapi-ts', |
140 | | - 'src', |
141 | | - 'generate', |
142 | | - ].join(path.sep), |
143 | | - }, |
144 | | - { |
145 | | - description: 'returns false when not ending with generate', |
146 | | - expected: false, |
147 | | - mockPath: ['', 'home', 'user', 'packages', 'openapi-ts', 'src', 'plugins'].join(path.sep), |
148 | | - }, |
149 | | - { |
150 | | - description: 'returns false when src exists but dist is later', |
151 | | - expected: false, |
152 | | - mockPath: ['', 'home', 'user', 'src', 'project', 'openapi-ts', 'dist', 'generate'].join( |
153 | | - path.sep, |
154 | | - ), |
155 | | - }, |
156 | | - ]; |
157 | | - |
158 | | - it.each(scenarios)('$description', ({ expected, mockPath }) => { |
159 | | - // Test the isDevMode logic |
160 | | - const normalized = mockPath.split(path.sep); |
161 | | - const srcIndex = normalized.lastIndexOf('src'); |
162 | | - const distIndex = normalized.lastIndexOf('dist'); |
163 | | - |
164 | | - const result = |
165 | | - srcIndex !== -1 && |
166 | | - srcIndex > distIndex && |
167 | | - srcIndex === normalized.length - 2 && |
168 | | - normalized[srcIndex + 1] === 'generate'; |
169 | | - |
170 | | - expect(result).toBe(expected); |
| 51 | + const originalEnv = process.env; |
| 52 | + |
| 53 | + beforeEach(() => { |
| 54 | + process.env = { ...originalEnv }; |
| 55 | + delete process.env.OPENAPI_TS_DEV_MODE; |
| 56 | + }); |
| 57 | + |
| 58 | + afterAll(() => { |
| 59 | + process.env = originalEnv; |
| 60 | + }); |
| 61 | + |
| 62 | + it('returns false when env var is not set', () => { |
| 63 | + expect(isDevMode()).toBe(false); |
| 64 | + }); |
| 65 | + |
| 66 | + it('returns true when env var is set to "true"', () => { |
| 67 | + process.env.OPENAPI_TS_DEV_MODE = 'true'; |
| 68 | + expect(isDevMode()).toBe(true); |
| 69 | + }); |
| 70 | + |
| 71 | + it('returns true when env var is set to "1"', () => { |
| 72 | + process.env.OPENAPI_TS_DEV_MODE = '1'; |
| 73 | + expect(isDevMode()).toBe(true); |
| 74 | + }); |
| 75 | + |
| 76 | + it('returns false when env var is set to other values', () => { |
| 77 | + process.env.OPENAPI_TS_DEV_MODE = '0'; |
| 78 | + expect(isDevMode()).toBe(false); |
| 79 | + |
| 80 | + process.env.OPENAPI_TS_DEV_MODE = 'false'; |
| 81 | + expect(isDevMode()).toBe(false); |
| 82 | + |
| 83 | + process.env.OPENAPI_TS_DEV_MODE = 'anything'; |
| 84 | + expect(isDevMode()).toBe(false); |
171 | 85 | }); |
172 | 86 | }); |
0 commit comments