Isolating production code in JavaScript

While writing services, we are likely to come around pieces of code that should only run in production. For eg. A function that takes a list of email addresses and sends out promotional emails, or a function that makes a financial transaction. You want those function to run only while on production. You don’t want to accidentally send out emails while testing your code.

A simple way to solve this is to check if an environment variable is set to `production` on each of this kind of functions.

But this is probably not the best way we can solve this. There are many issues we face if we go down this path.

  1. You can’t test those functions as they are only ever going to run on production. Well, you can, by changing process.env while testing and restoring it back. But it has its own side-effects. If you have tests running in async and you change process.env to test a function after mocking it, Some other tests might be running at the same time that hasn’t done mocking and expected the environment variable to be tested so that the code doesn’t run. NOT GOOD!.
  2. If you have such functions that call other such functions, you have to mock all those functions even if you want to test one of them.

We need a better way to solve this. We should be able to specify that a function is isolated and should only run when in production. And we still need the ability to ask it run while testing without messing with global variables. And only the function we asked to run, should be the one that runs. Other should still stay isolated, even if they are called from the function that we asked to run.

To solve this, I created Isograte. You can use it to isolate a function and then easily integrate it back while testing. Isograte exposes two functions, isolate and integrate. You can isolate any function to run only in production and integrate it back while testing. Only the function you directly called integrate on gets run.

And while testing:

Thank you for taking your time to read this.
Siddharth Doshi