1+ import { noop } from '@internal/listenerMiddleware/utils'
12import type { PayloadAction , WithSlice } from '@reduxjs/toolkit'
23import {
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
1612type CreateSlice = typeof createSlice
1713
1814describe ( '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