mardi 23 février 2016

Unit Testing using Mocha, gulp and Webpack for javascript AMD module

I am working on code coverage using Istanbul. I am new to this library. I am checking coverage for sample.js for the function inject. But somehow it is not including this function in the coverage as shown below.

gulp task for istanbul

gulp.task('test-cov', function () {
    return gulp.src(['./../_src/scripts/ui/*.js'])
        // Covering files
        .pipe(istanbul({includeUntested: true}))
        .pipe(istanbul.hookRequire())
        .on('finish', function () {
            gulp.src('./../tests-browser/*.js')
                .pipe(istanbul.writeReports({
                    dir: './../unit-test-coverage',
                    reporters: ['lcov'],
                    reportOpts: {dir: './../unit-test-coverage'}
                }));
        });
});

sample-test.js

describe('Conversation Response object', function() {
    jsdom();
    var eleMgr, conversation, almeBodyDiv;
    before(function (done) {
        define(['ElementManager', 'ConversationHistoryRenderer'],
            function (element, conv) {
                eleMgr = element;
                conversation = conv;
                almeBodyDiv = eleMgr.create('div', null, 'alme-body', null, 'height: 258px;');
                done();
            });
    });


    it('A new input box should be appended', function () {
        var textBox = eleMgr.create('div', 'alme-chat-history', 'alme-chat-history', null, 'position: relative;');
        conversation.inject(almeBodyDiv);
        expect(almeBodyDiv.innerHTML).to.equal('<div id="alme-chat-history" class="alme-chat-history" ' +
            'style="position: relative;"></div>');
    });

    it('It should have class alme-chat-history' , function() {
        conversation.inject(almeBodyDiv);
        expect(almeBodyDiv.firstChild.className).to.equal('alme-chat-history');
    });
});

sample.js

define(
[
    'MessageRenderer',
    'ElementManager'
],
function (
    messageRenderer,
    eleMgr
) {

    var injectToEle = function(parentContainer) {
        var textBox = eleMgr.create('div', TEXT_BOX_ID, TEXT_BOX_ID, null, 'position: relative;');

        agent.addEventListener(agent.supportedEvents.ResponseReceived, handleResponse);
        agent.addEventListener(agent.supportedEvents.HistoryLoaded, historyLoaded);
        agent.addEventListener(agent.supportedEvents.ErrorReceived, handleError);

        parentContainer.appendChild(textBox);
    };
return {
        inject: injectToEle
        };
});

Code Coverage Results

---------------------------------|----------|----------|----------|----------|----------------|
File                             |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
---------------------------------|----------|----------|----------|----------|----------------|
 ui/                             |          |          |          |          |                |
        Sample.js    |        8 |      100 |        0 |        8 |... 70,71,72,75 |
    Sample1.js               |      2.6 |        0 |        0 |      2.6 |... 168,169,172 |
    Sample2.js               |    12.24 |        0 |        0 |    12.24 |... 140,141,146 |
    Sample3.js               |     7.84 |        0 |        0 |     7.84 |... 170,171,179 |
    Sample4.js               |     7.69 |        0 |        0 |     7.69 |... 67,68,70,75 |
---------------------------------|----------|----------|----------|----------|----------------|
All files                        |     7.28 |        0 |        0 |     7.31 |                |
---------------------------------|----------|----------|----------|----------|----------------|


=============================== Coverage summary ===============================
Statements   : 7.28% ( 48/400 )
Branches     : 0% ( 0/1 )
Functions    : 0% ( 0/40 )
Lines        : 7.31% ( 48/400 )
================================================================================

Please let me know if I am missing something here. Thanks for the help in advance.

Aucun commentaire:

Enregistrer un commentaire