Skip to content

Commit 9e24958

Browse files
authored
Merge pull request #2461 from dannielss/pr/issue-2459
2 parents d00e08f + 661dabd commit 9e24958

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

packages/toolkit/src/createSlice.ts

+10
Original file line numberDiff line numberDiff 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

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

+23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
import type { PayloadAction } from '@reduxjs/toolkit'
22
import { createSlice, createAction } from '@reduxjs/toolkit'
3+
import {
4+
mockConsole,
5+
createConsole,
6+
getLog,
7+
} from 'console-testing-library/pure'
38

49
describe('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: {

0 commit comments

Comments
 (0)