0

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

CLI screenshot 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.

  • By default, `npm test` doesn't do anything. What testing runner/framework are you using? Many of them support async test, e.g. Mocha does. – Pavlo Feb 09 '17 at 12:11
  • npm catches the fact that the script exits on an unhandled error. Correct? – Jesse van der Pluijm Feb 09 '17 at 12:19
  • Sorry, forgot to mention. @pavlo – Jesse van der Pluijm Feb 09 '17 at 12:31
  • possible duplicate of [Can I catch an error from async without using await?](http://stackoverflow.com/q/30649994/1048572), [How do I catch thrown errors with async / await?](http://stackoverflow.com/q/33562284/1048572) and many more – Bergi Feb 10 '17 at 02:57

1 Answers1

0

since async/await returns promises, you must handle rejections (just as the console output suggests) via .catch:

const handle = init()
handle
  .then((result) => handleSuccess(result))
  .catch(({message}) => console.log(message))

try/catch (at least, in my experience)is for sync code.

EDIT:

To make the script 'crash', You may just add a process.exit(1).

const handle = init()
handle
  .then((result) => handleSuccess(result))
  .catch(({message}) => {
     console.log(message)
     process.exit(1)
   })
Seth Malaki
  • 4,298
  • 21
  • 47
  • I'll look into it. Though `try/catch` blocks where best practice in combo with async/await – Jesse van der Pluijm Feb 09 '17 at 12:34
  • 1
    Yes it is, if you're handling errors *within* the async function and not outside -- just like its predecessor, ES6 Generators. Does that make sense? Also, where'd you find that information about `try/catch`? – Seth Malaki Feb 09 '17 at 12:39
  • Information was based upon my very own memory haha. Read it somewhere someday... (I'll test the `.catch()` approach quickly) – Jesse van der Pluijm Feb 09 '17 at 13:58
  • Okey so yeah, while that is the desired way to handle any errors its still not a valid way to make npm actually catch the fact that the program failed. You're handeling the error and therefore it's expected... the script won't crash. – Jesse van der Pluijm Feb 09 '17 at 14:07
  • simply add `process.exit(1)` in the `catch` section – Seth Malaki Feb 09 '17 at 22:32