Calling it identifies each individual test, but by itself, it does not tell Mocha how your test suite is structured. How you use the describe call is what gives the structure to your test suite. Here are some of the things that describe use to structure your test suite for you. Here is an example of a test suite simplified for discussion:
function Foo() { } describe("Foo", function () { var foo; beforeEach(function () { foo = new Foo(); }); describe("#clone", function () { beforeEach(function () {
Suppose Foo and Bar are full classes. Foo has clone and equals methods. Bar has a clone . The structure described above is one of the possible ways to structure tests for these classes.
(The designation # used by some systems (for example, jsdoc) to indicate an instance field. Therefore, when used with a method name, it indicates the method called by the class instance (and not the class that is called for the class itself). The test suite will also work without presence # .)
Provide Banners
Some of Mocha's reporters show the names you give describe in the reports they create. For example, a spec reporter (which you can use by running $ mocha -R spec ) will report:
Foo
Help Select Parts to Run
If you want to run only some of the tests, you can use the --grep option. Therefore, if you only care about the Bar class, you can make $ mocha -R spec --grep Bar and get the result:
Bar
Or if you only care about clone methods for all classes, then $ mocha -R spec --grep '\bclone\b' and get the result:
Foo
The value specified in --grep is interpreted as a regular expression, so when I pass \bclone\b , I ask only the word clone , and not things like clones or cloned .
Provide hooks
In the above example, the calls to beforeEach and afterEach are intercepts. Each hook affects it calls, which are inside the describe call, which is the parent of the hook. Various hooks:
beforeEach , which is executed before each individual it inside the describe call.
afterEach , which runs after each separate it inside the describe call.
before , which starts once before any of it is executed inside the describe call.
after , which starts once after running an individual it inside the describe call.
These hooks can be used to obtain resources or create the data structures necessary for testing, and then to free resources or destroy these structures (if necessary) after the tests are completed.
The snippet that you will show at the end of your question will not lead to an error, but in fact it does not contain any test, because the tests are determined by it .