How to add custom message to Jest expect?

前端 未结 8 1207
鱼传尺愫
鱼传尺愫 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:54

    You can rewrite the expect assertion to use toThrow() or not.toThrow(). Then throw an Error with your custom text. jest will include the custom text in the output.

    // Closure which returns function which may throw
    function isValid (email) {
      return () => {
         // replace with a real test!
         if (email !== 'some@example.com') {
           throw new Error(`Email ${email} not valid`)
         }
      }
    }
    
    expect(isValid(email)).not.toThrow()
    
    

提交回复
热议问题