How to easily identify unhadled rejections in NodeJS

This article was published on Jun 08, 2021

In the current setup of my work, when we have a not handled rejection in a promise, instead of throwing an error it just throws a warning message.

But the problem is that this message does not contain a stack trace that points us to where this is happening. Consequently, when we run our unit tests (around 2k files) concurrently, the warning message shows up in the middle of random tests, making it hard to find where the problem is.

Luckily I've found a NodeJS option called --unhandled-rejections which allows us to force a runtime error at the moment where this error happens.

Since I don't run node binary directly, I needed to pass this via environment variable:

NODE_OPTIONS=--unhandled-rejections=strict rushx test

By running our tests with this Node option, now an error is raised in the exact point where the promise was failing.