最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论