Vue component computed doesn't listen to $dirty changes when testing with jest
See original GitHub issueI 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:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.how
The link to your repository is currently invalid, could you share a working code of this solution?