How to determine if JEST is running the code or not?
I usually have NODE_ENV=development
set globally on my shell. This works for me:
typeof jest !== 'undefined'
(note that global.jest
and 'jest' in global
don't work, as this doesn't seem to be a global variable, just a value made available on all modules much like node's require
or __filename
)
jest sets an environment variable called JEST_WORKER_ID so you check if this is set:
function areWeTestingWithJest() {
return process.env.JEST_WORKER_ID !== undefined;
}
I also see that if NODE_ENV is not set the jest CLI sets it to the value 'test'. This might be another way to check.