Skip to content

Commit ca397f3

Browse files
committed
Fix issues related to console spies in immutableStateInvariantMiddleware.test.ts
1 parent b54aaf1 commit ca397f3

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

packages/toolkit/src/tests/immutableStateInvariantMiddleware.test.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1+
import { trackForMutations } from '@internal/immutableStateInvariantMiddleware'
2+
import { noop } from '@internal/listenerMiddleware/utils'
13
import type {
2-
Store,
3-
MiddlewareAPI,
44
ImmutableStateInvariantMiddlewareOptions,
55
Middleware,
6+
MiddlewareAPI,
7+
Store,
68
} from '@reduxjs/toolkit'
79
import {
810
createImmutableStateInvariantMiddleware,
911
isImmutableDefault,
1012
} from '@reduxjs/toolkit'
1113

12-
import { trackForMutations } from '@internal/immutableStateInvariantMiddleware'
13-
import {
14-
mockConsole,
15-
createConsole,
16-
getLog,
17-
} from 'console-testing-library/pure'
18-
1914
type MWNext = Parameters<ReturnType<Middleware>>[0]
2015

2116
describe('createImmutableStateInvariantMiddleware', () => {
@@ -147,14 +142,20 @@ describe('createImmutableStateInvariantMiddleware', () => {
147142

148143
const dispatch = middleware({ warnAfter: 4 })(next)
149144

150-
const restore = mockConsole(createConsole())
145+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(noop)
146+
151147
try {
152148
dispatch({ type: 'SOME_ACTION' })
153-
expect(getLog().log).toMatch(
154-
/^ImmutableStateInvariantMiddleware took \d*ms, which is more than the warning threshold of 4ms./,
149+
150+
expect(consoleWarnSpy).toHaveBeenCalledOnce()
151+
152+
expect(consoleWarnSpy).toHaveBeenLastCalledWith(
153+
expect.stringMatching(
154+
/^ImmutableStateInvariantMiddleware took \d*ms, which is more than the warning threshold of 4ms./,
155+
),
155156
)
156157
} finally {
157-
restore()
158+
consoleWarnSpy.mockRestore()
158159
}
159160
})
160161

@@ -167,12 +168,14 @@ describe('createImmutableStateInvariantMiddleware', () => {
167168

168169
const dispatch = middleware({ warnAfter: 4 })(next)
169170

170-
const restore = mockConsole(createConsole())
171+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(noop)
172+
171173
try {
172174
dispatch({ type: 'SOME_ACTION' })
173-
expect(getLog().log).toEqual('')
175+
176+
expect(consoleWarnSpy).not.toHaveBeenCalled()
174177
} finally {
175-
restore()
178+
consoleWarnSpy.mockRestore()
176179
}
177180
})
178181
})

0 commit comments

Comments
 (0)