Skip to content

Commit 4beb3f6

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 4ab36d0 commit 4beb3f6

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'
@@ -32,7 +33,6 @@ import {
3233
import userEvent from '@testing-library/user-event'
3334
import { HttpResponse, http } from 'msw'
3435
import { useEffect, useState } from 'react'
35-
import type { MockInstance } from 'vitest'
3636

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

961961
describe('Hook middleware requirements', () => {
962-
let mock: MockInstance
962+
const consoleErrorSpy = vi
963+
.spyOn(console, 'error')
964+
.mockImplementation(noop)
963965

964-
beforeEach(() => {
965-
mock = vi.spyOn(console, 'error').mockImplementation(() => {})
966+
afterEach(() => {
967+
consoleErrorSpy.mockClear()
966968
})
967969

968-
afterEach(() => {
969-
mock.mockReset()
970+
afterAll(() => {
971+
consoleErrorSpy.mockRestore()
970972
})
971973

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

0 commit comments

Comments
 (0)