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.

Vue component computed doesn't listen to $dirty changes when testing with jest

See original GitHub issue

I couldn’t reproduce this on jsfiddle because of the tests, so I created a repository.

https://github.com/phiter/vuelidate-jest-issue

Basically, I have this computed property

validations: {
    name: {
      required,
    },
  },
computed: {
    nameErrors() {
      const errors = [];
      
      if (!this.$v.name.$dirty) return errors;
      console.log('is dirty');

      if (!this.$v.name.required) errors.push('Required field');
      
      return errors;
    },
  },

and this input

<input type="text" v-model="$v.name.$model"/>

When I update the input, I can access wrapper.vm.$v.name.$dirty from jest and it says it’s true. It works when I run the app trough npm run serve, but computed doesn’t listen to $dirty being true when I try to get it.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
dobromir-hristovcommented, Aug 12, 2019

Hey mate, sorry it took me 2 days, was on a short vacation. So, vue-test-utils does some magic behind the scenes to make allot of actions sync, that however does not work great with Vuelidate, because of the way it works behind the scenes (very complicated to explain).

So to make it work, mount the component with sync: false, this makes it work as if all events are async (as they are). You do have to await a bit more here and there, but not a big deal 😃

return mount(App, { localVue, sync: false }); Then you can await a tick, or flushPromises or whatever you desire, after you have clicked on the button.

    // Clicks the button
    const btn = wrapper.find('#touch');
    btn.trigger('click');

    // The button should be clicked, this also runs $v.$touch() internally
    expect(wrapper.vm.clicked).toBe(true);
    // Should be true because it's now dirty
    expect(wrapper.vm.$v.name.$dirty).toBe(true);

    await wrapper.vm.$nextTick() // <- over here

    // This is failing, computed doesn't seem to notice that $dirty is true now.
    expect(wrapper.findAll('.error').length).toBe(1);
0reactions
victorrajicommented, Jan 19, 2022

how

I couldn’t reproduce this on jsfiddle because of the tests, so I created a repository.

https://github.com/phiter/vuelidate-jest-issue

Basically, I have this computed property

validations: {
    name: {
      required,
    },
  },
computed: {
    nameErrors() {
      const errors = [];
      
      if (!this.$v.name.$dirty) return errors;
      console.log('is dirty');

      if (!this.$v.name.required) errors.push('Required field');
      
      return errors;
    },
  },

and this input

<input type="text" v-model="$v.name.$model"/>

When I update the input, I can access wrapper.vm.$v.name.$dirty from jest and it says it’s true. It works when I run the app through npm run serve, but computed doesn’t listen to $dirty being true when I try to get it.

The link to your repository is currently invalid, could you share a working code of this solution?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing Vue.js components with Vue Test Utils - LogRocket Blog
This guide showcases the benefits of testing your Vue.js components using Vue Test Utils and includes some real-world examples.
Read more >
Test Computed Properties and Watchers in Vue.js ... - Alex Jover
Learn about testing the Computed Properties and Watchers reactivity in Vue.js. ... Testing Vue.js components with Jest.
Read more >
Vue: When a computed property can be the wrong tool
Since our template depends on sortedList , and it's marked as "dirty" (potentially changed, needs re-evaluation), the component re-renders.
Read more >
Vue.js computed property not updating - Stack Overflow
Show activity on this post. I'm using a Vue. js computed property but am running into an issue: The computed method IS being...
Read more >
How to test Vue watcher that watches a computed property ...
and then in your component.spec.js you'd do this: import { mainStore } from '../store'; import Vuex from 'vuex'; //...
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