Skip to content

Commit 332e7ee

Browse files
committed
test: improve beforeEach cleanup in telemetry tests
Remove vi.spyOn that re-implements getCliToken with a custom function. Instead, use vi.resetModules() + dynamic import in beforeEach to get a fresh module and reset the module-level cachedToken between tests. This tests the actual implementation rather than a spy, and each test gets a clean slate for the token cache.
1 parent 5ab6b9e commit 332e7ee

File tree

1 file changed

+11
-25
lines changed

1 file changed

+11
-25
lines changed

packages/@sanity/cli-core/src/config/__tests__/cliToken.test.ts

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,21 @@
11
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'
22

33
import {getCliUserConfig} from '../../services/cliUserConfig'
4-
import * as cliTokenModule from '../../services/getCliToken'
54

65
vi.mock('../../services/cliUserConfig', () => ({
76
getCliUserConfig: vi.fn(),
87
}))
98

109
describe('getCliToken', () => {
1110
const originalEnv = process.env
12-
let cachedToken: string | undefined
11+
let getCliToken: () => Promise<string | undefined>
1312

14-
beforeEach(() => {
13+
beforeEach(async () => {
1514
process.env = {...originalEnv}
1615
vi.clearAllMocks()
1716
vi.resetModules()
18-
cachedToken = undefined
19-
vi.spyOn(cliTokenModule, 'getCliToken').mockImplementation(async () => {
20-
if (cachedToken !== undefined) {
21-
return cachedToken
22-
}
23-
24-
const token = process.env.SANITY_AUTH_TOKEN
25-
if (token) {
26-
cachedToken = token.trim()
27-
return cachedToken
28-
}
29-
30-
cachedToken = await getCliUserConfig('authToken')
31-
return cachedToken
32-
})
17+
const module = await import('../../services/getCliToken.js')
18+
getCliToken = module.getCliToken
3319
})
3420

3521
afterEach(() => {
@@ -39,7 +25,7 @@ describe('getCliToken', () => {
3925

4026
it('should return token from environment variable', async () => {
4127
process.env.SANITY_AUTH_TOKEN = 'test-token'
42-
const token = await cliTokenModule.getCliToken()
28+
const token = await getCliToken()
4329
expect(token).toBe('test-token')
4430
expect(getCliUserConfig).not.toHaveBeenCalled()
4531
})
@@ -48,7 +34,7 @@ describe('getCliToken', () => {
4834
delete process.env.SANITY_AUTH_TOKEN
4935
vi.mocked(getCliUserConfig).mockResolvedValueOnce('config-token')
5036

51-
const token = await cliTokenModule.getCliToken()
37+
const token = await getCliToken()
5238
expect(token).toBe('config-token')
5339
expect(getCliUserConfig).toHaveBeenCalledWith('authToken')
5440
})
@@ -57,17 +43,17 @@ describe('getCliToken', () => {
5743
delete process.env.SANITY_AUTH_TOKEN
5844
vi.mocked(getCliUserConfig).mockResolvedValueOnce(undefined)
5945

60-
const token = await cliTokenModule.getCliToken()
46+
const token = await getCliToken()
6147
expect(token).toBeUndefined()
6248
expect(getCliUserConfig).toHaveBeenCalledWith('authToken')
6349
})
6450

6551
it('should cache the token from environment variable', async () => {
6652
process.env.SANITY_AUTH_TOKEN = 'cached-env-token'
6753

68-
const firstCall = await cliTokenModule.getCliToken()
54+
const firstCall = await getCliToken()
6955
process.env.SANITY_AUTH_TOKEN = 'new-token'
70-
const secondCall = await cliTokenModule.getCliToken()
56+
const secondCall = await getCliToken()
7157

7258
expect(firstCall).toBe('cached-env-token')
7359
expect(secondCall).toBe('cached-env-token')
@@ -78,8 +64,8 @@ describe('getCliToken', () => {
7864
delete process.env.SANITY_AUTH_TOKEN
7965
vi.mocked(getCliUserConfig).mockResolvedValueOnce('cached-config-token')
8066

81-
const firstCall = await cliTokenModule.getCliToken()
82-
const secondCall = await cliTokenModule.getCliToken()
67+
const firstCall = await getCliToken()
68+
const secondCall = await getCliToken()
8369

8470
expect(firstCall).toBe('cached-config-token')
8571
expect(secondCall).toBe('cached-config-token')

0 commit comments

Comments
 (0)