Adding describe() for individual methods

Now, in the case of utils.test.js (refer to the previous screenshot), we have one test per method, but if you have a lot of tests that are targeting a complex method, it's best to wrap that in its own describe block. We can nest describe blocks and tests in any way we like. For example, right inside utils just after the describe statement, we can call describe again. We can pass a new description. Let's use # (pound sign) followed by add:

describe('Utils', () => {

describe('#add')

The # (pound sign) followed by the method name is the common syntax for adding a describe block for a specific method. Then we can provide that callback function:

describe('Utils', () => {

describe('#add', () => {

})

Then, we can take any tests we want to add into that group, cut them out, and paste them in:

describe('Utils', () => {

describe('#add', () => {
it('should add two numbers', () => {
var res = utils.add(33, 11);

expect(res).toBe(44).toBeA('number');
});
});

Then I can save the file. This will rerun the test suite and now we have test output that's even more scannable:

It's super easy to find the utils add method tests because they're clearly labelled. Now you could go as crazy or as uncrazy with this as you want. There really is no hard-and-fast rule for how often to use describe to structure your tests. It's really up to you to figure out what makes sense given the amount of tests you have for a method or a file.

In this case, we have quite a few tests in the file so it definitely makes sense to add that utils block. And I just wanted to show you you could nest them, so I added it for add as well. If I was writing this code, I probably wouldn't add a second layer of tests, but if I had more than one test per method, I definitely would add a second describe block.