Fix setData/data through shallowMount/mount for components using setup() #2655
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We had some tests lying around that pretty extensively used
setData
to test some components, but after updating from the old object API to<script setup>
this no longer worked to my surprise.I had a spare morning, and long story short it now works, though I had to dig pretty far into Vue internals to figure out how it sets up components.
The setData and initialData can now technically be used to overwrite anything returned by
setup()
or<script setup>
, which I don't really like, but because Vue's composition API doesn't distinguish between what symbols are considered data and which are considered functionality, I think it's impossible to define a correct behavior here, and we'll have to settle for what most users desire, which I reckon is just being able to set values on refs. If anything, this "functionality" might even serve as a niche escape hatch for when you really need to mock something? I'd be happy to discuss alternate approaches though!