Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

create-app.test.ts

407 lines11.9 KB

1import { describe, expect, it } from 'vitest'
2import { resolve } from 'node:path'
3​
4import { createApp } from '../src/create-app.js'
5import { createApp as createEdgeApp } from '../src/edge-create-app.js'
6​
7import { createMemoryEnvironment } from '../src/environment.js'
8import type { AddOn, Options } from '../src/types.js'
9​
10const simpleOptions = {
11 projectName: 'test',
12 targetDir: '/',
13 framework: {
14 id: 'test',
15 name: 'Test',
16 basePackageJSON: {
17 scripts: {
18 dev: 'react-scripts start',
19 },
20 },
21 optionalPackages: {
22 typescript: {
23 devDependencies: {
24 typescript: '^5.0.0',
25 },
26 },
27 tailwindcss: {
28 dependencies: {
29 tailwindcss: '^3.0.0',
30 },
31 },
32 'file-router': {
33 dependencies: {
34 'file-router': '^1.0.0',
35 },
36 },
37 },
38 getFiles: () => ['src/test.txt'],
39 getFileContents: () => 'Hello',
40 getDeletedFiles: () => [],
41 },
42 chosenAddOns: [],
43 packageManager: 'pnpm',
44 typescript: true,
45 tailwind: true,
46 mode: 'file-router',
47 variableValues: {},
48} as unknown as Options
49​
50describe('createApp', () => {
51 it('should create an app', async () => {
52 const { environment, output } = createMemoryEnvironment()
53 await createApp(environment, simpleOptions)
54​
55 expect(output.files['/src/test.txt']).toEqual('Hello')
56 })
57​
58 it('should create an app - not silent', async () => {
59 const { environment, output } = createMemoryEnvironment()
60 await createApp(environment, {
61 ...simpleOptions,
62 targetDir: '/foo/bar/baz',
63 })
64​
65 const cwd = process.cwd()
66​
67 expect(output.files[resolve(cwd, '/foo/bar/baz/src/test.txt')]).toEqual(
68 'Hello',
69 )
70 })
71​
72 it('should create an app - with a starter', async () => {
73 const { environment, output } = createMemoryEnvironment()
74 await createApp(environment, {
75 ...simpleOptions,
76 starter: {
77 command: {
78 command: 'echo',
79 args: ['Hello'],
80 },
81 getFiles: () => ['src/test2.txt'],
82 getFileContents: () => 'Hello-2',
83 getDeletedFiles: () => [],
84 } as unknown as AddOn,
85 })
86​
87 expect(output.files['/src/test2.txt']).toEqual('Hello-2')
88 expect(output.commands.some(({ command }) => command === 'echo')).toBe(true)
89 })
90​
91 it.each(['react', 'solid'])(
92 'generates routes for %s file-router apps after install',
93 async (frameworkId) => {
94 const { environment, output } = createMemoryEnvironment()
95 await createApp(environment, {
96 ...simpleOptions,
97 framework: {
98 ...simpleOptions.framework,
99 id: frameworkId,
100 },
101 install: true,
102 } as Options)
103​
104 expect(output.commands).toContainEqual({
105 command: 'pnpm',
106 args: ['generate-routes'],
107 })
108 },
109 )
110​
111 it('skips route generation when dependency install is skipped', async () => {
112 const { environment, output } = createMemoryEnvironment()
113 await createApp(environment, {
114 ...simpleOptions,
115 framework: {
116 ...simpleOptions.framework,
117 id: 'react',
118 },
119 install: false,
120 })
121​
122 expect(output.commands).not.toContainEqual({
123 command: 'pnpm',
124 args: ['generate-routes'],
125 })
126 })
127​
128 it.each([createApp, createEdgeApp])(
129 'skips intent setup and reports the follow-up when dependency install is skipped (%#)',
130 async (create) => {
131 const { environment, output } = createMemoryEnvironment()
132 let outro = ''
133 environment.outro = (message: string) => {
134 outro = message
135 }
136 await create(environment, {
137 ...simpleOptions,
138 intent: true,
139 install: false,
140 })
141​
142 expect(output.commands).not.toContainEqual({
143 command: 'pnpm',
144 args: ['dlx', '@tanstack/intent', 'install'],
145 })
146 expect(outro).not.toContain('was wired up by TanStack Intent')
147 expect(outro).toContain('TanStack Intent setup was skipped')
148 expect(outro).toContain('pnpm install')
149 expect(outro).toContain('pnpm dlx @tanstack/intent install --map')
150 },
151 )
152​
153 it.each([createApp, createEdgeApp])(
154 'sets up intent and reports the agent config when dependencies are installed (%#)',
155 async (create) => {
156 const { environment, output } = createMemoryEnvironment()
157 let outro = ''
158 environment.outro = (message: string) => {
159 outro = message
160 }
161 await create(environment, {
162 ...simpleOptions,
163 intent: true,
164 install: true,
165 })
166​
167 expect(output.commands).toContainEqual({
168 command: 'pnpm',
169 args: ['dlx', '@tanstack/intent', 'install'],
170 })
171 expect(outro).toContain('was wired up by TanStack Intent')
172 expect(outro).not.toContain('TanStack Intent setup was skipped')
173 },
174 )
175​
176 it.each([createApp, createEdgeApp])(
177 'does not report successful Intent setup when the install command fails (%#)',
178 async (create) => {
179 const { environment } = createMemoryEnvironment()
180 const execute = environment.execute
181 environment.execute = (command, args, cwd, options) =>
182 args.includes('@tanstack/intent')
183 ? Promise.resolve({ stdout: '', failed: true })
184 : execute(command, args, cwd, options)
185 let outro = ''
186 const steps: Array<string> = []
187 environment.outro = (message) => {
188 outro = message
189 }
190 environment.finishStep = (_id, message) => {
191 steps.push(message)
192 }
193 await create(environment, {
194 ...simpleOptions,
195 intent: true,
196 install: true,
197 })
198​
199 expect(steps).toContain('TanStack Intent setup failed')
200 expect(steps).not.toContain('TanStack Intent configured')
201 expect(outro).not.toContain('was wired up by TanStack Intent')
202 expect(outro).toContain('To retry, run pnpm dlx @tanstack/intent install')
203 },
204 )
205​
206 it('should create an app - with a add-on', async () => {
207 const { environment, output } = createMemoryEnvironment()
208 await createApp(environment, {
209 ...simpleOptions,
210 git: true,
211 chosenAddOns: [
212 {
213 type: 'add-on',
214 phase: 'add-on',
215 warning: 'This is a warning',
216 command: {
217 command: 'echo',
218 args: ['Hello'],
219 },
220 packageAdditions: {
221 dependencies: {},
222 devDependencies: {},
223 },
224 getFiles: () => ['src/test2.txt', 'public/foo.jpg'],
225 getFileContents: () => 'base64::aGVsbG8=',
226 getDeletedFiles: () => [],
227 } as unknown as AddOn,
228 ],
229 })
230​
231 // This is ok, we convert to binary right at the end of the process
232 expect(output.files['/src/test2.txt']).toEqual('base64::aGVsbG8=')
233 expect(output.commands.some(({ command }) => command === 'echo')).toBe(true)
234 })
235​
236 it('should strip demo files from add-ons when includeExamples is false', async () => {
237 const { environment, output } = createMemoryEnvironment()
238​
239 const demoFiles = [
240 'src/routes/demo/form.simple.tsx',
241 'src/routes/demo.form.tsx',
242 'src/routes/example.chat.tsx',
243 'src/components/demo-AIAssistant.tsx',
244 'src/components/demo.FormComponents.tsx',
245 'src/hooks/demo-useAudioRecorder.ts',
246 'src/hooks/demo.form.ts',
247 'src/lib/demo-store.ts',
248 'src/lib/demo.ai-hook.ts',
249 'src/data/demo-guitars.ts',
250 'src/store/demo.hooks.ts',
251 'src/store/demo.store.ts',
252 'src/demo.index.css',
253 'public/demo-neon.svg',
254 'public/example-guitar-flowers.jpg',
255 ]
256 const keepFiles = [
257 'src/routes/index.tsx',
258 'src/components/Header.tsx',
259 'src/lib/utils.ts',
260 ]
261 const allFiles = [...demoFiles, ...keepFiles]
262​
263 await createApp(environment, {
264 ...simpleOptions,
265 includeExamples: false,
266 chosenAddOns: [
267 {
268 id: 'test-addon',
269 type: 'add-on',
270 phase: 'add-on',
271 packageAdditions: { dependencies: {}, devDependencies: {} },
272 routes: [],
273 integrations: [],
274 getFiles: () => allFiles,
275 getFileContents: () => 'content',
276 getDeletedFiles: () => [],
277 } as unknown as AddOn,
278 ],
279 } as Options)
280​
281 for (const file of demoFiles) {
282 expect(output.files[`/${file}`]).toBeUndefined()
283 }
284 for (const file of keepFiles) {
285 expect(output.files[`/${file}`]).toBeDefined()
286 }
287 })
288​
289 it('should keep demo files from add-ons when includeExamples is true', async () => {
290 const { environment, output } = createMemoryEnvironment()
291​
292 await createApp(environment, {
293 ...simpleOptions,
294 includeExamples: true,
295 chosenAddOns: [
296 {
297 id: 'test-addon',
298 type: 'add-on',
299 phase: 'add-on',
300 packageAdditions: { dependencies: {}, devDependencies: {} },
301 routes: [],
302 integrations: [],
303 getFiles: () => ['src/components/demo-AIAssistant.tsx'],
304 getFileContents: () => 'content',
305 getDeletedFiles: () => [],
306 } as unknown as AddOn,
307 ],
308 } as Options)
309​
310 expect(output.files['/src/components/demo-AIAssistant.tsx']).toBeDefined()
311 })
312​
313 it('writes .env.example from add-on envVars metadata', async () => {
314 const { environment, output } = createMemoryEnvironment()
315​
316 await createApp(environment, {
317 ...simpleOptions,
318 chosenAddOns: [
319 {
320 id: 'fake-auth',
321 name: 'Fake Auth',
322 type: 'add-on',
323 phase: 'add-on',
324 packageAdditions: { dependencies: {}, devDependencies: {} },
325 routes: [],
326 integrations: [],
327 envVars: [
328 {
329 name: 'AUTH_SECRET',
330 description: 'Random secret used to sign sessions',
331 required: true,
332 },
333 {
334 name: 'AUTH_URL',
335 description: 'Base URL for auth callbacks',
336 required: false,
337 },
338 ],
339 getFiles: () => [],
340 getFileContents: () => '',
341 getDeletedFiles: () => [],
342 } as unknown as AddOn,
343 ],
344 } as Options)
345​
346 const envExample = output.files['/.env.example']
347 expect(envExample).toBeDefined()
348 expect(envExample).toContain('# Fake Auth')
349 expect(envExample).toContain(
350 '# Random secret used to sign sessions (required)',
351 )
352 expect(envExample).toContain('AUTH_SECRET=')
353 expect(envExample).toContain('# Base URL for auth callbacks')
354 expect(envExample).toContain('AUTH_URL=')
355 })
356​
357 it('does not write .env.example when no add-on declares envVars', async () => {
358 const { environment, output } = createMemoryEnvironment()
359​
360 await createApp(environment, {
361 ...simpleOptions,
362 chosenAddOns: [],
363 } as Options)
364​
365 expect(output.files['/.env.example']).toBeUndefined()
366 })
367​
368 it('dedupes env vars across add-ons in .env.example', async () => {
369 const { environment, output } = createMemoryEnvironment()
370​
371 await createApp(environment, {
372 ...simpleOptions,
373 chosenAddOns: [
374 {
375 id: 'addon-a',
376 name: 'Addon A',
377 type: 'add-on',
378 phase: 'add-on',
379 packageAdditions: { dependencies: {}, devDependencies: {} },
380 routes: [],
381 integrations: [],
382 envVars: [{ name: 'SHARED_KEY', required: true }],
383 getFiles: () => [],
384 getFileContents: () => '',
385 getDeletedFiles: () => [],
386 } as unknown as AddOn,
387 {
388 id: 'addon-b',
389 name: 'Addon B',
390 type: 'add-on',
391 phase: 'add-on',
392 packageAdditions: { dependencies: {}, devDependencies: {} },
393 routes: [],
394 integrations: [],
395 envVars: [{ name: 'SHARED_KEY', required: true }],
396 getFiles: () => [],
397 getFileContents: () => '',
398 getDeletedFiles: () => [],
399 } as unknown as AddOn,
400 ],
401 } as Options)
402​
403 const envExample = output.files['/.env.example'] || ''
404 const occurrences = envExample.match(/^SHARED_KEY=/gm) || []
405 expect(occurrences.length).toBe(1)
406 })
407})