Async Action Creator Test Structure
Learn the code structure required to test action creators.
Since async action creators might contain different flows based on the result of the async action, it is best to put them into their own describe()
blocks in the tests. This will also allow us to easily create a fresh “mock store” for each of the test cases using Jest’s beforeEach()
method:
// Structure of an async test block
describe('actions/recipes', () => {
let store;
beforeEach(() => store = mockStore({}));
it('fetchRecipe');
...
});
Our mock store gets automatically recreated before each iteration of the tests, clearing any actions cached from the previous run.
Get hands-on with 1400+ tech skills courses.