javascript - Testing Vue.js Component - Stack Overflow

I'd like to test a Vue.js ponent, and I'm failing at that. Simply put, I'm setting a pon

I'd like to test a Vue.js ponent, and I'm failing at that. Simply put, I'm setting a ponent property, and I want to assert that it is set correctly. If that matters, the module is loaded with exports, and the JS is output using Webpack.

// ponent
exports = module.exports = {};

module.exports = {
  data: function () {
    return {
      active: false
    };
  },
  methods: {
    'close': function () {
       console.log(this.active); // -> true
       this.active = false;
       console.log(this.active); // -> false
    }
  }
};

// ponent-test
var modal = require('../../resources/src/js/ponents/_ponent.js');
var assert = require('assert');

describe('close()', function () {
  beforeEach(function () {
    modal.data.active = true;
  });
  it('should set modal to inactive', function () {
    console.log(modal.data.active); // -> true
    modal.methods.close();
    console.log(modal.data.active); // -> true
    assert.equal(modal.data.active, false);
  });
});

I'd like to test a Vue.js ponent, and I'm failing at that. Simply put, I'm setting a ponent property, and I want to assert that it is set correctly. If that matters, the module is loaded with exports, and the JS is output using Webpack.

// ponent
exports = module.exports = {};

module.exports = {
  data: function () {
    return {
      active: false
    };
  },
  methods: {
    'close': function () {
       console.log(this.active); // -> true
       this.active = false;
       console.log(this.active); // -> false
    }
  }
};

// ponent-test
var modal = require('../../resources/src/js/ponents/_ponent.js');
var assert = require('assert');

describe('close()', function () {
  beforeEach(function () {
    modal.data.active = true;
  });
  it('should set modal to inactive', function () {
    console.log(modal.data.active); // -> true
    modal.methods.close();
    console.log(modal.data.active); // -> true
    assert.equal(modal.data.active, false);
  });
});
Share Improve this question asked Feb 8, 2016 at 22:55 veksenveksen 7,0035 gold badges22 silver badges33 bronze badges 2
  • so what is actually failing? what's the output of your tests? have you check the vue js guide about testing? – Yerko Palma Commented Feb 9, 2016 at 11:53
  • also check the webpack example on github, there are some test defined there, with karma + jasmine + phantomjs – Yerko Palma Commented Feb 9, 2016 at 11:57
Add a ment  | 

2 Answers 2

Reset to default 6

This should give you a hint on how to load vue ponents when testing;

var modalComponent = require('../../resources/src/js/ponents/_ponent.js');
var assert = require('assert');         

 //load the ponent with a vue instance
vm = new Vue({
    template: '<div><test v-ref:test-ponent></test></div>',
    ponents: {
        'test': modalComponent
    }
}).$mount();

var modal = vm.$refs.testComponent;

describe('close()', function () {
    beforeEach(function () {
        modal.active = true;
    });

    it('should set modal to inactive', function () {
        console.log(modal.active); // -> true
        modal.close();
        console.log(modal.active); // -> false
        assert.equal(modal.active, false);
    });
});

https://github./eddyerburgh/avoriaz is now the official testing library for Vue.js checkout the documentation on getting setup to make assertions on your ponents https://eddyerburgh.gitbooks.io/avoriaz/content/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742413874a4439356.html

相关推荐

  • javascript - Testing Vue.js Component - Stack Overflow

    I'd like to test a Vue.js ponent, and I'm failing at that. Simply put, I'm setting a pon

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信