File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -262,6 +262,16 @@ export function createSlice<
262262 if ( ! name ) {
263263 throw new Error ( '`name` is a required option for createSlice' )
264264 }
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+
265275 const initialState =
266276 typeof options . initialState == 'function'
267277 ? options . initialState
Original file line number Diff line number Diff line change 11import type { PayloadAction } from '@reduxjs/toolkit'
22import { createSlice , createAction } from '@reduxjs/toolkit'
3+ import {
4+ mockConsole ,
5+ createConsole ,
6+ getLog ,
7+ } from 'console-testing-library/pure'
38
49describe ( 'createSlice' , ( ) => {
10+ let restore : ( ) => void
11+
12+ beforeEach ( ( ) => {
13+ restore = mockConsole ( createConsole ( ) )
14+ } )
15+
516 describe ( 'when slice is undefined' , ( ) => {
617 it ( 'should throw an error' , ( ) => {
718 expect ( ( ) =>
@@ -34,6 +45,18 @@ describe('createSlice', () => {
3445 } )
3546 } )
3647
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+
3760 describe ( 'when passing slice' , ( ) => {
3861 const { actions, reducer, caseReducers } = createSlice ( {
3962 reducers : {
You can’t perform that action at this time.
0 commit comments