Javascript
/
Testing
- 1 Fundamentals 11
-
Hello World
-
Code structure
-
Use strict
-
Variables
-
Data types
-
Type conversions
-
Maths
-
Comparitions
-
Conditional
-
Loops
-
Functions
- 2 Testing 2
-
Mocha
-
Nested describe
- 3 Objects 4
-
Basics
-
Reference
-
Methods
-
Constructor
- 4 Types 5
-
Primitives
-
Numbers
-
Strings
-
Arrays
-
Json
- 5 Classes 3
-
Constructor
-
Binding
-
Inheritance
/
Nested describe
➟
➟
Last update: 25-01-2022
Nested
! The nested describe are used to subgroup tests.
/**
* Nested describe
*
* To group tests use nested describe.
*/
function isEven(n) {
return n%2 == 0;
}
function isOdd(n) {
return n%2 != 0;
}
/**
* Tests
*/
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);
});
});
});
/*
npm test
Number functions
isEven
Check if number is even
isOdd
Check if number is odd
*/
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.
*/
function isEven(n) {
return n%2 == 0;
}
function isOdd(n) {
return n%2 != 0;
}
/**
* Tests
*/
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);
});
});
});
/*
npm test
Test functions
-- Test Suite start
isEven
-- Test Case start
Check even
-- Test Case start
isOdd
-- Test Case start
Check odd
-- Test Case start
-- Test Suite End
*/
➥ Questions github Testing