Skip to content

Commit 8612882

Browse files
authored
chore(tests): reduce stderr output while running tests (#576)
1 parent 89de482 commit 8612882

File tree

7 files changed

+26
-8
lines changed

7 files changed

+26
-8
lines changed

packages/core/src/query/queryStore.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe('queryStore', () => {
2020
let instance: SanityInstance
2121
let liveEvents: Subject<LiveEvent>
2222
let fetch: SanityClient['observable']['fetch']
23+
let listen: SanityClient['observable']['listen']
2324
// Mock data for testing
2425
const mockData = {
2526
movies: [
@@ -37,6 +38,8 @@ describe('queryStore', () => {
3738
of({result: mockData.movies, syncTags: []}).pipe(delay(0)),
3839
) as SanityClient['observable']['fetch']
3940

41+
listen = vi.fn().mockReturnValue(of(mockData.movies))
42+
4043
liveEvents = new Subject<LiveEvent>()
4144

4245
const events = vi.fn().mockReturnValue(liveEvents) as SanityClient['live']['events']
@@ -47,7 +50,7 @@ describe('queryStore', () => {
4750
observable: of({
4851
config,
4952
live: {events},
50-
observable: {fetch},
53+
observable: {fetch, listen},
5154
} as SanityClient),
5255
} as StateSource<SanityClient>)
5356
})

packages/core/src/releases/releasesStore.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ vi.mock('../utils/listenQuery', () => ({
1717
}))
1818

1919
// Mock console.error to prevent test runner noise and allow verification
20-
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
20+
let consoleErrorSpy: ReturnType<typeof vi.spyOn>
2121

2222
describe('releasesStore', () => {
2323
let instance: SanityInstance
2424
const mockClient = {} as SanityClient
2525

2626
beforeEach(() => {
2727
vi.resetAllMocks()
28-
consoleErrorSpy.mockClear()
28+
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
2929

3030
instance = createSanityInstance({projectId: 'test', dataset: 'test'})
3131

@@ -36,6 +36,7 @@ describe('releasesStore', () => {
3636

3737
afterEach(() => {
3838
instance.dispose()
39+
consoleErrorSpy.mockRestore()
3940
})
4041

4142
it('should set active releases state when listenQuery succeeds', async () => {
@@ -63,6 +64,8 @@ describe('releasesStore', () => {
6364

6465
expect(state.getCurrent()).toEqual(mockReleases.reverse())
6566
expect(consoleErrorSpy).not.toHaveBeenCalled()
67+
68+
vi.useRealTimers()
6669
})
6770

6871
it('should update active releases state when listenQuery emits new data', async () => {

packages/react/src/components/SanityApp.test.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ describe('SanityApp', () => {
170170

171171
it('redirects to core if not inside iframe and not local url', async () => {
172172
const originalLocation = window.location
173+
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
173174

174175
const mockLocation = {
175176
replace: vi.fn(),
@@ -203,6 +204,7 @@ describe('SanityApp', () => {
203204
value: originalLocation,
204205
writable: true,
205206
})
207+
consoleLogSpy.mockRestore()
206208
})
207209

208210
it('does not redirect to core if not inside iframe and local url', async () => {

packages/react/src/context/ResourceProvider.test.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {type SanityConfig, type SanityInstance} from '@sanity/sdk'
2-
import {render, screen} from '@testing-library/react'
2+
import {act, render, screen} from '@testing-library/react'
33
import {StrictMode, use, useEffect} from 'react'
44
import {describe, expect, it} from 'vitest'
55

@@ -36,7 +36,7 @@ describe('ResourceProvider', () => {
3636
expect(screen.getByTestId('test-child')).toBeInTheDocument()
3737
})
3838

39-
it('shows fallback during loading', () => {
39+
it('shows fallback during loading', async () => {
4040
const {promise, resolve} = promiseWithResolvers()
4141
function SuspendingChild(): React.ReactNode {
4242
throw promise
@@ -49,7 +49,9 @@ describe('ResourceProvider', () => {
4949
)
5050

5151
expect(screen.getByTestId('fallback')).toBeInTheDocument()
52-
resolve()
52+
act(() => {
53+
resolve()
54+
})
5355
})
5456

5557
it('creates root instance when no parent context exists', async () => {
@@ -138,7 +140,7 @@ describe('ResourceProvider', () => {
138140
expect(instance?.isDisposed()).toBe(false)
139141
})
140142

141-
it('uses default fallback when none provided', () => {
143+
it('uses default fallback when none provided', async () => {
142144
const {promise, resolve} = promiseWithResolvers()
143145
function SuspendingChild(): React.ReactNode {
144146
throw promise
@@ -152,6 +154,8 @@ describe('ResourceProvider', () => {
152154
)
153155

154156
expect(screen.getByText(/Warning: No fallback provided/)).toBeInTheDocument()
155-
resolve()
157+
act(() => {
158+
resolve()
159+
})
156160
})
157161
})

packages/react/src/hooks/dashboard/useManageFavorite.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ describe('useManageFavorite', () => {
172172

173173
it('should throw error during favorite/unfavorite actions', async () => {
174174
const errorMessage = 'Failed to update favorite status'
175+
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
175176

176177
mockFetch.mockImplementation(() => {
177178
throw new Error(errorMessage)
@@ -191,6 +192,7 @@ describe('useManageFavorite', () => {
191192
})
192193

193194
expect(resolveFavoritesState).not.toHaveBeenCalled()
195+
consoleErrorSpy.mockRestore()
194196
})
195197

196198
it('should throw error when studio resource is missing projectId or dataset', () => {

packages/react/src/hooks/dashboard/useNavigateToStudioDocument.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,12 @@ describe('useNavigateToStudioDocument', () => {
7575
})
7676

7777
it('does not send message when no workspace is found', () => {
78+
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
7879
mockWorkspacesByProjectIdAndDataset = {}
7980
const {result} = renderHook(() => useNavigateToStudioDocument(mockDocumentHandle))
8081
result.current.navigateToStudioDocument()
8182
expect(mockSendMessage).not.toHaveBeenCalled()
83+
consoleSpy.mockRestore()
8284
})
8385

8486
it('warns when multiple workspaces are found', () => {

packages/react/src/hooks/dashboard/useRecordDocumentHistoryEvent.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ describe('useRecordDocumentHistoryEvent', () => {
4545
})
4646

4747
it('should handle errors when sending messages', () => {
48+
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
4849
mockSendMessage.mockImplementation(() => {
4950
throw new Error('Failed to send message')
5051
})
5152

5253
const {result} = renderHook(() => useRecordDocumentHistoryEvent(mockDocumentHandle))
5354

5455
expect(() => result.current.recordEvent('viewed')).toThrow('Failed to send message')
56+
consoleErrorSpy.mockRestore()
5557
})
5658

5759
it('should throw error when resourceId is missing for non-studio resources', () => {

0 commit comments

Comments
 (0)