Skip to content

Testing

Paul Götze edited this page Jul 20, 2016 · 2 revisions

You can run the tests along with linting with

$ npm run test

For TDD you can watch the code for changes and run tests automatically with

$ npm run test:watch

Testing libraries

We use:

  • mocha for unit tests
  • enzyme for testing React components
  • chai as assertion library

What and How to test

We try to keep the structure of a test consistent by defining the actual and the expected values as variables and then pass them into the asserting function:

it('shows the preferred assertion style', () => {
  const actual = doSomethingToGetTheActualValue();
  const expected = 0;

  assert.equal(actual, expected);
});

Helper functions

You should unit test helper functions.

Reducers

You should unit test reducers for

  • the initial state
  • state handling for each action

Example:

import chai from 'chai';
import counter from 'reducers/counter';

const { assert } = chai;

describe('counter reducer', () => {
  it('provides the initial state', () => {
    const actual = counter(undefined, {});
    const expected = 0;

    assert.equal(actual, expected);
  });

 it('handles the COUNTER_PLUS action', () => {
    const actual = counter(1, { type: 'COUNTER_PLUS' });
    const expected = 2;

    assert.equal(actual, expected);
  });

  it('handles the COUNTER_MINUS action', () => {
    const actual = counter(2, { type: 'COUNTER_MINUS' });
    const expected = 1;

    assert.equal(actual, expected);
  });
});

Routes

You should test routes for whether they are accessable.

Example:

import chai from 'chai';
import server from 'server';

const { assert } = chai;

// example for successful response
describe('GET /', () => {
  it('responds with 200', done => {
    server.injectThen('/')
      .then(response => {
        const actual = response.statusCode;
        const expected = 200;

        assert.equal(actual, expected);
        done();
      });
  });
});

// example for other response statuses
describe('GET a not defined route', () => {
  it('responds with 404', done => {
    server.injectThen('/not-defined')
      .then(response => {
        const actual = response.statusCode;
        const expected = 404;

        assert.equal(actual, expected);
        done();
      });
  });
});

React components

Test React components whenever it is needed (To Be Defined).

Clone this wiki locally