I thought I knew what describe was for. But as almost everything under-used, I didn’t really knew what I could REALLY do with it. ;)

It proved itself more useful than I thought. Besides grouping of tests - the thing I’ve used it before (which can be really cool and useful, too!), it has one great use-case - it gives you the ability to run beforeAll/afterAll (or beforeEach/afterEach) setup “hooks” (too much React Hooks?!) per describe function.

This is really helpful when you have a more complex test scenarios. Like setting up some mocks to validate the success and the error paths. You can setup the appropriate mock depending on what you want to test.

Here is a quick code sample:

afterAll(() => {
  // restore the default mock after all tests
  setupMock();
});

describe("success", () => {
  beforeAll(() => {
    setupMock({ status: "OK" });
  });

  it("should succeed of course ;)", () => {
    // write your magical code here!
  });
});

describe("error", () => {
  beforeAll(() => {
    setupMock({ status: "ERROR" });
  });

  it("expect only errors here... :(", () => {
    // the "bad" code goes here...
  });
});

I’m not suggesting this is the only valid use-case of describe, but it was something I actually needed in my job. ;)

Think twice before using those “hooks” (beforeAll, afterAll), they can be easily overused.