javascript - Testing the redux store using Jest -
i've set following function configure redux store
const configurestore = (initialstate) => { console.log(initialstate); const store = createstore( rootreducer, initialstate, applymiddleware( thunkmiddleware ) ); console.log(store.getstate()); return store; };
now, running normal app entry point, console logs show, example:
-> initial state = { test: "test" } -> (store gets created) -> store state = { test: "test" }
thats good, , expected. i'm trying confirm behaviour following jest test
it("should set supplied initial state", () => { const initialstate = { teststate: "test" }; const store = configurestore(initialstate); expect(store.getstate()).toequal({ teststate: "test" }); });
this test failing, store.getstate() returning undefined. can see console logs configurestore function, , showing:
-> initial state = { test: "test" } -> (store gets created) -> store state = undefined
why behaviour different? jest mocking createstore function? i've read jest no longer auto-mocks dependencies, why case? i'm new jest unit testing, light can shed on issue appreciated.
edit: realise i'm testing redux function, unnecessary. regardless, i'd understand testing behaviour that's causing issue.
second edit: writing own createstore function follows makes test pass:
const createstore = (reducer, initstate, middleware) => { return { getstate: () => (initstate) }; };
but still doesn't answer why redux createstore function behaving differently in test environment.
all actions go through your:
rootreducer
including action fired when redux instantiates e.g. initial action has type '@@redux/init'.
so if ever unexpected state check reducer behaving expected.
as found, reducer not behaving expected mock instance not being preserved across tests.
Comments
Post a Comment