I'm trying to unit test a function in my application that checks the value of process.env.NODE_ENV
. FWIW the type of process.env.NODE_ENV
is string | undefined
.
In my jest test, I can easily test string values when it's set like this:
it('test', () => {
// Arrange
process.env.NODE_ENV = 'development';
// Act
functionUnderTest();
// Assert
export(true).toBe(true);
});
The problem arises when I want to test my code's handling of process.env.NODE_ENV
being set to undefined
. I would expect to be able to simply set process.env.NODE_ENV = undefined
in my jest test. Unfortunately, it would seem that somewhere along the line, the value is converted to a string
such that when the test hits my function, the value of process.env.NODE_ENV
is "undefined"
(as a string).
I've also tried to set process.env.NODE_ENV
to undefined
like this, but with the same result:
process.env = Object.assign(process.env, { NODE_ENV: undefined });
How can I set process.env.NODE_ENV
to undefined
in a jest test?