Skip to content

Fix setData/data through shallowMount/mount for components using setup() #2655

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

KCMertens
Copy link

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!

Copy link

netlify bot commented Apr 19, 2025

Deploy Preview for vue-test-utils-docs ready!

Name Link
🔨 Latest commit e6104e4
🔍 Latest deploy log https://app.netlify.com/sites/vue-test-utils-docs/deploys/680764e078725b000816376e
😎 Deploy Preview https://deploy-preview-2655--vue-test-utils-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

KCMertens and others added 3 commits April 21, 2025 10:12
…ent projects

We were checking reference equality with the default vue EMPTY_OBJ placeholder, however in the production bundle of test-utils, this was inlined, so in effect there were suddenly two EMPTY_OBJ definitions, and the references would no longer match.
Instead, detect whether the object is frozen, which should always hold for the EMPTY_OBJ, no matter if it's inlined or not
Using setData or providing a data() function for an array property would not work correctly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant