How I can test in jest line with throw e?

后端 未结 2 457
青春惊慌失措
青春惊慌失措 2021-01-27 02:38

How I can test in jest error case? This is what I do: I don\'t know if exist a method how to test this.

it (\'the fetch fails and throw an error\', async () =         


        
2条回答
  •  孤街浪徒
    2021-01-27 02:53

    You can do it in the following way:

    async function throws () {
      throw new Error('error')
    }
    
    test('promise throws', async () => {
      await expect(throws()).rejects.toThrow()
    })
    
    test('the fetch fails with an error', async () => {
      await expect(throws()).rejects.toThrow('error');
    });
    
    test('the fetch fails with an error', () => {
      return expect(throws()).rejects.toMatch('error');
    });
    

    Read more docs.

提交回复
热议问题