Skip to content

Commit a474db3

Browse files
authored
Actually throw an error when there's no initialState
1 parent 89bff79 commit a474db3

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

packages/toolkit/src/createSlice.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ export function createSlice<
268268
process.env.NODE_ENV === 'development'
269269
) {
270270
if(options.initialState === undefined) {
271-
console.error('initial state must be different of undefined')
271+
throw new Error('You must provide an `initialState` value that is not `undefined`. You may have misspelled `initialState`')
272272
}
273273
}
274274

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('createSlice', () => {
1313
restore = mockConsole(createConsole())
1414
})
1515

16-
describe('when slice is undefined', () => {
16+
describe('when name is undefined', () => {
1717
it('should throw an error', () => {
1818
expect(() =>
1919
// @ts-ignore
@@ -29,7 +29,7 @@ describe('createSlice', () => {
2929
})
3030
})
3131

32-
describe('when slice is an empty string', () => {
32+
describe('when name is an empty string', () => {
3333
it('should throw an error', () => {
3434
expect(() =>
3535
createSlice({
@@ -47,17 +47,17 @@ describe('createSlice', () => {
4747

4848
describe('when initial state is undefined', () => {
4949
it('should throw an error', () => {
50-
createSlice({
51-
name: 'test',
52-
reducers: {},
53-
initialState: undefined,
54-
})
55-
56-
expect(getLog().log).toBe('initial state must be different of undefined')
50+
expect(() =>
51+
// @ts-ignore
52+
createSlice({
53+
name: 'test',
54+
reducers: {},
55+
})
56+
).toThrowError('You must provide an `initialState` value that is not `undefined`. You may have misspelled `initialState`')
5757
})
5858
})
5959

60-
describe('when passing slice', () => {
60+
describe('when passing name', () => {
6161
const { actions, reducer, caseReducers } = createSlice({
6262
reducers: {
6363
increment: (state) => state + 1,

0 commit comments

Comments
 (0)