Skip to content

Commit 4e94fcb

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 7de16ef commit 4e94fcb

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
@@ -967,14 +967,16 @@ describe('hooks tests', () => {
967967
})
968968

969969
describe('Hook middleware requirements', () => {
970-
let mock: MockInstance
970+
const consoleErrorSpy = vi
971+
.spyOn(console, 'error')
972+
.mockImplementation(noop)
971973

972-
beforeEach(() => {
973-
mock = vi.spyOn(console, 'error').mockImplementation(() => {})
974+
afterEach(() => {
975+
consoleErrorSpy.mockClear()
974976
})
975977

976-
afterEach(() => {
977-
mock.mockReset()
978+
afterAll(() => {
979+
consoleErrorSpy.mockRestore()
978980
})
979981

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

0 commit comments

Comments
 (0)