What is the role of describe() in Mocha?
Solution 1:
The it
call identifies each individual tests but by itself it
does not tell Mocha anything about how your test suite is structured. How you use the describe
call is what gives structure to your test suite. Here are some of the things that using describe
to structure your test suite does for you. Here's an example of a test suite, simplified for the purpose of discussion:
function Foo() {
}
describe("Foo", function () {
var foo;
beforeEach(function () {
foo = new Foo();
});
describe("#clone", function () {
beforeEach(function () {
// Some other hook
});
it("clones the object", function () {
});
});
describe("#equals", function () {
it("returns true when the object passed is the same", function () {
});
it("returns false, when...", function () {
});
});
afterEach(function () {
// Destroy the foo that was created.
// foo.destroy();
});
});
function Bar() {
}
describe("Bar", function () {
describe("#clone", function () {
it("clones the object", function () {
});
});
});
Imagine that Foo
and Bar
are full-fledged classes. Foo
has clone
and equals
methods. Bar
has clone
. The structure I have above is one possible way to structure tests for these classes.
(The #
notation is used by some systems (like for instance, jsdoc) to indicate an instance field. So when used with a method name, it indicates a method called on an instance of the class (rather than a class method, which is called on the class itself). The test suite would run just as well without the presence of #
.)
Provide Banners
Some of Mocha's reporters show the names you give to describe
in the reports they produce. For instance, the spec
reporter (which you can use by running $ mocha -R spec
), would report:
Foo
#clone
✓ clones the object
#equals
✓ returns true when the object passed is the same
✓ returns false, when...
Bar
#clone
✓ clones the object
4 passing (4ms)
Help Select Parts to Run
If you want to run only some of the tests, you can use the --grep
option. So if you care only about the Bar
class, you can do $ mocha -R spec --grep Bar
, and get the output:
Bar
#clone
✓ clones the object
1 passing (4ms)
Or if you care only about the clone
methods of all classes, then $ mocha -R spec --grep '\bclone\b'
and get the output:
Foo
#clone
✓ clones the object
Bar
#clone
✓ clones the object
2 passing (5ms)
The value given to --grep
is interpreted as a regex so when I pass \bclone\b
I'm asking only for the word clone
, and not things like clones
or cloned
.
Provide Hooks
In the example above the beforeEach
and afterEach
calls are hooks. Each hook affects the it
calls that are inside the describe
call which is the parent of the hook. The various hooks are:
beforeEach
which runs before each individualit
inside thedescribe
call.afterEach
which runs after each individualit
inside thedescribe
call.before
which runs once before any of the individualit
inside thedescribe
call is run.after
which runs once after all the individualit
inside thedescribe
call are run.
These hooks can be used to acquire resources or create data structures needed for the tests and then release resources or destroy these structures (if needed) after the tests are done.
The snippet you show at the end of your question won't generate an error but it does not actually contain any test, because tests are defined by it
.
Solution 2:
To my knowledge, describe is really just there for humans... So we can see different areas of the app. You can nest describe n levels deep.
describe('user',function(){
describe('create',function(){}
});
Solution 3:
It's hard to add to Louis' excellent answer. There are a couple of advantages of the describe block that he didn't mention which are the skip
and only
functions.
describe.skip(...) {
...
}
will skip this describe and all its nested describe and it functions while:
describe.only(...) {
...
}
will only execute that describe and its nested describe and it functions. The skip()
and only()
modifiers can also be applied to the it() functions.
Solution 4:
Describe is just used for the sake of understanding the purpose of the tests , it is also used to logically group the tests . Lets say you are testing the database API's , all the database tests could come under the outer describe , so the outer describe logically groups all the database related . Lets say there are 10 database related API's to test , each of the inner describe functions defines what those tests are ....