question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to use `direct-vuex`

See original GitHub issue

Describe the bug Because of the way store is initialized, I’m unable to use direct-vuex stores. Short summary, direct-vuex is a “wrapper” for vuex to provide typing for store. Thanks to it you can do this.$store.dispatch.myAction(myPayload) happily.

To Reproduce Steps to reproduce the behavior:

  • create store via createDirectStore from direct-vuex.
  • result of this function is already a Vuex.Store so initialization in render fails (getters should be function)

Expected behavior

I can use direct-vuex stores.

I’m skipping few sections as I already know the solution (or at least where problem originates):

Problematic code here.

if (store) {
    const Vuex = require('vuex')
    localVue.use(Vuex)
    vuexStore = new Vuex.Store(store)
  }

I do realize why it’s written this way.

Question to the maintainers is, how do you want to implement it. Or whenever you want to implement it at all! There could be boolean switch in additionalOptions to skip store initialization (like initializeStore, by default set to true, to not break API), or some other options like directVuexStore which you can use to add to the Vue instance instead of plain store. I’m happy to help with this in any way you need.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
afontcucommented, May 27, 2020

Isn’t the following an option? I’ve never used direct-vuex before, so I might be wrong:

const increment = jest.fn();
const decrement = jest.fn();

const { getByLabelText } = render(
  ExampleComponent,
  {},  // <-- without importing and passing localVue
  (vue) => {
    vue.use(Vuex);

    const { store } = createDirectStore({
      state: () => {
        return { counter: 0 };
      },
      getters: {
        counter: (state) => state.counter,
      },
      mutations: {
        increment,
        decrement,
      },
    });

    return { store: store.original };
  }
);
1reaction
kosciak9commented, May 27, 2020

While I understand your point of view and also dislike this way of testing, sometimes you have to draw a line between units that you’re testing, even if those units are quite huge.

In our case we abstracted any communication with APIs to Vuex, and provide just one or two actions that will check input and refetch accordingly. UI components are decoupled from anything besides looking at fetchState and actual data. That’s why mocked stores are mostly just one getter and one action. It’s a more complicated props setup, that allows us to do this.$store.direct stuff.

I’m not sure if that’s the greatest approach but it allows us to think in smaller blocks (UI, Server) and separate logic.

We do have tests that use our “real” store the way user is using them, but such a big test failing won’t give you quick insight into what have you actually broke.

It’s like they say in Python’s Zen: Special cases aren’t special enough to break the rules. Although practicality beats purity.

I feel practicality here beats purity of DOM testing. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript - `direct-vuex` has error when defining Getters
I'm using direct-vuex package to define the store for Vuejs+Typescript. I have this error when trying to define Getters:.
Read more >
[Solved]-`direct-vuex` has error when defining Getters
Coding example for the question `direct-vuex` has error when defining Getters: "Function implicitly has return type 'any'..."-Vue.js.
Read more >
Unable to sign into the Direct TV app despite being able to ...
First of all make sure you are using the DirecTV app and not DIRECTV STREAM. Just mentioning as the recent rebrand of the...
Read more >
Vue 3 — The New Store. Is the Vuex Store still necessary in…
That is also why I recommend you to use Typescript. I've tried to build enterprise software with JS only and it is doomed...
Read more >
Does anyone else feel typescript integration is super hacky
Eslint and vetur works as expected and for vuex I use direct-vuex which is a package ... Can't stop seeing stars I'm catching...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found