5 Questions Every Unit Test Must Answer

How to Write Better Tests

Eric Elliott
Aug 29, 2015 · 7 min read

Most Developers
Don’t Know How to Test

Why Bother with Test Discipline?

The Science of TDD

Write Tests First

Before you implement,
write the test.

What’s in a Good Unit Test?

The Unit Test as a Bug Report

A failing test should read
like a high-quality bug report.

What’s in a good test failure bug report?

Image for post
Image for post
Example of a good failure report.

Start by answering “what are you testing?”:

What should the feature do?

'compose() should return a function.'

What component aspect are we testing?

test('<What component aspect are we testing?>', assert => {
test('Compose function output type.', assert => {
test('Compose function output type.', assert => {
'compose() should return a function.'

What is the Output (Expected and Actual)?

Equal is your new default assertion.
It is the staple of every good test suite.

A Challenge

  const actual = '<what is the actual output?>';
const expected = '<what is the expected output?>';
  const actual = '<how is the test reproduced?>';
  const actual = typeof compose();
const expected = 'function';
  assert.equal(actual, expected,
'compose() should return a function.');

A Unit Test Template:

Next Steps

Watch the webcast recording,
TDD in ES6 & React

& much more with the
Lifetime Access Pass

JavaScript Scene

JavaScript, software leadership, software development, and…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store