Home / Blog / How to Access Vue Refs Defined in Script Setup within Unit Tests
How to Access Vue Refs Defined in Script Setup within Unit Tests

How to Access Vue Refs Defined in Script Setup within Unit Tests

Daniel Kelly
Daniel Kelly
Updated: November 11th 2024

You may be familiar with how to read data defined on your Vue components with the options API from within your unit tests. That looks like this:

wrapper.vm.$data

But most people these days prefer using the composition API along with script setup. Reading reactive data defined with ref and reactive during testing looks a little different. So how do you manage it?

⚠️ NOTE: that most unit tests should NOT need to read the components internal data directly. Instead you should prefer to read the rendered HTML output to ensure it’s displayed as expected.

Setup the Example

Let’s say we have a component that looks like this. It has a reactive ref msg defined. In the template we’ve bound the msg data to an input with v-model.

<!-- MyComponent.vue -->
<script setup>
import { ref } from "vue";
const msg = ref("");
</script>

<template>
  <div id="my-id" class="my-class another-class">
    <input type="text" v-model="msg" />
  </div>
</template>

Reading the Data

The data we define within script setup will be added as a property directly on the component instance. Therefore we can read from within our tests like this:

wrapper.vm.msg

So if we simulate the user typing in the input with setValue then we can expect the msg data to be updated.

const wrapper = mount(MyComponent);

// to start it's an empty string
expect(wrapper.vm.msg).toBe("");

// simulate user input
const input = wrapper.find("input");
await input.setValue("hello");

// now the msg data is equal to what the user typed
expect(wrapper.vm.msg).toBe("hello");

Conclusion

So there you have it! In limited situations where you might need to read a component’s reactive data from within your unit tests, now you know how! Remember though, checking component internal data should not be the approach you take most times. Instead, you should cultivate the habit of checking a component's rendered output.

Start learning Vue.js for free

Daniel Kelly
Daniel Kelly
Daniel is the lead instructor at Vue School and enjoys helping other developers reach their full potential. He has 10+ years of developer experience using technologies including Vue.js, Nuxt.js, and Laravel.

Comments

Latest Vue School Articles

Developing a Full Stack Nuxt App with Bolt.new &#8211; An AI Experiment

Developing a Full Stack Nuxt App with Bolt.new – An AI Experiment

Use bolt.new to quickly iterate on a new Nuxt project. Learn how in this article, where we bootstrap a custom blog in no time!
Daniel Kelly
Daniel Kelly
Upgrading Eslint from v8 to v9 in Vue.js

Upgrading Eslint from v8 to v9 in Vue.js

Learn how to upgrade ESLint to v9 in Vue.js, taking advantage of the new flat configuration system and other significant changes in ESLint 9.0.
Mostafa Said
Mostafa Said

Our goal is to be the number one source of Vue.js knowledge for all skill levels. We offer the knowledge of our industry leaders through awesome video courses for a ridiculously low price.

More than 200.000 users have already joined us. You are welcome too!

Follow us on Social

© All rights reserved. Made with ❤️ by BitterBrains, Inc.