Skip to content

Commit 6b2cc3d

Browse files
committed
Fix issues related to console spies in createSlice.test.ts
1 parent 0fe45f1 commit 6b2cc3d

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

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

Lines changed: 13 additions & 10 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,19 +8,18 @@ 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
15+
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(noop)
2016

2117
beforeEach(() => {
22-
restore = mockConsole(createConsole())
18+
vi.clearAllMocks()
19+
})
20+
21+
afterAll(() => {
22+
vi.restoreAllMocks()
2323
})
2424

2525
describe('when slice is undefined', () => {
@@ -57,8 +57,10 @@ describe('createSlice', () => {
5757
describe('when initial state is undefined', () => {
5858
beforeEach(() => {
5959
vi.stubEnv('NODE_ENV', 'development')
60+
})
6061

61-
return vi.unstubAllEnvs
62+
afterEach(() => {
63+
vi.unstubAllEnvs()
6264
})
6365

6466
it('should throw an error', () => {
@@ -68,7 +70,9 @@ describe('createSlice', () => {
6870
initialState: undefined,
6971
})
7072

71-
expect(getLog().log).toBe(
73+
expect(consoleErrorSpy).toHaveBeenCalledOnce()
74+
75+
expect(consoleErrorSpy).toHaveBeenLastCalledWith(
7276
'You must provide an `initialState` value that is not `undefined`. You may have misspelled `initialState`',
7377
)
7478
})
@@ -397,7 +401,6 @@ describe('createSlice', () => {
397401

398402
beforeEach(() => {
399403
vi.resetModules()
400-
restore = mockConsole(createConsole())
401404
})
402405

403406
afterEach(() => {

0 commit comments

Comments
 (0)