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

special-steps.test.ts

165 lines5.0 KB

1import { describe, it, expect, vi, beforeEach } from 'vitest'
2import * as fs from 'node:fs'
3import { postInitScript } from '../src/special-steps/post-init-script.js'
4import { rimrafNodeModules } from '../src/special-steps/rimraf-node-modules.js'
5import { runSpecialSteps } from '../src/special-steps/index.js'
6​
7import type { Environment, Options } from '../src/types.js'
8​
9vi.mock('node:fs')
10​
11describe('Special Steps', () => {
12 let mockEnvironment: Environment
13 let mockOptions: Options
14​
15 beforeEach(() => {
16 mockEnvironment = {
17 exists: vi.fn(),
18 execute: vi.fn(),
19 startStep: vi.fn(),
20 finishStep: vi.fn(),
21 error: vi.fn(),
22 warn: vi.fn(),
23 info: vi.fn(),
24 rimraf: vi.fn(),
25 deleteFile: vi.fn(),
26 readFile: vi.fn(),
27 } as unknown as Environment
28​
29 mockOptions = {
30 targetDir: '/test/project',
31 packageManager: 'npm',
32 chosenAddOns: [],
33 } as Options
34 })
35​
36 describe('postInitScript', () => {
37 it('should run post-create-init script when it exists', async () => {
38 const mockPackageJson = {
39 scripts: {
40 'post-create-init': 'echo "Running post-create-init"',
41 },
42 }
43​
44 vi.spyOn(mockEnvironment, 'exists').mockReturnValue(true)
45 vi.mocked(fs.readFileSync).mockReturnValue(
46 JSON.stringify(mockPackageJson),
47 )
48​
49 await postInitScript(mockEnvironment, mockOptions)
50​
51 expect(mockEnvironment.startStep).toHaveBeenCalledWith({
52 id: 'post-init-script',
53 type: 'command',
54 message: 'Running post-create-init script...',
55 })
56​
57 expect(mockEnvironment.execute).toHaveBeenCalledWith(
58 'npm',
59 ['run', 'post-create-init'],
60 '/test/project',
61 { inherit: true },
62 )
63​
64 expect(mockEnvironment.finishStep).toHaveBeenCalledWith(
65 'post-init-script',
66 'Post-cta-init script complete',
67 )
68 })
69​
70 it('should skip when package.json does not exist', async () => {
71 vi.spyOn(mockEnvironment, 'exists').mockReturnValue(false)
72​
73 await postInitScript(mockEnvironment, mockOptions)
74​
75 expect(mockEnvironment.warn).toHaveBeenCalledWith(
76 'Warning',
77 'No package.json found, skipping post-create-init script',
78 )
79 expect(mockEnvironment.execute).not.toHaveBeenCalled()
80 })
81​
82 it('should skip when post-create-init script does not exist', async () => {
83 const mockPackageJson = {
84 scripts: {
85 build: 'echo "Building"',
86 },
87 }
88​
89 vi.spyOn(mockEnvironment, 'exists').mockReturnValue(true)
90 vi.mocked(fs.readFileSync).mockReturnValue(
91 JSON.stringify(mockPackageJson),
92 )
93​
94 await postInitScript(mockEnvironment, mockOptions)
95​
96 // Note: The function now skips silently without calling info()
97 expect(mockEnvironment.execute).not.toHaveBeenCalled()
98 })
99​
100 it('should handle different package managers', async () => {
101 const packageManagers = ['yarn', 'pnpm', 'bun', 'deno'] as const
102 const expectedCommands = {
103 yarn: { command: 'yarn', args: ['run', 'post-create-init'] },
104 pnpm: { command: 'pnpm', args: ['post-create-init'] },
105 bun: { command: 'bun', args: ['--bun', 'run', 'post-create-init'] },
106 deno: { command: 'deno', args: ['task', 'post-create-init'] },
107 }
108​
109 for (const pm of packageManagers) {
110 mockOptions.packageManager = pm
111 vi.clearAllMocks()
112 vi.spyOn(mockEnvironment, 'exists').mockReturnValue(true)
113 vi.mocked(fs.readFileSync).mockReturnValue(
114 JSON.stringify({ scripts: { 'post-create-init': 'test' } }),
115 )
116​
117 await postInitScript(mockEnvironment, mockOptions)
118​
119 const expected = expectedCommands[pm]
120 expect(mockEnvironment.execute).toHaveBeenCalledWith(
121 expected.command,
122 expected.args,
123 '/test/project',
124 { inherit: true },
125 )
126 }
127 })
128 })
129​
130 describe('runSpecialSteps', () => {
131 it('should run multiple special steps in sequence', async () => {
132 const specialSteps = ['rimraf-node-modules', 'post-init-script']
133​
134 vi.spyOn(mockEnvironment, 'exists').mockReturnValue(true)
135 vi.mocked(fs.readFileSync).mockReturnValue(
136 JSON.stringify({ scripts: { 'post-create-init': 'test' } }),
137 )
138​
139 await runSpecialSteps(mockEnvironment, mockOptions, specialSteps)
140​
141 expect(mockEnvironment.startStep).toHaveBeenCalledWith({
142 id: 'special-steps',
143 type: 'command',
144 message: 'Running special steps...',
145 })
146​
147 expect(mockEnvironment.rimraf).toHaveBeenCalled()
148 expect(mockEnvironment.execute).toHaveBeenCalled()
149 expect(mockEnvironment.finishStep).toHaveBeenCalledWith(
150 'special-steps',
151 'Special steps complete',
152 )
153 })
154​
155 it('should handle unknown special steps', async () => {
156 const specialSteps = ['unknown-step']
157​
158 await runSpecialSteps(mockEnvironment, mockOptions, specialSteps)
159​
160 expect(mockEnvironment.error).toHaveBeenCalledWith(
161 'Special step unknown-step not found',
162 )
163 })
164 })
165})