4

I am starting with Vue.js and is really hard to find documentation about Unit Test.

I am trying to test components methods and builtin stuff as ready(). I can call those correctly but they internally have references to this object and this context is lost during testing time.

error

TypeError: this.$on is not a function

spec.js

import Vue      from 'vue';
import Partners from 'components/main/partner/Partners';

describe.only('Partners.vue', () => {
  it('should render with mocked partners', (cb) => {
    Partners.ready(); // I get an error here because ready() is calling inside: this.$on(...)

    cb(null);
  });
});

component.vue

export default {
  name: 'Partners',

  data() {
    return { };
  },

  methods: {
    get() {
      // ...
    }
  },
  ready() {
    this.$on('confirm', (confirm) => {
      // ...
    });

    this.get();
  }
};

1 Answer 1

2

I think ready() is depreciated along with Vue 1.0. Consider upgrading to Vue 2 (where mounted() replaces ready()).

To test your component, you need to initialize your component and a Vue instance, (and usually mount it, depending what you are doing).

Using the vue-webpack template (which uses Vue 2):

var ctor = Vue.extend(Partners)
var vm = new ctor()
vm.$mount()

now you can do things like vm.method(), and vm.mount() will automatically be called, etc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.