I am testing an endpoint with JEST and Got. I expect 403 Forbidden error. The following code prints the error from catch block AND fails that the identical call does not throw an error. Why?
try { response = await api(`verify/${profile.auth.verifyToken}`, {method: 'POST'}).json(); } catch (e) { console.log(e); } expect(async () => { response = await api(`verify/${profile.auth.verifyToken}`, {method: 'POST'}).json(); }).toThrow(); Output:
console.log test/api.int.test.js:112 HTTPError: Response code 403 (Forbidden) at EventEmitter.<anonymous> (C:\dev\mezinamiridici\infrastructure\node_modules\got\dist\source\as-promise.js:118:31) at processTicksAndRejections (internal/process/task_queues.js:97:5) { name: 'HTTPError' } Error: expect(received).toThrow() Received function did not throw This variant does not work either:
expect(() => api(`verify/${profile.auth.verifyToken}`, {method: 'POST'})).toThrow(); Btw when the HTTPError is thrown and not catched, there is no stacktrace and I do not see where the error was thrown. If there are other error I exactly see which test line was responsible. Why?