Javascript
/
Testing
- 1 Fundamentals 11
-
Hello World S
-
Code structure S
-
Use strict S
-
Variables S
-
Data types S
-
Type conversions S
-
Maths S
-
Comparitions S
-
Conditional S
-
Loops S
-
Functions S
- 2 Testing 2
-
Mocha S
-
Nested describe S
- 3 Objects 4
-
Basics S
-
Reference S
-
Methods S
-
Constructor S
- 4 Types 5
-
Primitives S
-
Numbers S
-
Strings S
-
Arrays S
-
Json S
- 5 Classes 3
-
Constructor S
-
Binding S
-
Inheritance S
S
R
Q
Javascript Testing Nested Describe
Group tests, with nested describe Something to execute before tests describe('Tests', function() describe('isEven', function() {}); before(() => { ... });
Nested
The nested describe are used to subgroup tests.
/**
* Group tests by nesting describe()
*
* cd javascript-pages/main/testing/nested_describe/
* npm test
*/
function isEven(n) {
return n%2 == 0;
}
function isOdd(n) {
return n%2 != 0;
}
let assert = require('assert');
describe('Number functions', function() {
describe('isEven', function() {
it('Check if number is even', function() {
assert.equal(isEven(6), true);
assert.equal(isEven(5), false);
});
});
describe('isOdd', function() {
it('Check if number is odd', function() {
assert.equal(isOdd(8), false);
assert.equal(isOdd(11), true);
});
});
});
Before
We can set before/after functions.
/**
* Before/After
*
* We can set functions that are executed before starting the tests
* Also, we can set functions before every test
*
* cd javascript-pages/main/testing/nested_describe/
* npm test
*/
function isEven(n) {
return n%2 == 0;
}
function isOdd(n) {
return n%2 != 0;
}
let assert = require('assert');
describe('Test functions', function() {
before(() => {console.log('-- Test Suite start')});
after(() => {console.log('-- Test Suite End')});
beforeEach(() => {console.log('\t -- Test Case start')});
afterEach(() => {console.log('\t -- Test Case start')});
describe('isEven', function() {
it('Check even', () => {
assert.equal(isEven(6), true);
assert.equal(isEven(5), false);
});
});
describe('isOdd', function() {
it('Check odd', () => {
assert.equal(isOdd(8), false);
assert.equal(isOdd(11), true);
});
});
});
➥ Questions
Last update: 61 days ago