Vue-test-utils: vuex actions testing using `toHaveBeenCalledWith` (necessary since setMethods being deprecated)

Created on 11 May 2020  路  4Comments  路  Source: vuejs/vue-test-utils

What problem does this feature solve?

currently when using the recommended process for testing vuex actions within a component (as detailed here), toHaveBeenCalledWith requires a first argument that contains

{
    dispatch: [Function (anonymous)],
    commit: [Function (anonymous)],
    getters: [Object],
    state: [Object],
    rootGetters: [Object],
    rootState: [Object]
},

a feature that automatically injects this, or provides an easy way to add this into an toHaveBeenCalledWith would be helpful

What does the proposed API look like?

//component
this.actionName(argument)

//expect
expect(actions.actionName).toHaveBeenCalledWith(argument) 

// instead of 
expect(actions.actionName).toHaveBeenCalledWith(giantArgumentIncludingContextGettersAndState, argument)

Most helpful comment

Hi! Not sure if there's a better approach, but currently when I test action in isolation I rely on expect.any(Object) for the first argument:

expect(actions.actionName).toHaveBeenCalledWith(
  expect.any(Object), // The Vuex context
  { id: 1, name: 'foo' }
)

All 4 comments

Hi! Not sure if there's a better approach, but currently when I test action in isolation I rely on expect.any(Object) for the first argument:

expect(actions.actionName).toHaveBeenCalledWith(
  expect.any(Object), // The Vuex context
  { id: 1, name: 'foo' }
)

I'm gonna close this up, feel free to reopen or fill in a new issue is the suggested solution does not cover your needs 馃

@afontcu yeah this works, may be worth adding to the documentation on that page I linked above so future developers don't run into the same problem.

That's a good idea! Fancy to open up a PR for docs? 馃槈 @An-AngryBear

Was this page helpful?
0 / 5 - 0 ratings

Related issues

robcresswell picture robcresswell  路  3Comments

alexanderstudte picture alexanderstudte  路  3Comments

lusarz picture lusarz  路  3Comments

38elements picture 38elements  路  3Comments

chenxeed picture chenxeed  路  3Comments