Skip to content

Commit 9ea7b2f

Browse files
committed
Fix issue with console spy inside buildHooks.test.tsx
- `.mockReset()` should not be called in spies since it calls `.mockClear()` and returns the implementation to its **initial** form. In this case it was silencing some of the `act` related warnings emitted by `@testing-library/react` which needed to be resolved since they were calling issues. So `.mockReset()` calls on spies need to be changed to `.mockRestore()` calls since `.mockRestore()` restores the implementation to its **original** form.
1 parent 1e02320 commit 9ea7b2f

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

packages/toolkit/src/query/tests/buildHooks.test.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { noop } from '@internal/listenerMiddleware/utils'
12
import type { SubscriptionOptions } from '@internal/query/core/apiState'
23
import type { SubscriptionSelectors } from '@internal/query/core/buildMiddleware/types'
34
import { server } from '@internal/query/tests/mocks/server'
@@ -33,7 +34,6 @@ import {
3334
import userEvent from '@testing-library/user-event'
3435
import { HttpResponse, http } from 'msw'
3536
import { useEffect, useState } from 'react'
36-
import type { MockInstance } from 'vitest'
3737

3838
// Just setup a temporary in-memory counter for tests that `getIncrementedAmount`.
3939
// This can be used to test how many renders happen due to data changes or
@@ -1120,14 +1120,16 @@ describe('hooks tests', () => {
11201120
})
11211121

11221122
describe('Hook middleware requirements', () => {
1123-
let mock: MockInstance
1123+
const consoleErrorSpy = vi
1124+
.spyOn(console, 'error')
1125+
.mockImplementation(noop)
11241126

1125-
beforeEach(() => {
1126-
mock = vi.spyOn(console, 'error').mockImplementation(() => {})
1127+
afterEach(() => {
1128+
consoleErrorSpy.mockClear()
11271129
})
11281130

1129-
afterEach(() => {
1130-
mock.mockReset()
1131+
afterAll(() => {
1132+
consoleErrorSpy.mockRestore()
11311133
})
11321134

11331135
test('Throws error if middleware is not added to the store', async () => {

0 commit comments

Comments
 (0)