Skip to content

Commit 491a579

Browse files
rexxarsclaude
andcommitted
test: fix mock isolation — use vi.resetAllMocks() with beforeEach defaults
vi.clearAllMocks() only clears call history, not implementations. Mocks like getMonoRepo.mockResolvedValue([...]) would leak into subsequent tests. Switch to vi.resetAllMocks() in afterEach and move all default implementations into beforeEach. Also bring mkdir and spinner into the mocks object so their implementations survive resets. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 1854cd4 commit 491a579

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

packages/@sanity/cli/src/actions/init/__tests__/bootstrapRemoteTemplate.test.ts

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
import {type Output} from '@sanity/cli-core'
2-
import {afterEach, describe, expect, test, vi} from 'vitest'
2+
import {afterEach, beforeEach, describe, expect, test, vi} from 'vitest'
33

44
import {bootstrapRemoteTemplate} from '../bootstrapRemoteTemplate.js'
55

66
const mocks = vi.hoisted(() => ({
7-
applyEnvVariables: vi.fn().mockResolvedValue(undefined),
8-
checkIfNeedsApiToken: vi.fn().mockResolvedValue(false),
9-
createCorsOrigin: vi.fn().mockResolvedValue({}),
10-
createToken: vi.fn().mockResolvedValue({key: 'test-token'}),
11-
detectFrameworkRecord: vi.fn().mockResolvedValue(null),
12-
downloadAndExtractRepo: vi.fn().mockResolvedValue(undefined),
7+
applyEnvVariables: vi.fn(),
8+
checkIfNeedsApiToken: vi.fn(),
9+
createCorsOrigin: vi.fn(),
10+
createToken: vi.fn(),
11+
detectFrameworkRecord: vi.fn(),
12+
downloadAndExtractRepo: vi.fn(),
1313
getDefaultPortForFramework: vi.fn(),
14-
getGitHubRawContentUrl: vi
15-
.fn()
16-
.mockReturnValue('https://raw.githubusercontent.com/sanity-io/test-template/main/'),
17-
getMonoRepo: vi.fn().mockResolvedValue(null),
18-
tryApplyPackageName: vi.fn().mockResolvedValue(undefined),
14+
getGitHubRawContentUrl: vi.fn(),
15+
getMonoRepo: vi.fn(),
16+
mkdir: vi.fn(),
17+
spinner: vi.fn(),
18+
tryApplyPackageName: vi.fn(),
1919
tryGitInit: vi.fn(),
20-
updateInitialTemplateMetadata: vi.fn().mockResolvedValue(undefined),
21-
validateTemplate: vi.fn().mockResolvedValue({isValid: true}),
20+
updateInitialTemplateMetadata: vi.fn(),
21+
validateTemplate: vi.fn(),
2222
}))
2323

24-
vi.mock('node:fs/promises', () => ({mkdir: vi.fn().mockResolvedValue(undefined)}))
24+
vi.mock('node:fs/promises', () => ({mkdir: mocks.mkdir}))
2525

2626
vi.mock('@sanity/template-validator', () => ({
2727
getMonoRepo: mocks.getMonoRepo,
@@ -38,7 +38,7 @@ vi.mock('@vercel/fs-detectors', () => ({
3838

3939
vi.mock('@sanity/cli-core/ux', () => ({
4040
logSymbols: {success: '✔'},
41-
spinner: vi.fn().mockReturnValue({start: vi.fn().mockReturnThis(), succeed: vi.fn()}),
41+
spinner: mocks.spinner,
4242
}))
4343

4444
vi.mock('../../../services/cors.js', () => ({createCorsOrigin: mocks.createCorsOrigin}))
@@ -81,14 +81,31 @@ const baseOpts = {
8181
}
8282

8383
describe('bootstrapRemoteTemplate', () => {
84+
beforeEach(() => {
85+
mocks.applyEnvVariables.mockResolvedValue(undefined)
86+
mocks.checkIfNeedsApiToken.mockResolvedValue(false)
87+
mocks.createCorsOrigin.mockResolvedValue({})
88+
mocks.createToken.mockResolvedValue({key: 'test-token'})
89+
mocks.detectFrameworkRecord.mockResolvedValue(null)
90+
mocks.downloadAndExtractRepo.mockResolvedValue(undefined)
91+
mocks.getDefaultPortForFramework.mockReturnValue(3000)
92+
mocks.getGitHubRawContentUrl.mockReturnValue(
93+
'https://raw.githubusercontent.com/sanity-io/test-template/main/',
94+
)
95+
mocks.getMonoRepo.mockResolvedValue(null)
96+
mocks.mkdir.mockResolvedValue(undefined)
97+
mocks.spinner.mockReturnValue({start: vi.fn().mockReturnThis(), succeed: vi.fn()})
98+
mocks.tryApplyPackageName.mockResolvedValue(undefined)
99+
mocks.updateInitialTemplateMetadata.mockResolvedValue(undefined)
100+
mocks.validateTemplate.mockResolvedValue({isValid: true})
101+
})
102+
84103
afterEach(() => {
85-
vi.clearAllMocks()
104+
vi.resetAllMocks()
86105
})
87106

88107
describe('CORS origin setup', () => {
89108
test('adds CORS origin for a framework port that is not the Sanity default (3333)', async () => {
90-
mocks.getDefaultPortForFramework.mockReturnValue(3000)
91-
92109
await bootstrapRemoteTemplate(baseOpts)
93110

94111
expect(mocks.createCorsOrigin).toHaveBeenCalledOnce()
@@ -134,8 +151,6 @@ describe('bootstrapRemoteTemplate', () => {
134151
})
135152

136153
test('logs newly added CORS origins but not the pre-seeded default port', async () => {
137-
mocks.getDefaultPortForFramework.mockReturnValue(3000)
138-
139154
await bootstrapRemoteTemplate(baseOpts)
140155

141156
const logCalls = vi.mocked(mockOutput.log).mock.calls.flat()
@@ -182,7 +197,6 @@ describe('bootstrapRemoteTemplate', () => {
182197
mocks.checkIfNeedsApiToken.mockImplementation((_path: string, type: string) =>
183198
Promise.resolve(type === 'read'),
184199
)
185-
mocks.getDefaultPortForFramework.mockReturnValue(3000)
186200

187201
await bootstrapRemoteTemplate(baseOpts)
188202

@@ -194,7 +208,6 @@ describe('bootstrapRemoteTemplate', () => {
194208
mocks.checkIfNeedsApiToken.mockImplementation((_path: string, type: string) =>
195209
Promise.resolve(type === 'write'),
196210
)
197-
mocks.getDefaultPortForFramework.mockReturnValue(3000)
198211

199212
await bootstrapRemoteTemplate(baseOpts)
200213

@@ -203,9 +216,6 @@ describe('bootstrapRemoteTemplate', () => {
203216
})
204217

205218
test('does not create any tokens when the template requires none', async () => {
206-
mocks.checkIfNeedsApiToken.mockResolvedValue(false)
207-
mocks.getDefaultPortForFramework.mockReturnValue(3000)
208-
209219
await bootstrapRemoteTemplate(baseOpts)
210220

211221
expect(mocks.createToken).not.toHaveBeenCalled()

0 commit comments

Comments
 (0)