It is a Good Naming Convention

I really like the describe and it naming convention made popular by Mocha and Jasmine. Indeed, I put a fair amount of time into SpectrumBDD for Java to try to provide this syntax in Java tests, though to be fair, the lack of IDE support for running them spoiled it in the end.

Though Jest also provides the more monolithic test function, I still prefer to use it. Here’s how to use them, and why it’s good.

describe

Use it to name the thing you’re testing. This may be the name of the module, or the name of a functional area within the module. Consider it a heading and give it the name you’d give a heading in a document.

Example:

describe('Metrics emitter', () => {
   describe('Cloudwatch enabled', () => {

   });
   describe('Cloudwatch disabled', () => {
   });
});

This describes the hierarchy of the test suite as well as the hierachy of the functionality.

it

This is a reminder to start a sentence with the word it and then continue it to describe a use case:

it('outputs a count metric with the namespace', () => { ... });
it('logs to console', () => { ... });

So…

The output in the test window then reads like a spec. Somehow these magic words become like a madlib where you fill in the blanks and get documentation/tests in one:

  • Metrics emitter
    • Cloudwatch enabled
      • outputs a count metric with the namespace
    • Cloudwatch disabled
      • logs to console

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s