- [Instructor] Test cases help us test different scenarios
in which our code will be used.
Wouldn't it be awesome to define the context
in which a test is running and group related tests?
This will surely be beneficial to properly reading
and interpreting test results.
Using Jest, we have the ability to group tests
using describe blocks.
This way we can group and label related tests
and also define dependencies per test group.
Let's dive into our code
and see how we can use describe blocks
to group all the tests written so far.
In this exercise,
we'll be creating a group for the string tests,
another group for the numeric tests
and a third group for the objects and arrays tests.
Inside the test folder,
create a new file with the name organized.js.
Inside this test suite, let's import all properties
contained in the utilities module.
Next, we create a container describe block
to hold all our test cases.
Let's now nest three describe blocks
inside the parent block.
One for each set of tests we have written so far.
Good.
Let's now copy the tests from each test file written so far
into their respective blocks.
With all the tests in their describe blocks, save the file
and run the new test suite on the command line.
As expected, our tests pass.
But this is not the main story here.
Look at how tests are now organized into sections
and hierarchies making it easier to interpret.
This arrangement also indicates the order
in which the tests are ran.
To confirm this,
let's try switching the position of the describe blocks.
Back in the editor,
let's switch the position of the numeric test
for that of the object test group.
Now save the file and return to the CLI once again
to run the test.
As observed, this time the order changes,
and we see the arrays and objects tests
running before the numeric tests.
This is a very useful tool
when the order in which the tests run
matter to the testing process.
Using describe blocks, we can easily group tests
and introduce context into our testing process.
Describe blocks also helps us to use Jest set up
and tear down functions
to manage dependencies for test groups.