PROGRAMMING

  MINTE9
REMEMBERS




Last update:   23-01-2022

Browser

! Automated tests are written separately, in addition to the code.
 
<!DOCTYPE html>
<html>
<head>
    <link href="https://cdn.jsdelivr.net/npm/mocha@9.1.4/mocha.css" 
        integrity="sha256-a4j/9d0TJl1bbNF1UC8zUh8pnur9RLQIyyaGAVtj8fM=" 
        crossorigin="anonymous" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mocha@9.1.4/mocha.js" 
        integrity="sha256-wl8hYFZuIeOkXAmxbny9ku7nrD7qcAu7ybl5ZzyBG4g=" 
        crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/chai@4.3.4/chai.js" 
        integrity="sha256-80GiP7ACKKHzARzuCmIkx5TLAUSPlcl7cUd9d5wT4ZU=" 
        crossorigin="anonymous"></script>
</head>
<body>
    <script>

        /**
         * Code to be tested
         */
        function isEven(n) {
            return n%2 == 0;
        }

        function isOdd(n) {
            return n%2 != 0;
        }

        /**
         * Tests
         */

        mocha.setup('bdd'); // minimal setup
        let assert = chai.assert; // assert library
        
        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);
            });
        });

    </script>

    <div id="mocha"></div>
    <script>
        mocha.run(); // test run
    </script>

</body>
</html>

Node

! Mocha allows you to use any assertion library you wish.
 
/**
 * Mocha - the core framework: 
 * provide testing functions like describe and it, 
 * and the mai functions to run tests.
 * 
 * Chai - the library with many assertions. 
 * It allows to use a lot of different assertions,
 * beyond the simple assert (from node.js)
 * 
 * npm install --save-dev mocha
 * npm install --save-dev chai
 * 
 * BDD - Behavior Driven Development: tests AND docs AND examples.
 */


/**
 * Code (to be tested, usually in separate file)
 */

function isEven(n) {
    return n%2 == 0;
}

function isOdd(n) {
    return n%2 != 0;
}

/**
 * Tests
 */

let assert = require('assert'); // node.js OR ...
assert = require('chai').assert; // chai

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() { // it.only()
        assert.equal(isOdd(8), false);
        assert.equal(isOdd(11), true);
    });
});

/**
 * Run tests
 * 
 * Add 'scripts' to package.json
 * 
 * cd myproject/
 * npm test
 * 
 *   isEven
 *     Check if number is even
 *
 *   isOdd
 *     Check if number is odd
 * 
 * 
 * To run only one:
 * npm test -- --grep 'isOdd'
 * 
 * Or use it.only() in the test code
 */

// "scripts": { "test": "mocha '**/*.test.js'" }

Questions     github Testing
Nested describe
        A B C D E F
🔔
1/2