How to tell if my code is running inside node:test?

29 views Asked by At

I'm using the native node:test runner however I have some code that I do not want to execute when I'm running the unit tests.

How can I identify whether my code is being run inside a node:test unit test or not?

(I am aware of workarounds such as manually setting environment variables, but I'd like to know whether it can be done with built in library support instead of such hacks.)

My reason for asking is that when I'm not running the unit tests, I want the Debug module to write its output differently, so that multiline debug messages are kept within a single AWS CloudWatch log event, rather than being split across one log event per line. The code that currently does this with Jest is:

// Don't do this when running unit tests, as the default output method works
// best with Jest.
if (!process.env.JEST_WORKER_ID) {
  // When running for real (not inside Jest), tell Debug to use console.log()
  // instead of writing to stderr.  This makes Lambda/CloudWatch combine
  // multi-line messages into a single log event.
  Debug.log = console.log.bind(console);
}

But this causes all the colours to disappear in the debug output when the unit tests are run locally, so I'd like to skip that block when the unit tests are being run.

0

There are 0 answers