javascript - Vue.js testing: props validation,how to test Vue console.error log with JEST - Stack Overflow

During testing my new VueJS project with Jest I encountered problem caused by props validation. All of

During testing my new VueJS project with Jest I encountered problem caused by props validation.

All of my tests that has proper mocked data, pass the validator from ponent.

I would like to test situation when I mock wrong data and pass it to the ponent. I know that when the validator from ponent return false, vuemon.js console.error this:

console.error node_modules/vue/dist/vuemon.js:576
[Vue warn]: Invalid prop: custom validator check failed for prop "SOMETHING".

I would like to catch this error in my Jest test, but none of methods below I've tried work for me:

  1. Using spyOn(console, 'error') and then except(spy.calls.mostRecent()).toHaveBeenCalled().

  2. Registering console.* in globals like this global.console {...}

I have no idea what should I try now or what I am doing wrong.

Thanks for the answers, cheers.

During testing my new VueJS project with Jest I encountered problem caused by props validation.

All of my tests that has proper mocked data, pass the validator from ponent.

I would like to test situation when I mock wrong data and pass it to the ponent. I know that when the validator from ponent return false, vue.mon.js console.error this:

console.error node_modules/vue/dist/vue.mon.js:576
[Vue warn]: Invalid prop: custom validator check failed for prop "SOMETHING".

I would like to catch this error in my Jest test, but none of methods below I've tried work for me:

  1. Using spyOn(console, 'error') and then except(spy.calls.mostRecent()).toHaveBeenCalled().

  2. Registering console.* in globals like this global.console {...}

I have no idea what should I try now or what I am doing wrong.

Thanks for the answers, cheers.

Share Improve this question asked Nov 24, 2017 at 14:03 Wojtek JureczkaWojtek Jureczka 1612 silver badges8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Solved:

I had to use my wrapper.vm object, something like this:

expect(wrapper.vm.$options.props.YOUR_PROPS_OBJECT.validator(YOUR_WRONG_PROPS)).toBe(false);

One more way to deal with validator testing:

the ponent can be used directly, for instance, if the the ponent, being tested is named App and the property name is p then

expect(App.props.p.validator(arg)).toBe(res) 

is the test for it.

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信