diff --git a/src/devTools.js b/src/devTools.js index f57e9bc1..4704438a 100644 --- a/src/devTools.js +++ b/src/devTools.js @@ -203,6 +203,7 @@ function unliftState(liftedState) { * Unlifts the DevTools store to act like the app's store. */ function unliftStore(liftedStore, reducer) { + let lastDefinedState; return { ...liftedStore, devToolsStore: liftedStore, @@ -211,7 +212,11 @@ function unliftStore(liftedStore, reducer) { return action; }, getState() { - return unliftState(liftedStore.getState()); + const state = unliftState(liftedStore.getState()); + if (state !== undefined) { + lastDefinedState = state; + } + return lastDefinedState; }, getReducer() { return reducer; diff --git a/test/devTools.spec.js b/test/devTools.spec.js index b7d2d65c..20a523e4 100644 --- a/test/devTools.spec.js +++ b/test/devTools.spec.js @@ -197,6 +197,22 @@ describe('devTools', () => { expect(spy.calls[0].arguments[0]).toMatch( /ReferenceError/ ); + + spy.restore(); + }); + + it('returns the last non-undefined state from getState', () => { + let spy = spyOn(console, 'error'); + + store.dispatch({ type: 'INCREMENT' }); + store.dispatch({ type: 'DECREMENT' }); + store.dispatch({ type: 'INCREMENT' }); + store.dispatch({ type: 'INCREMENT' }); + expect(store.getState()).toBe(2); + + store.replaceReducer(counterWithBug); + expect(store.getState()).toBe(1); + spy.restore(); }); });