question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

$refs doesn't work when using v-for and destructuring

See original GitHub issue

Version

1.1.1

Reproduction link

https://github.com/adrianosmond/refs-v-for-and-rest-destructuring

Steps to reproduce

The repo above is set up so that running the tests (with npm run test) will fail on the component (src/components/Broken.vue) which combines object rest destructuring, ref=“” and v-for like so:

<li v-for="{key, label, ...toBind} in items" :key="key" ref="listItem">
    <a v-bind="toBind">{{ label }}</a>
</li>

This works fine in Vue itself, as can be seen if you run the project (npm run serve) and open it in your browser on port 8080.

A component (src/components/Working.vue) is also included which is identical except that it doesn’t use rest destructuring inside v-for. The same test then passes for this component.

What is expected?

$refs.listItem should be an array containing 2 <li> elements

What is actually happening?

$refs.listItem is undefined

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
lmiller1990commented, Feb 24, 2022

Nice, seems I won’t need to spend much more time on this. 3.0.7 is a really old build, glad you got it working. New Vue CLI templates ship the latest vue2-jest, so should be good moving forward.

Going to close this one off as solved/wontfix.

0reactions
renatodeleaocommented, Feb 23, 2022

@lmiller1990 many thanks for the explanation! You can stop wondering: it works with jest@27 and latest @vue/vue2-jest. I’m sry for not checking basic setup instructions and consequently reviving this thread. Cheers!

// setup that works
{
  "vue": "2.6.14",
-  "vue-jest": "3.0.7",
+  "@vue/vue2-jest": "27.0.0-alpha.4",
  "jest": "27.5.1",
  "babel-jest": "27.5.1"
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot destructure ref property as it is undefined trying to use ...
Cannot destructure ref property as it is undefined trying to use a multiple ref context. Save this question. Show activity on this post....
Read more >
Reactivity Transform - Vue.js
If a destructured value is not a ref (e.g. a function), it will still work - the value will be wrapped in a...
Read more >
Ref patterns, destructuring, and invisible borrows - Medium
So the lesson is that when destructuring is involved, it's not enough to look for & to find the borrows of a given...
Read more >
Vue 3 Composition API: How to Omit `.value` in refs
Previously, if you tried to destructure an object that was a ref , the destructured variables would lose their reactivity. Let's go with...
Read more >
10. Destructuring - Exploring JS
Don't start a statement with a curly brace ... Array destructuring (works for all iterable values): ... That means that it works with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found