How to add custom message to Jest expect?

前端 未结 8 1205
鱼传尺愫
鱼传尺愫 2021-02-03 17:35

Image following test case:

it(\'valid emails checks\', () => {
  [\'abc@y.com\', \'a@b.nz\'/*, ...*/].map(mail => {
    expect(isValid(mail)).toBe(true);
          


        
8条回答
  •  [愿得一人]
    2021-02-03 17:56

    you can use this: (you can define it inside the test)

          expect.extend({
    ToBeMatch(expect, toBe, Msg) {  //Msg is the message you pass as parameter
        const pass = expect === toBe;
        if(pass){//pass = true its ok
            return {
                pass: pass,
                message: () => 'No ERRORS ',
              };
        }else{//not pass
            return {
                pass: pass,
                message: () => 'Error in Field   '+Msg + '  expect  ' +  '  ('+expect+') ' + 'recived '+'('+toBe+')',
              };
        }
    },  });
    

    and use it like this

         let z = 'TheMassageYouWantWhenErrror';
        expect(first.name).ToBeMatch(second.name,z);
    

提交回复
热议问题