Skip to content

Commit ac1151c

Browse files
committed
Fix issues related to console spies in createSlice.test.ts
1 parent e0d88e0 commit ac1151c

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { noop } from '@internal/listenerMiddleware/utils'
12
import type { PayloadAction, WithSlice } from '@reduxjs/toolkit'
23
import {
34
asyncThunkCreator,
@@ -7,21 +8,10 @@ import {
78
createAction,
89
createSlice,
910
} from '@reduxjs/toolkit'
10-
import {
11-
createConsole,
12-
getLog,
13-
mockConsole,
14-
} from 'console-testing-library/pure'
1511

1612
type CreateSlice = typeof createSlice
1713

1814
describe('createSlice', () => {
19-
let restore: () => void
20-
21-
beforeEach(() => {
22-
restore = mockConsole(createConsole())
23-
})
24-
2515
describe('when slice is undefined', () => {
2616
it('should throw an error', () => {
2717
expect(() =>
@@ -55,10 +45,19 @@ describe('createSlice', () => {
5545
})
5646

5747
describe('when initial state is undefined', () => {
48+
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(noop)
49+
5850
beforeEach(() => {
5951
vi.stubEnv('NODE_ENV', 'development')
6052

61-
return vi.unstubAllEnvs
53+
return () => {
54+
vi.unstubAllEnvs()
55+
consoleErrorSpy.mockClear()
56+
}
57+
})
58+
59+
afterAll(() => {
60+
consoleErrorSpy.mockRestore()
6261
})
6362

6463
it('should throw an error', () => {
@@ -68,7 +67,9 @@ describe('createSlice', () => {
6867
initialState: undefined,
6968
})
7069

71-
expect(getLog().log).toBe(
70+
expect(consoleErrorSpy).toHaveBeenCalledOnce()
71+
72+
expect(consoleErrorSpy).toHaveBeenLastCalledWith(
7273
'You must provide an `initialState` value that is not `undefined`. You may have misspelled `initialState`',
7374
)
7475
})
@@ -397,7 +398,6 @@ describe('createSlice', () => {
397398

398399
beforeEach(() => {
399400
vi.resetModules()
400-
restore = mockConsole(createConsole())
401401
})
402402

403403
afterEach(() => {

0 commit comments

Comments
 (0)