Skip to content

Test Blocks

licarm edited this page Jan 20, 2021 · 15 revisions

Code block types are important for how the tests are organized. Below are descriptions of the different test block types:

Describe Blocks

describe blocks are methods used for grouping any number of tests, which are run via it or test methods.

The format of a describe block is as follows:

describe('name of describe block test', () => {
  it('name of it block test', () => {
    test body
  });
  it('name of it block test', () => {
    test body
  });
  it('name of it block test', () => {
    test body
  });
});

It Blocks

An it block is a test case. Structure it blocks by following these conventions, where each step is separated by one blank line:

  1. Test setup all together
  2. Test action(s) all together
  3. Test assertions all together
// bad example
it('invokes onClick handler when clicked', () => {
  const handler = jest.fn();
  const wrapper = mount(<Chip onClick={handler} />);
  wrapper.find("Chip").simulate("click");
  expect(handler).toHaveBeenCalledTimes(1);
});

// good example
it('invokes onClick handler when clicked', () => {
  const handler = jest.fn();

  const wrapper = mount(<Chip onClick={handler} />);
  wrapper.find('Chip').simulate('click');

  expect(handler).toHaveBeenCalledTimes(1);
});
Clone this wiki locally