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

create-app.test.ts

377 lines10.8 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('should create an app - with a add-on', async () => {
177 const { environment, output } = createMemoryEnvironment()
178 await createApp(environment, {
179 ...simpleOptions,
180 git: true,
181 chosenAddOns: [
182 {
183 type: 'add-on',
184 phase: 'add-on',
185 warning: 'This is a warning',
186 command: {
187 command: 'echo',
188 args: ['Hello'],
189 },
190 packageAdditions: {
191 dependencies: {},
192 devDependencies: {},
193 },
194 getFiles: () => ['src/test2.txt', 'public/foo.jpg'],
195 getFileContents: () => 'base64::aGVsbG8=',
196 getDeletedFiles: () => [],
197 } as unknown as AddOn,
198 ],
199 })
200​
201 // This is ok, we convert to binary right at the end of the process
202 expect(output.files['/src/test2.txt']).toEqual('base64::aGVsbG8=')
203 expect(output.commands.some(({ command }) => command === 'echo')).toBe(true)
204 })
205​
206 it('should strip demo files from add-ons when includeExamples is false', async () => {
207 const { environment, output } = createMemoryEnvironment()
208​
209 const demoFiles = [
210 'src/routes/demo/form.simple.tsx',
211 'src/routes/demo.form.tsx',
212 'src/routes/example.chat.tsx',
213 'src/components/demo-AIAssistant.tsx',
214 'src/components/demo.FormComponents.tsx',
215 'src/hooks/demo-useAudioRecorder.ts',
216 'src/hooks/demo.form.ts',
217 'src/lib/demo-store.ts',
218 'src/lib/demo.ai-hook.ts',
219 'src/data/demo-guitars.ts',
220 'src/store/demo.hooks.ts',
221 'src/store/demo.store.ts',
222 'src/demo.index.css',
223 'public/demo-neon.svg',
224 'public/example-guitar-flowers.jpg',
225 ]
226 const keepFiles = [
227 'src/routes/index.tsx',
228 'src/components/Header.tsx',
229 'src/lib/utils.ts',
230 ]
231 const allFiles = [...demoFiles, ...keepFiles]
232​
233 await createApp(environment, {
234 ...simpleOptions,
235 includeExamples: false,
236 chosenAddOns: [
237 {
238 id: 'test-addon',
239 type: 'add-on',
240 phase: 'add-on',
241 packageAdditions: { dependencies: {}, devDependencies: {} },
242 routes: [],
243 integrations: [],
244 getFiles: () => allFiles,
245 getFileContents: () => 'content',
246 getDeletedFiles: () => [],
247 } as unknown as AddOn,
248 ],
249 } as Options)
250​
251 for (const file of demoFiles) {
252 expect(output.files[`/${file}`]).toBeUndefined()
253 }
254 for (const file of keepFiles) {
255 expect(output.files[`/${file}`]).toBeDefined()
256 }
257 })
258​
259 it('should keep demo files from add-ons when includeExamples is true', async () => {
260 const { environment, output } = createMemoryEnvironment()
261​
262 await createApp(environment, {
263 ...simpleOptions,
264 includeExamples: true,
265 chosenAddOns: [
266 {
267 id: 'test-addon',
268 type: 'add-on',
269 phase: 'add-on',
270 packageAdditions: { dependencies: {}, devDependencies: {} },
271 routes: [],
272 integrations: [],
273 getFiles: () => ['src/components/demo-AIAssistant.tsx'],
274 getFileContents: () => 'content',
275 getDeletedFiles: () => [],
276 } as unknown as AddOn,
277 ],
278 } as Options)
279​
280 expect(output.files['/src/components/demo-AIAssistant.tsx']).toBeDefined()
281 })
282​
283 it('writes .env.example from add-on envVars metadata', async () => {
284 const { environment, output } = createMemoryEnvironment()
285​
286 await createApp(environment, {
287 ...simpleOptions,
288 chosenAddOns: [
289 {
290 id: 'fake-auth',
291 name: 'Fake Auth',
292 type: 'add-on',
293 phase: 'add-on',
294 packageAdditions: { dependencies: {}, devDependencies: {} },
295 routes: [],
296 integrations: [],
297 envVars: [
298 {
299 name: 'AUTH_SECRET',
300 description: 'Random secret used to sign sessions',
301 required: true,
302 },
303 {
304 name: 'AUTH_URL',
305 description: 'Base URL for auth callbacks',
306 required: false,
307 },
308 ],
309 getFiles: () => [],
310 getFileContents: () => '',
311 getDeletedFiles: () => [],
312 } as unknown as AddOn,
313 ],
314 } as Options)
315​
316 const envExample = output.files['/.env.example']
317 expect(envExample).toBeDefined()
318 expect(envExample).toContain('# Fake Auth')
319 expect(envExample).toContain(
320 '# Random secret used to sign sessions (required)',
321 )
322 expect(envExample).toContain('AUTH_SECRET=')
323 expect(envExample).toContain('# Base URL for auth callbacks')
324 expect(envExample).toContain('AUTH_URL=')
325 })
326​
327 it('does not write .env.example when no add-on declares envVars', async () => {
328 const { environment, output } = createMemoryEnvironment()
329​
330 await createApp(environment, {
331 ...simpleOptions,
332 chosenAddOns: [],
333 } as Options)
334​
335 expect(output.files['/.env.example']).toBeUndefined()
336 })
337​
338 it('dedupes env vars across add-ons in .env.example', async () => {
339 const { environment, output } = createMemoryEnvironment()
340​
341 await createApp(environment, {
342 ...simpleOptions,
343 chosenAddOns: [
344 {
345 id: 'addon-a',
346 name: 'Addon A',
347 type: 'add-on',
348 phase: 'add-on',
349 packageAdditions: { dependencies: {}, devDependencies: {} },
350 routes: [],
351 integrations: [],
352 envVars: [{ name: 'SHARED_KEY', required: true }],
353 getFiles: () => [],
354 getFileContents: () => '',
355 getDeletedFiles: () => [],
356 } as unknown as AddOn,
357 {
358 id: 'addon-b',
359 name: 'Addon B',
360 type: 'add-on',
361 phase: 'add-on',
362 packageAdditions: { dependencies: {}, devDependencies: {} },
363 routes: [],
364 integrations: [],
365 envVars: [{ name: 'SHARED_KEY', required: true }],
366 getFiles: () => [],
367 getFileContents: () => '',
368 getDeletedFiles: () => [],
369 } as unknown as AddOn,
370 ],
371 } as Options)
372​
373 const envExample = output.files['/.env.example'] || ''
374 const occurrences = envExample.match(/^SHARED_KEY=/gm) || []
375 expect(occurrences.length).toBe(1)
376 })
377})