reduxjs / redux-mock-store

A mock store for testing Redux async action creators and middleware.
MIT License
2.5k stars 147 forks source link

Types for dispatch with thunk are incorrect #148

Open ChuckJonas opened 6 years ago

ChuckJonas commented 6 years ago

If I load you async example into a project, i get the following error on return store.dispatch(fetchData())

Argument of type '(dispatch: any) => Promise<any>' is not assignable to parameter of type 'Action'.
  Property 'type' is missing in type '(dispatch: any) => Promise<any>'.

Using Redux 4.0

flq commented 6 years ago

Can confirm, with redux 4 types ain’t working.

martpie commented 6 years ago

I can confirm it too.

flq commented 6 years ago

Since it doesn’t look lile this package is maintained we switched to some custom code - at the end we didn’t need full store semantics in our action tests, but just a working dispatcher and a way to collect dispatched actions

chungnho commented 6 years ago

This is probably a problem with the @types/redux-mock-store package rather than this one. https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/redux-mock-store

That package lists redux@3.6.0 as a dependency and since there were significant type changes between redux@3 and redux@4 new types will need to be submitted to DefinitelyTyped that are compatible with redux@4

chungnho commented 6 years ago

Just submitted this update which should allow you to add the ThunkDispatch signature when creating the store: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/26732

When that is merged and released you can do something like:

const middlewares: Array<Middleware> = [thunk]
type DispatchExts = ThunkDispatch<RootState, undefined, AnyAction>
const mockStoreCreator: MockStoreCreator<RootState, DispatchExts> = 
    createMockStore<RootState, DispatchExts>(middlewares)
const store: MockStoreEnhanced<RootState, DispatchExts> = mockStoreCreator({})
mpcen commented 6 years ago

@chungnho Thank you sir. Took me 5 hours to find this.

kgajowy commented 5 years ago

👍 to put this in the docs

jednano commented 5 years ago

I made a TypeScript fork of this library and resolved some open issues at @jedmao/redux-mock-store.