Skip to content

Within 'setData' method computed variables are re-calculated, even if are not used in template #377

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

Closed
lusarz opened this issue Jan 25, 2018 · 6 comments
Labels

Comments

@lusarz
Copy link
Contributor

lusarz commented Jan 25, 2018

Version

1.0.0-beta.10

Reproduction link

https://github.com/lusarz/vue-test-utils/commit/51cf7b5c10cd2010c26a21d7e9f2cca400faacd1

Steps to reproduce

Use wrapper.setData(...) method

What is expected?

Computed properties that are not truly used in template shouldn't be re-calculated.

By 'truly used' I mean variables that are not filtered out by v-if directive.

What is actually happening?

All computed properties are calculated. In some situations it cause Cannot read property ${property} of null kind of errors.

@LinusBorg
Copy link
Member

By 'truly used' I mean variables that are not filtered out by v-if directive.

That's not how Vue works. This computed property will be re-evaluated independent of the usage in the render function.

This is not a bug in the test-utils, it's how Vue is designed to work.

@lusarz
Copy link
Contributor Author

lusarz commented Jan 25, 2018

@LinusBorg
I have some doubts. Could you check here:
https://github.com/lusarz/vue/commit/9ef5946f082a5565efdd0d0794371f61bf0e73d9

Both tests pass. In second one, when <span v-if="a">{{ b }}</span> is evaluated to not be rendered, function that calculates b is not invoked.

@LinusBorg LinusBorg reopened this Jan 25, 2018
@LinusBorg
Copy link
Member

Can't take a look right now but reopened for someone else to check

@LinusBorg
Copy link
Member

Edit: Quickly checked this with a fiddle: https://jsfiddle.net/tohdxapr/1/

Seems OP is right, sorry :/

@lusarz
Copy link
Contributor Author

lusarz commented Jan 25, 2018

No problem. @LinusBorg I turned off cache for computed property in fiddle: https://jsfiddle.net/tohdxapr/2/

With cache it doesn't prove anything :)

@lusarz
Copy link
Contributor Author

lusarz commented Jan 25, 2018

This issue seems to be related with watchers which are launched in setData -> update methods:

this._watchers.forEach(watcher => {
watcher.run()
})

I noticed that watcher on computed property is run here, not sure where this watcher is initialized

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants