Interfaces
In the previous tutorial we looked at the different command-line instructions available in mocha. This tutorial will introduce you to different interfaces that are available to you in mocha.
Mocha's interface enables developers to choose their style of DSL. There are BDD, TDD, Exports, QUnit and Require-style interfaces in Mocha.
Bdd
The BDD interface provides us with describe(), context(), it(), specify(), before(), after(), beforeEach(), and afterEach().
The context() is an alias for describe(), and it behaves the same way as the describe(); it provides a way to keep tests easier to read and organized. Similarly, specify() is just an alias for it().
The example below is written using the BDD interface.
describe('Array', function() {
before(function() {
// ...
});
describe('#indexOf()', function() {
context('when not present', function() {
it('it should not throw an error', function() {
(function() {
[1, 2, 3].indexOf(4);
}.should.not.throw());
});
it('it should return -1', function() {
[1, 2, 3].indexOf(4).should.equal(-1);
});
});
context('when present', function() {
it('it should return the index where the element first appears in the array', function() {
[1, 2, 3].indexOf(3).should.equal(2);
});
});
});
});
Tdd
The TDD interface provides us with suite(), test(), suiteSetup(), suiteTeardown(), setup(), and teardown():
suite('Array', function() {
setup(function() {
// ...
});
suite('#indexOf()', function() {
test('it should return -1 when not present', function() {
assert.equal(-1, [1, 2, 3].indexOf(4));
});
});
});
Exports
The Exports interface is similar to Mocha?s predecessor expresso. The keys before, after, beforeEach, and afterEach are special-cased, the object values are suites while the function values are test-cases:
Qunit
The QUnit-inspired interface will match the ?flat? look of QUnit, where the test suite will simply be defined before the test-cases. Just like the TDD, QUnit uses suite() and test(), but it resembles BDD, QUnit als contains before(), after(), beforeEach(), and afterEach(). Here is an example:
function ok(expr, msg) {
if (!expr) throw new Error(msg);
}
suite('Array');
test('#length', function() {
var arr = [1, 2, 3];
ok(arr.length == 3);
});
test('#indexOf()', function() {
var arr = [1, 2, 3];
ok(arr.indexOf(1) == 0);
ok(arr.indexOf(2) == 1);
ok(arr.indexOf(3) == 2);
});
suite('String');
test('#length', function() {
ok('foo'.length == 3);
});
Require
The require interface enables you to require the describe and friend words directly, just by using require and then call them whatever you want. This interface is equally useful when you want to avoid global variables in your tests.
It should be noted that the require interface cannot be run via the node executable, and has to be run via mocha:
var testCase = require('mocha').describe;
var pre = require('mocha').before;
var assertions = require('mocha').it;
var assert = require('chai').assert;
testCase('Array', function() {
pre(function() {
// ...
});
testCase('#indexOf()', function() {
assertions(' it should return -1 when not present', function() {
assert.equal([1, 2, 3].indexOf(4), -1);
});```
});
});
- New Content published on w3resource :
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework