I'm currently working on some tests scripts for my project. The code is written in ES7 and compiled using babel
First some background:
Let's say you run npm test
and the test file looks like this:
function foo (bar) {
if (!bar) throw new Error('foooo')
console.log(bar)
}
foo() // No argument given
Now when you run this script the npm will show you an error like this:
$ npm ERR! Test failed. See above for more details.
This is the desired output, something went wrong, you do not want to release this software yet since there's an error in there.
So what's my question?
Okey so I have a test script setup like this:
async function init () {
const status = await someRequestPromise()
if (status.error) throw status.error
console.log(status.result) // Log the result of this async request...
}
init()
To run my test first I compile the ES7 using babel like so:
$ babel src -d dist
and then run npm test
The result
To be clear, npm did not trigger the error...
I also tried the following code, however this did not change anything...
async function init () {
const status = await someRequestPromise()
if (status.error) throw status.error
console.log(status.result) // Log the result of this async request...
}
try {
init()
} catch (e) {
console.log(e)
}
I'd like to know how I can trigger npm to catch the error and report the fact that the test failed. Also I'd like to know how to catch the error's, since node marks them as unhandled right now, even with the try catch block.
PS. adding an await
statement in front of the init()
function causes a syntax error, please don't suggest that.