(Updated: )
/ #ava #Testing #node 

AVA: pass or fail a test if an environment variable is missing/empty

In a recent project I needed to fail/pass some AVA if a runtime environment variable was unset or empty. Here’s how I solved this issue.

AVA is a test runner for Node.js with a concise API, detailed error output, embrace of new language features and process isolation that let you write tests more effectively.

I’m a big fan of AVA’s explicit nature and it plays nicely with my favourite formatting tool: xo.

I recently used AVA to drive both unit and end to end tests for a JavaScript SDK/API wrapper for the Buttondown API. Buttondown is “a small, elegant tool for producing newsletters”.

You can see the SDK/Node.js API Wrapper at github.com/HugoDF/buttondown.

In the End to End suite, I needed a way to skip the tests, but only if the environment didn’t have an environment variable needed to run them.

Table of Contents

Pass an AVA test if an environment variable is missing/empty

The great thing about AVA tests is that they take function callbacks, which means we can easily short-circuit them.

Note: other names you might know for “short-circuiting” a function are: “guard clauses”, “early returns”

To check a REQUIRED_ENV_VARIABLE is not set or empty we can do if (!process.env.REQUIRED_ENV_VARIABLE). Then we have an AVA built-in to pass the test, t.pass(), which we call with our message.

The most important thing is to return when we hit the “if” statement thereby short-circuiting the AVA test callback.

Full example, you can see it in action at github.com/HugoDF/ava-pass-fail-test-missing-env-var.

const test = require('ava');

test('passes if environment variable is missing', (t) => {
  if (!process.env.REQUIRED_ENV_VARIABLE) {
    return t.pass('REQUIRED_ENV_VARIABLE is missing');
  }

  throw new Error(
    'if we got here, we would need REQUIRED_ENV_VARIABLE and fail'
  );
});

Running the test normally using npx ava yields a pass.

Running the test with REQUIRED_ENV_VARIABLE=here (ie. REQUIRED_ENV_VARIABLE set to ‘here’) gives the following output

REQUIRED_ENV_VARIABLE=here ava

  1 test failed

  passes if environment variable is missing

  test.js:8

   7:
   8:   throw new Error(
   9:     'if we got here, we would need REQUIRED_ENV_VARIAB…

  Error thrown in test:

  Error {
    message: 'if we got here, we would need REQUIRED_ENV_VARIABLE and fail',
  }

We’ve now see how to automatically pass an AVA test if an environment variable it requires is missing or empty.

Next we’ll see how to automatically fail an AVA test if an environment variable it requires is missing or empty.

Fail an AVA test if an environment variables is missing/empty

In a similar fashion as for the “pass automatically”, if we require a REQUIRED_ENV_VARIABLE environment variable, we can check using if (!process.env.REQUIRED_ENV_VARIABLE). Then we have a built-in AVA function to automatically fail the test, t.fail(), which we call with a message.

Again the most important thing is to return when we hit the “if” statement thereby short-circuiting the AVA test callback.

Full example, you can see it in action at github.com/HugoDF/ava-pass-fail-test-missing-env-var.

const test = require('ava');

test.failing('fails if environment variable is missing', (t) => {
  if (!process.env.REQUIRED_ENV_VARIABLE) {
    return t.fail('REQUIRED_ENV_VARIABLE is missing');
  }

  t.pass(
    'If we got here the ".failing" modifier would not be satisfied since the test passed'
  );
});

Running the test normally using npx ava yields a pass since the test is failing but is marked as failing (using the .failing modifier).

Running the test with REQUIRED_ENV_VARIABLE=here (ie. REQUIRED_ENV_VARIABLE set to ‘here’) gives the following output.

REQUIRED_ENV_VARIABLE=here ava

  1 test failed

  fails if environment variable is missing


  Error: Test was expected to fail, but succeeded, you should stop marking the test as failing

That’s it we’ve seen how to fail or pass an AVA test if an environment variable is unset/empty.

Just a reminder that you can see all of the above sample tests in action at github.com/HugoDF/ava-pass-fail-test-missing-env-var and see the pattern being used in the real world at github.com/HugoDF/buttondown/tree/master/tests/e2e.

unsplash-logoMatthew Smith

Author

Hugo Di Francesco

Co-author of "Professional JavaScript", "Front-End Development Projects with Vue.js" with Packt, "The Jest Handbook" (self-published). Hugo runs the Code with Hugo website helping over 100,000 developers every month and holds an MEng in Mathematical Computation from University College London (UCL). He has used JavaScript extensively to create scalable and performant platforms at companies such as Canon, Elsevier and (currently) Eurostar.

Get The Jest Handbook (100 pages)

Take your JavaScript testing to the next level by learning the ins and outs of Jest, the top JavaScript testing library.