Categories
JavaScript Answers

How to stub process.env in Node.js?

Spread the love

To stub process.env in Node.js, we set the values in our test.

For instance, we write

it("does something interesting", () => {
  process.env.NODE_ENV = "test";
  // ...
});

afterEach(() => {
  delete process.env.NODE_ENV;
});

to set the process.env.NODE_ENV to 'test' in our test.

And we delete the property in the afterEach callback so it’s removed after each test.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *