Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is describe() in Mocha

I am trying to get my hands dirty with Mocha and here is the example I saw from documentation:

var assert = require("assert")
describe('Array', function(){
  describe('#indexOf()', function(){
    it('should return -1 when the value is not present', function(){
      assert.equal(-1, [1,2,3].indexOf(5));
      assert.equal(-1, [1,2,3].indexOf(0));
    })
  })
})

Most of the examples show the second describe statement

  describe('#indexOf()', function(){

starting with # in #indexOf(). What is the significance of this #? Why can this not be written simply as indexOf? Where can I get a fundamental understanding of how the describe works?

PS: I looked at the documentation at http://visionmedia.github.io/mocha/#interfaces but can't figure out how these came into picture and how are these interfaces processed.

Thx

like image 429
Kiran Avatar asked Jan 25 '14 17:01

Kiran


People also ask

What is the difference between the describe and it call in Mocha?

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.

What is describe in JS?

Describe is a function in the Jasmine testing framework. It simply describes the suite of test cases enumerated by the "it" functions. Also used in the mochajs framework.

What is beforeEach in Mocha?

The beforeEach method is a feature (hook) in test libraries that you can use to set preconditions for each test. Just like the name suggests, it runs before each test in your test suite. For example, the beforeEach method will run four times in a suite with four tests.


2 Answers

the bdd syntax of mocha is a inspired in ruby's RSpec, therefore you'd find the best tips for mocha's conventions searching for RSpec, here's a good place to start:

http://betterspecs.org/

In particular the # is mentioned there:

For instance, use the Ruby documentation convention of . (or ::) when referring to a class method's name and # when referring to an instance method's name.

like image 133
Benja Avatar answered Sep 29 '22 00:09

Benja


Looking at the source code of Mocha, I don't see it doing anything significant with a # appearing in the first parameter of describe.

This being said, in some documentation systems for JavaScript (for instance, jsdoc), the use of a # indicates that what follows belongs to an object instance, rather than to the object's class. So Foo#bar would be something you could call like this:

var foo = new Foo();
foo.bar(...);

Not like this:

Foo.bar(...)

The latter would be represented as Foo.bar. So it would make sense in a test suite to use the same kind of notation to distinguish whether the methods being tested belong to instances or classes.

like image 28
Louis Avatar answered Sep 28 '22 22:09

Louis