Comments (2)
For what it's worth, we usually don't want to encourage mocking like this too much. It is often overused and gives very little actual confidence to test these little units instead of testing actual store interactions with all the moving parts included.
If you want to do something like this, you could create a store enhancer to enable that behavior for your code base, but I'm not too much in favor of adding more code on our side to enable a feature that we actually want to discourage.
An example enhancer could look like this:
import { configureStore } from "@reduxjs/toolkit";
import { Store, StoreEnhancer } from "redux";
const mockableEnhancer: StoreEnhancer<
{
mockable: Pick<Store, "dispatch" | "getState">;
},
{}
> = (next) => (...args): any => {
const store = next(...args);
const mockable = {
dispatch: store.dispatch,
getState: store.getState
};
return Object.assign({}, store, {
mockable,
getState() {
return mockable.getState();
},
dispatch(action: any) {
return mockable.dispatch(action);
}
});
};
const store = configureStore({
reducer: () => 0,
enhancers: (e) => e.concat(mockableEnhancer)
});
// now we mock
store.mockable.getState = () => "foo";
// and thunks access the mock
store.dispatch((dispatch, getState) => {
console.log(getState());
});
from redux-thunk.
@phryneas I'm in agreement with you, I think the best idea would be to have a store that is not a singleton and only load the slices of it that are required in your tests.
But legacy code sometimes requires getting worst before getting better.
The test is hard, and forcing it to test in a way that people and architectures are not prepared to do could cause discourage people to test at all, or simply tag tests as impossible.
Also, I think there is a very valid scenario where you want to keep your test slim, fast and quick to write, by testing Components Integrated with the store for a happy path and a sad path, but other scenarios can just be covered with a contract test, which this PR helps to achieve.
Also, scenarios where you create a common (internal) component library where part of the API is that your components dispatch actions to be handled in consumers' apps. In the current status will force you to write TestDoubles when a simple could solve the issue.
I think limiting the kind of tests that you prefer to discourage can prevent other kinds of architectures that you are not thinking about right now, which are totally valid and could benefit from this change, and also transition states in big and legacy projects.
The enhancer you propose will require adding a Test code to production code, as the store is exactly the same as it is for production, and adding this enhancer only on test via an env prop could be even worst than my proposed solution.
In my opinion, it's always better to allow to test in any way possible, than force people to test in your opinionated way (which I agree with). Encourage them to test better but never force nor limit.
I think in the industry we suffer too much elitism as regard testing and how is supposed to be tested, or TDD is king and we are creating a toxic environment for those that are outside of the "good practices" (personally follow TDD, and Kent Beck's ideas)
Just as a last argument, if I mock store.getState
or store.dispatch
, I expect to be mocked I don't expect to be cached by a library.
I truly hope you change your mind about this. I know it can be annoying to accept a PR like this from your point of view, but because there are bad cases to use, are also valid and helpful ones.
Cheers!
from redux-thunk.
Related Issues (20)
- Why `redux` module extension is not part of index.d.ts file in package? HOT 1
- Passing `dispatch` instead of calling it.
- Empty payload in PENDING state HOT 1
- Why not next? HOT 1
- types wrong by infer in TS HOT 1
- No type hints when using dispatch HOT 4
- import 'redux-thunk/extend-redux' doesn't work HOT 12
- When running server getting expected errors HOT 2
- ThunkAction is not assignable to parameter of type 'AnyAction'. HOT 46
- TS do not show the error if payload is Partial<> HOT 1
- Can I abort redux thunk dispatches? Without Redux toolkit or aborting the fetch request itself. HOT 1
- Allow wrapped thunk actions for extensibility HOT 2
- When type: "module" is used in root package.json, redux-thunk cannot be applied without crashing HOT 8
- Warning in React-thunk while installing HOT 1
- Query About Redux Thunk HOT 1
- `thunk` as a named export? HOT 3
- redux thunk cannot be use as middleware using Typescript 5.0 or above HOT 1
- during integrating thunk into react-redux project, getting middleware is not a function error. HOT 1
- No default export for redux-thunk HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from redux-thunk.