-
Notifications
You must be signed in to change notification settings - Fork 62
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
Testing of computed properties #15
Comments
Three potential solutions here:
const wrapper = mount(Component, {computed: { someMethod }})
expect(wrapper.vm.someMethod).to.equal(someMethod) // true :)
function someMethod(){
console.log(this)
}
const wrapper = mount(Component, {computed: { someMethod }})
expect(Component.computed().someMethod).to.equal(someMethod) // throws assertion error
const wrapper = mount(Component, {computed: { someMethod }})
expect(wrapper.someMethod).to.equal(someMethod) // true Problems with this:
So far I'm leaning towards the first solution I'd really appreciate any suggestions from others. I think the best thing to do right not is log an error when method(), computed() and propsData() are called. |
@atilacamurca @stephane thoughts on the above? |
I agree with you, you should always keep it a wrapper. |
@ryanjgill I can't think of a good way of fixing this. Instead I've added a warning that points to this issue and suggest using wrapper.vm.methodName to access computed functions that use this. |
@eddyerburgh Sounds good. Thanks for all the feedback guys! |
hi @eddyerburgh given(
...some test data
)
.it('should accept prop:', (propName, propValue) => {
const wrapper = mount(InfiniteScroller, {
propsData: { propName: propValue }
});
expect(wrapper.propsData().propName).to.equal(propValue);
}); Then I got this warning messages and found out the reason in the issue from the messages.
According to the comment #15 (comment), looks like there is still no solution for somethink like expect(wrapper.vm.propName).to.equal(propValue); am I right, guys? |
@jasperck Both of your examples should work. The warning message is only meant for users trying to call propsData that use this. |
@eddyerburgh Thanks, I see, but actually it only worked for expect(wrapper.propsData().propName).to.equal(propValue); but not expect(wrapper.vm.propName).to.equal(propValue);
// test output
expected undefined to equal `propValue` calling prop directly on |
@jasperck Hi, sorry for the miselading message. I've updated and released a correct warning message in 1.9.4. To access props with this bound the vue instance, you must run: expect(wrapper.vm.$props.propName).to.equal(propValue); |
Hi. I am trying to follow the steps in the warning in order to check a computed but I get an error and this is the code
Of course, the computed exists in the component. |
@disitec computed methods are used as getter functions for the property - https://vuejs.org/v2/guide/computed.html#Computed-Properties t.is(wrapper.vm.welcomeText, 'INTRODUCTION TEXT HERE'); Should pass |
Yep, It works. So, I think you shuould update the warning without parenthesis ;) Thanks! |
Fancy making a PR? 😉 |
Here you have. |
When trying to test a computed property using AVA and with Avoriaz, the context of
this
is not what is expected. I'm trying to use a computed property that is based on some data from the component.More details on stackoverflow.
The text was updated successfully, but these errors were encountered: