Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is role of the suite function in Mocha?

I read the book Web Development with Node.js and Express. And there is used the function suite().

var assert = require('chai').assert;
suite('tests', function () {
  // set of tests
});

I don't understand where it comes from. I can't find any documentation about this function. Seems that it looks and has same functionality like the describe() function in Mocha.

like image 650
Andr1i Avatar asked Feb 14 '18 00:02

Andr1i


People also ask

What does it () do in Mocha?

In Mocha, the it() function is used to execute individual tests. It accepts a string to describe the test and a callback function to execute assertions. Calls to it() are commonly nested within describe() blocks.

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 the difference between Mocha and chai?

Mocha is a JavaScript test framework running on Node. js and in the browser. Mocha allows asynchronous testing, test coverage reports, and use of any assertion library. Chai is a BDD / TDD assertion library for NodeJS and the browser that can be delightfully paired with any javascript testing framework.

What is context in Mocha?

ctx is the context in effect where the describe call appears, which happens to be the same context as this in the afterEach call. I would actually recommend not traversing Mocha's internal structures and instead doing something like: describe('main describe', function() { var prop; afterEach(function() { console.


2 Answers

Mocha supports several different ways of writing tests (interfaces) so that you can choose a style that suits your methodology. describe() and suite() essentially do the same thing: they let you label and group together a set of tests; the grouped tests are organised under a common label in the output and can use common setup and teardown functions.

The choice of which function to use depends on whether you are using a Behaviour Driven Development (BDD) methodology (where you describe() the behaviour you want it() to do), or Test Driven Development (TDD), where you define a suite() of test()s you want your code to pass. You should choose whichever style you feel makes your code more readable.

Here's a blog explaining the Difference Between TDD and BDD with regard to test design.

like image 162
JRI Avatar answered Oct 11 '22 17:10

JRI


Documentation can be found on the mocha website: https://mochajs.org/#tdd

suite is the TDD version of describe. You generally use it to describe and isolate the functionality/features/behaviour that you are going to test.

like image 27
Fiddles Avatar answered Oct 11 '22 18:10

Fiddles