How to add custom message to Jest expect?

前端 未结 8 1212
鱼传尺愫
鱼传尺愫 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 18:05

    Another way to add a custom error message is by using the fail() method:

    it('valid emails checks', (done) => {
      ['abc@y.com', 'a@b.nz'/*, ...*/].map(mail => {
        if (!isValid(mail)) {
          done.fail(`Email '${mail}' should be valid`)
        } else {
          done()
        }
      })
    })
    

提交回复
热议问题