Comments (16)
Whilst I agree a 'setComputed' method should be added, you actually don't need it.
Just add a 'computed' property along with your other mounting options, like so...
const wrapper = mount(Component, {
...
computed: {
foo() {
return 'bar'
},
},
})
from vue-test-utils.
I agree, most of the time you should test it, but if there's a use case for setting a computed property I think we should add it.
I think @jackmellis described a use case well:
There have been times when a method relies on a computed property that is so complicated it's taken me far outside the purpose of my unit test just to get the right computed value!
from vue-test-utils.
Only just saw this after creating a similar issue on the Avoriaz repo. A setComputed
method is exactly what I need.
from vue-test-utils.
@jonnyparris that is a fair point, i think with the feature we should add something to documentation with some potential gotchas with it.
from vue-test-utils.
Sure the works for mounting but you can't trigger the watcher on mount. You need to update the reactive data that the watcher is observing. If it's a computed value, or a getter thats mocked from the store you can't force the computed value to update.
from vue-test-utils.
from vue-test-utils.
I can see a pragmatism to this.
I do agree with @blocka though and think that the downside with setComputed is that it is encouraging the use of computed properties in the same way you would use something like props or data. You could end up in a situation where you are unit tested 100% by setting computed and have a component that does not work. Guess it is a black box vs white box test thing.
I'm interested in the scenario/thoughts for needing this and if it would be better to create a separate component that you pass the computed property to as a prop and then test that component?
from vue-test-utils.
@Austio why not make both options available and let people decide for themselves?
Ideally I could have unit level tests for all my computed and method functions (which encourages smaller single-purpose functions) and have a 'component integration test' that checks all of those unit tests playing well together.
from vue-test-utils.
I made a PR — #84
from vue-test-utils.
What do you think about adding something like computedMock
mounting option:
mount(Component, { computedMock: { someVariable () {return 'mocked value'}}
and use extends
inside mount:
const ExtendedComponent = {
extends: Component,
computed: params.computed
}
return mount(ExtendedComponent);
It's only skeleton of idea, I'm not sure if it's even possible.
from vue-test-utils.
I think we should add setComputed, because we already have setData
, setMethods
, and setProps
from vue-test-utils.
Can you please add this method to docs? Thank you
from vue-test-utils.
Thanks for reminding me. I'll add it to my list.
If you are able make a PR it would be very helpful!
from vue-test-utils.
any hope on bringing back this feature,
What if my computed value was set by getters from vuex and im mocking the getters?
And then i want to test a watcher that is listening for changes on that value?
I would want a way to force update the computed value
from vue-test-utils.
from vue-test-utils.
@nath-codes thats great for mounting, the issue is further explained #331
from vue-test-utils.
Related Issues (20)
- `RouterLink.useLink` is missing when using `RouterLinkStub`. HOT 3
- Test child component, which is the root element HOT 3
- Computed value does not update when data updates
- isVisible() not seeing display: none using vitest with jsdom HOT 4
- Testing component with onBeforeRouteLeave hook raising "No active route record was found" warning HOT 1
- vue2 引入 element2. 页面存在table 表格 jest 测试报错 HOT 1
- *.svg?inline issues
- Errors in `setup()` are silent if `render()` throws an error HOT 6
- Check style changes on hover HOT 1
- setData on writable computed property does not seem to work as it does in VTU.1
- classes documentation
- Support Vue 2.7.16 HOT 2
- Vue test utils not updating template when testing composables
- TypeError: $setup.mergeProps is not a function
- Data property is already defined in Props. HOT 1
- Unable to use vitest spy within vue/nuxt 3 component methods. HOT 3
- `findComponent` doesn't find anything when using `@vue/compat` HOT 1
- Components with TS generic HOT 1
- event.keyCode is deprecated
- Change mocks mid-test
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vue-test-utils.