How to write a test which expects an Error to be thrown in Jasmine?

前端 未结 9 1101
忘了有多久
忘了有多久 2020-11-28 17:26

I\'m trying to write a test for the Jasmine Test Framework which expects an error. At the moment I\'m using a Jasmine Node.js integration from GitHub.

In my Node mod

9条回答
  •  旧时难觅i
    2020-11-28 18:05

    For anyone who still might be facing this issue, for me the posted solution didn't work and it kept on throwing this error: Error: Expected function to throw an exception. I later realised that the function which I was expecting to throw an error was an async function and was expecting promise to be rejected and then throw error and that's what I was doing in my code:

    throw new Error('REQUEST ID NOT FOUND');
    

    and thats what I did in my test and it worked:

    it('Test should throw error if request not found', willResolve(() => {
             const promise = service.getRequestStatus('request-id');
                    return expectToReject(promise).then((err) => {
                        expect(err.message).toEqual('REQUEST NOT FOUND');
                    });
                }));
    

提交回复
热议问题