bencodezen / vue-enterprise-boilerplate

An ever-evolving, very opinionated architecture and dev environment for new Vue SPA projects using Vue CLI.
7.78k stars 1.32k forks source link

Assert a Vuex Action Calls a Mutation #153

Closed GazEdge closed 5 years ago

GazEdge commented 5 years ago

In some of the vuex module unit tests, when an action is tested, an assertion is run on store.state

Would it be better to assert that the action calls the correct mutator? Otherwise the action unit tests are testing too much (they are also testing that the mutations behave correctly).

Additionally, i ran into an issue where someone had modified the state directly in an action. I thought vue warned against this, but turns out the warning doesnt appear during unit tests. Testing that an action calls a mutation would mitigate against accidentally modifying the state directly in the action.

marceloavf commented 5 years ago

How did you get it done @GazEdge ? I'm trying to test if an action calls a mutation but no success at all.

I was able to make it work like this, but doesn't know if it is correct

it('actions.resetSelected do not commit when no component selected', () => {
      const commit = jest.fn()
      notificationCreatorModule.actions.resetSelected({
        commit,
        state: { selected: { id: null } },
      })
      expect(commit).not.toHaveBeenCalled()
    })
chrisvfritz commented 5 years ago

Would it be better to assert that the action calls the correct mutator?

That's a good idea. I'll update the tests shortly.

i ran into an issue where someone had modified the state directly in an action. I thought vue warned against this, but turns out the warning doesnt appear during unit tests.

In some projects, I've implemented something like this in the Jest setup file for console.error (and sometimes console.warn), but I have mixed feelings on whether it's a good idea here. I've been on some projects where a 3rd-party library emits a lot of warnings/errors even when nothing's wrong. Maybe I'll emit an error, but with instructions on how to selectively whitelist some errors/warnings as OK so that people don't get stuck.

chrisvfritz commented 5 years ago

I just updated Jest to fail on state mutations outside of mutations - and also any other time console.error or console.warn is called. However, I decided not to assert that the correct mutator is called, for a few reasons: