addons-frontend

Testing

We want to maintain a project with a high coverage (aiming for 100%). Our main coverage criterion is branch coverage, that is making sure all possible code branches are covered by test. At an absolute bare minimum, we require 100% line coverage. Codecov monitors our test coverage for every Pull Request. You can make sure you haven’t decreased coverage by checking the Pull Request on GitHub. You can also generate a coverage report locally.

General guidelines

Jest

Jest is our main testing framework. Please refer to the README section about running the test suite to know how to run the tests. Below are a few rules regarding Jest:

When creating a new test file, start with a describe() block that takes the current file name as first argument. This makes easy to find/edit a failing test case as Jest will display the test file in its output:

describe(__filename, () => {});

Spies/Stubs/Mocks and Sinon.JS/Jest

We are in the process of moving all of our mocking from sinon to Jest. Any new tests, or tests that are being updated, should use Jest for mocking in place of sinon.

Testing reducers and sagas

For sagas/reducers, there are two useful helpers: dispatchClientMetadata() and dispatchSignInActions() (tests/unit/helpers) that should be used to initialize state in a realistic manner. The former is used to obtain a non-authenticated state while the latter returns an authenticated state.

When you need a errorHandler or a errorHandlerId, use the createStubErrorHandler() helper from tests/unit/helpers.

When asserting for exceptions/errors, do not use ES6 shorthand functions/implicit return functions. Instead, you should make visible the method/function that should thrown an exception:

expect(() => {
  methodThatThrowsAnError();
}).toThrow(/expected error message/);

When testing sagas, use an action creator to construct the expected actions that should be called by the saga under test:

const expectedLoadAction = autocompleteLoad(results);

await sagaTester.waitFor(expectedLoadAction.type);
mockApi.verify();

const loadAction = sagaTester.getCalledActions()[2];
expect(loadAction).toEqual(expectedLoadAction);

Testing UI components

We use React Testing Library for testing UI components (React components). Below are a few rules regarding our use of React Testing Library: