[Vuejs]-Can you implement Vue Unit tests without a component using purely the Vue Instance?

2👍

Yes, you can. Here is a simple example with QUnit where we test a “Hello, World!” message:

QUnit.test('"Hello, World!" is displayed', function (assert) {
  assert.equal(vm.$data.message, 'Hello, World!');
  assert.equal(document.querySelector('h1').textContent, 'Hello, World!');
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/qunit/2.4.0/qunit.min.css" rel="stylesheet">

<div id="app">
  <h1>{{ message }}</h1>
</div>

<div id="qunit"></div>
<div id="qunit-fixture"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qunit/2.4.0/qunit.min.js"></script>
<script>
  var vm = new Vue({
    el: '#app',
    data: {
      message: 'Hello, World!'
    }
  });
</script>

Leave a comment