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

create-app.test.ts

120 lines3.2 KB

1import { describe, expect, it } from 'vitest'
2import { resolve } from 'node:path'
3​
4import { createApp } from '../src/create-app.js'
5​
6import { createMemoryEnvironment } from '../src/environment.js'
7import { FILE_ROUTER } from '../src/constants.js'
8import { 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('should create an app - with a add-on', async () => {
92 const { environment, output } = createMemoryEnvironment()
93 await createApp(environment, {
94 ...simpleOptions,
95 git: true,
96 chosenAddOns: [
97 {
98 type: 'add-on',
99 phase: 'add-on',
100 warning: 'This is a warning',
101 command: {
102 command: 'echo',
103 args: ['Hello'],
104 },
105 packageAdditions: {
106 dependencies: {},
107 devDependencies: {},
108 },
109 getFiles: () => ['./src/test2.txt', './public/foo.jpg'],
110 getFileContents: () => 'base64::aGVsbG8=',
111 getDeletedFiles: () => [],
112 } as unknown as AddOn,
113 ],
114 })
115​
116 // This is ok, we convert to binary right at the end of the process
117 expect(output.files['/src/test2.txt']).toEqual('base64::aGVsbG8=')
118 expect(output.commands.some(({ command }) => command === 'echo')).toBe(true)
119 })
120})