Start learning free

LESSON 144 OF 173 · BACKEND: TESTS AND RUNNING IT

Settings per Environment

Practice

Your shop runs in two places with the same code: on your computer while you build it, the development environment, and on the server where customers use it, production. Only a few settings differ. The server tells the program where it runs with an environment variable, a named text value that Node keeps in the object process.env.

You read one like any property. A variable that isn't set is undefined, and || then gives the value on its right, the default. A test can set a variable by assigning to it: it keeps the old value first and puts it back at the end, so the other tests don't see the change. Here a shop picks its currency:

const currency = process.env.SHOP_CURRENCY || "USD";

const saved = process.env.SHOP_CURRENCY;
process.env.SHOP_CURRENCY = "EUR";
console.log(process.env.SHOP_CURRENCY);   // EUR
process.env.SHOP_CURRENCY = saved;

The setting that matters most is the mode Express runs in, kept in its setting env: app.set("env", value) stores it and app.get("env") reads it. In development mode, when a request crashes, Express shows the error and its stack trace right in the browser: handy on your computer, dangerous on the server. A new Express app starts in development mode, and for Express every value except exactly production means development. So your rule is strict the other way around: development mode only when SHOP_ENV is exactly development, and production mode for production, for a missing variable and for a typo.

The check starts your shop as on the server, with SHOP_ENV=production, calls your function in each case, and runs your test on the shop and on a copy that stays in development mode everywhere. It must print:

Your shop, started on the server with SHOP_ENV=production:
  env = production
loadConfig on your computer, with SHOP_ENV=development:
  env = development
loadConfig on the server, with SHOP_ENV=production:
  env = production
loadConfig on a server where nobody set SHOP_ENV:
  env = production
loadConfig with SHOP_ENV=Development, a typo:
  env = production
Your tests on the shop:
  production mode on the server ... ok
  1 test: OK
Your tests on a copy that stays in development mode everywhere:
  production mode on the server ... FAIL
    AssertionError: 'development' != 'production'
  1 test: 1 failed, so they caught this bug

Your task: write loadConfig(app), which reads SHOP_ENV with the default "production" and sets the app's env setting to "development" or "production", and call it for the shop's app. Then write the test "production mode on the server", which loads the settings into a new app with SHOP_ENV set to production and checks that its env setting is "production".


Assignment
Follow the Coding Tutorial to load the shop's settings from SHOP_ENV and write the test that keeps the server in production mode.


Hint
Write the function as function loadConfig(app) { ... }, so the check can put its broken copy in its place. In the test, make a new express() app instead of changing the shop's, put SHOP_ENV back right after the loadConfig call, and read the setting with testApp.get("env").


STEP 1 OF 12
    Ctrl + Enter to check