Search code examples
node.jsmocha.jsfunctional-testingchai

Functional Site Testing using Mocha and Chai for Nodejs


I am having difficulty understanding the following code that utilizes mocha and chai for site testing in nodejs:

suite('Global Tests', function(){
        test('page has a valid title', function(){
                assert(document.title && document.title.match(/\S/) &&
                        document.title.toUpperCase() === 'AHC');
        });
});

What is confusing is how this code is working yet does not seem to follow the chai documentation for assert. Where the format is as follows:

assert(expression, message)


Solution

  • It is just not passing the second parameter (the message). This is not invalid is just makes it more difficult to determine why the test failed. If you wanted to make the test cleaner you could break the expression into three asserts and pass a message about what the assert if verifying.

    suite('Global Tests', function(){
      test('page has a valid title', function(){
        assert(document.title, 'document title is not defined');
        assert(document.title.match(/\S/), 'document title does not have a single character');
        assert(document.title.toUpperCase() === 'AHC', 'Document title does not equal AHC');
        });
    });