Print message on expect() assert failure

后端 未结 8 2191
一个人的身影
一个人的身影 2020-12-15 02:11

Is there a way to print a custom error message when a Jasmine expect() fails?

As an example, for end to end testing I have an array of web pages and I u

相关标签:
8条回答
  • 2020-12-15 03:02

    I had a requirement to log custom messages for Jasmine and I used the following method.

    beforeEach(function(){
        this.addMatchers({
            customError: function(mesg){
                             this.message= function () {
                                               return mesg;
                                           };
                             return this.actual == true;
                             }
                         });
            });
    if(<fail condidtion>){
        expect(false).customError(<Fail message>)
    }
    

    Please do note, what I have mentioned above is by jasmine 1 format. There will been slight change if you are using jasmine 2. Hope this is helpful for you

    0 讨论(0)
  • 2020-12-15 03:05

    Since Jasmine 3.3, there's a way to do it through withContext

    Example:

    expect(someValue).withContext('expected someValue to be true...').toBe(true)
    

    See also https://github.com/jasmine/jasmine/issues/641#issuecomment-457037665

    0 讨论(0)
提交回复
热议问题