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

config-file.test.ts

105 lines2.9 KB

1import { beforeEach, describe, expect, it, vi } from 'vitest'
2import { fs, vol } from 'memfs'
3import { resolve } from 'node:path'
4​
5import {
6 readConfigFileFromEnvironment,
7 writeConfigFileToEnvironment,
8} from '../src/config-file.js'
9import { CONFIG_FILE } from '../src/constants.js'
10import { createMemoryEnvironment } from '../src/environment.js'
11import type { AddOn, Environment, Framework, Options } from '../src/types.js'
12​
13vi.mock('node:fs', () => fs)
14vi.mock('node:fs/promises', () => fs.promises)
15​
16beforeEach(() => {
17 vol.reset()
18})
19​
20describe('writeConfigFile', () => {
21 it('should write the config file', async () => {
22 const targetDir = 'test-dir'
23 const options = {
24 framework: {
25 id: 'react-cra',
26 getAddOns: () => [],
27 } as unknown as Framework,
28 chosenAddOns: [
29 {
30 id: 'add-on-1',
31 description: 'Add-on 1',
32 modes: ['file-router'],
33 } as AddOn,
34 ],
35 targetDir,
36 } as unknown as Options
37 const persistedOptions = {
38 version: 1,
39 framework: options.framework.id,
40 chosenAddOns: options.chosenAddOns.map((addOn) => addOn.id),
41 }
42 const env = {
43 writeFile: (path, optionsString) => {
44 expect(path).toEqual(resolve(targetDir, CONFIG_FILE))
45 expect(optionsString).toEqual(JSON.stringify(persistedOptions, null, 2))
46 },
47 } as Environment
48 await writeConfigFileToEnvironment(env, options)
49 })
50})
51​
52describe('readConfigFileFromEnvironment', () => {
53 it('should read the config file', async () => {
54 const targetDir = 'test-dir'
55 const persistedOptions = {
56 version: 1,
57 framework: 'react-cra',
58 chosenAddOns: ['add-on-1'],
59 }
60 const { environment } = createMemoryEnvironment()
61 environment.writeFile(
62 resolve(targetDir, CONFIG_FILE),
63 JSON.stringify(persistedOptions, null, 2),
64 )
65 const config = await readConfigFileFromEnvironment(environment, targetDir)
66 expect(config).toEqual(persistedOptions)
67 })
68​
69 it('should upgrade old config files', async () => {
70 const targetDir = 'test-dir'
71 const persistedOptions = {
72 framework: 'react',
73 projectName: 'foo',
74 typescript: false,
75 tailwind: false,
76 packageManager: 'pnpm',
77 toolchain: 'none',
78 mode: 'code-router',
79 git: true,
80 variableValues: {},
81 version: 1,
82 existingAddOns: [],
83 }
84 const { output, environment } = createMemoryEnvironment()
85 environment.writeFile(
86 resolve(targetDir, CONFIG_FILE),
87 JSON.stringify(persistedOptions, null, 2),
88 )
89 const config = await readConfigFileFromEnvironment(environment, targetDir)
90​
91 environment.finishRun()
92​
93 expect(config).toEqual({
94 framework: 'react-cra',
95 projectName: 'foo',
96 typescript: false,
97 tailwind: false,
98 packageManager: 'pnpm',
99 chosenAddOns: [],
100 git: true,
101 mode: 'code-router',
102 version: 1,
103 })
104 })
105})