0

I have found that expect() works within describe(). So the function test() is not necessary, if I see it right. Do I see it right?

In other words, is it enough to write that:

const sum = require('./sum');

describe('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3);
});

Or does that bring advantages:


const sum = require('./sum');

describe('test sum', () => {
  test('adds 1 + 2 to equal 3', () => {
    expect(sum(1, 2)).toBe(3);
  });
});
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
astridx
  • 6,581
  • 4
  • 17
  • 35
  • Try a _failing_ test and see where it gets you. A single test isn't a good example because you can just use `test` (or `it`) _without_ `describe`. – jonrsharpe Oct 16 '21 at 21:38
  • Does this answer your question? [What is the difference between describe and it in Jest?](https://stackoverflow.com/questions/32055287/what-is-the-difference-between-describe-and-it-in-jest) – jonrsharpe Oct 16 '21 at 21:41

1 Answers1

2

describe(name, fn) creates a block that groups together several related tests.

Detail: https://jestjs.io/docs/api#describename-fn

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Tony Yip
  • 705
  • 5
  • 14
  • Thanks for your answer. But I want to know, if it is fine, to use my fist example without the test()-methode, because it works - it also show failing tests. – astridx Oct 16 '21 at 18:06
  • @astridx I never try this before and I wouldn't do this. If you only want one layer, use `test` instead of `describe`. I think the test report might get wrong if you use your first example – Tony Yip Oct 16 '21 at 18:10