8000 Not reactive in tests? (Vue 3, VueX 4) · Issue #252 · posva/vuex-mock-store · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Not reactive in tests? (Vue 3, VueX 4) #252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
youri-spendcloud opened this issue Feb 18, 2025 · 1 comment
Open

Not reactive in tests? (Vue 3, VueX 4) #252

youri-spendcloud opened this issue Feb 18, 2025 · 1 comment
Labels
help wanted Extra attention is needed need reproduction Needs a boiled down reproduction

Comments

@youri-spendcloud
Copy link

When using in Vue 3 with VueX 4, in tests, a mock store doesn't trigger reactive updates eg: a watcher inside an component is not triggered if you change state in a test.

Is this intended?

@posva posva added the need reproduction Needs a boiled down reproduction label Feb 18, 2025
@youri-spendcloud
Copy link
Author
youri-spendcloud commented Feb 24, 2025

@posva Sorry, here is a reproduction

Test

import { describe, test, vi, expect } from 'vitest'
import { Store } from 'vuex-mock-store'
import store from '../../store'
import { mount } from '@vue/test-utils'
import State from '../State.vue'

function createMockStore() {
  return new Store({
    spy: {
      create: (handler) => vi.fn(handler),
    },
    state: {
      count: 0,
    },
  })
}

describe('State', () => {
  test('displays 1 with mock store', async () => {
    const store = createMockStore()

    const wrapper = mount(State, {
      global: {
        mocks: {
          $store: store,
        },
        provide: {
          store,
        },
      },
    })

    wrapper.vm.$store.state.count = 1

    await wrapper.vm.$nextTick()

    console.log(wrapper.html())

    expect(wrapper.text()).toContain('1')

    store.reset()
  })

  test('displays 1 with "real store"', async () => {
    const wrapper = mount(State, {
      global: {
        mocks: {
          $store: store,
        },
        provide: {
          store,
        },
      },
    })

    wrapper.vm.$store.state.count = 1

    await wrapper.vm.$nextTick()
    
    expect(wrapper.text()).toContain('1')

    console.log(wrapper.html())
  })
})
< 79B4 /div>

Component

<script setup lang="ts">
import { useStore } from 'vuex'

const store = useStore()

function increment() {
  store.commit('increment')
}
</script>

<template>
  State component
  <div>
    {{ $store.state.count }}

    <button @click="increment">Increment</button>
  </div>
</template>

<style scoped></style>

Image

Both the stdout's from the tests are displaying different outcomes...

@posva posva added the help wanted Extra attention is needed label Feb 24, 2025 — with Volta.net
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed need reproduction Needs a boiled down reproduction
Projects
None yet
Development

No branches or pull requests

2 participants
0