File tree 2 files changed +33
-0
lines changed
2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -262,6 +262,16 @@ export function createSlice<
262
262
if ( ! name ) {
263
263
throw new Error ( '`name` is a required option for createSlice' )
264
264
}
265
+
266
+ if (
267
+ typeof process !== 'undefined' &&
268
+ process . env . NODE_ENV === 'development'
269
+ ) {
270
+ if ( options . initialState === undefined ) {
271
+ console . error ( 'You must provide an `initialState` value that is not `undefined`. You may have misspelled `initialState`' )
272
+ }
273
+ }
274
+
265
275
const initialState =
266
276
typeof options . initialState == 'function'
267
277
? options . initialState
Original file line number Diff line number Diff line change 1
1
import type { PayloadAction } from '@reduxjs/toolkit'
2
2
import { createSlice , createAction } from '@reduxjs/toolkit'
3
+ import {
4
+ mockConsole ,
5
+ createConsole ,
6
+ getLog ,
7
+ } from 'console-testing-library/pure'
3
8
4
9
describe ( 'createSlice' , ( ) => {
10
+ let restore : ( ) => void
11
+
12
+ beforeEach ( ( ) => {
13
+ restore = mockConsole ( createConsole ( ) )
14
+ } )
15
+
5
16
describe ( 'when slice is undefined' , ( ) => {
6
17
it ( 'should throw an error' , ( ) => {
7
18
expect ( ( ) =>
@@ -34,6 +45,18 @@ describe('createSlice', () => {
34
45
} )
35
46
} )
36
47
48
+ describe ( 'when initial state is undefined' , ( ) => {
49
+ it ( 'should throw an error' , ( ) => {
50
+ createSlice ( {
51
+ name : 'test' ,
52
+ reducers : { } ,
53
+ initialState : undefined ,
54
+ } )
55
+
56
+ expect ( getLog ( ) . log ) . toBe ( 'You must provide an `initialState` value that is not `undefined`. You may have misspelled `initialState`' )
57
+ } )
58
+ } )
59
+
37
60
describe ( 'when passing slice' , ( ) => {
38
61
const { actions, reducer, caseReducers } = createSlice ( {
39
62
reducers : {
You can’t perform that action at this time.
0 commit comments