$ npm install
$ npm run test
In helpers.test.ts
, remove the try/catch
block, leaving only the line that was in try
.
Do npm run test
and see how the test fails, as the failed request throws an uncaught error.
It's simple to use async
and await
, but they don't catch errors.
If this request failed outside of a try
block, like with a 404
:
const response = await axios.get('http://localhost:3000/endpoint/does-not-exist');
...there would be an uncaught error:
Instead of using await
, calling the axios.get
Promise with .catch()
will prevent the uncaught error:
axios.get('http://localhost:3000/endpoint/does-not-exist')
.then((result) => console.log(result))
.catch((error) => console.log(error.message));
Using async
and await
is usually easier, but you may want to catch errors.