Dale
Dale

Reputation: 5677

How to include modules for code coverage for unit testing?

My assumption is that any module tested using Intern will automatically be covered by Istanbul's code coverage. For reasons unknown to me, my module is not being included.

I am:

Directory Structure (only showing relevant files):

plister
|
|--libraries
|  |--file-type-support.js
|
|--tests
|  |--intern.js
|  |--unit
|     |--file-type-support.js
|
|--node_modules
   |--intern

plister/tests/intern.js

define({
    useLoader: {
        'host-node': 'dojo/dojo'
    },
    loader: {
        packages: [
            {name: 'libraries', location: 'libraries'}
        ]        
    },
    reporters: ['console'],
    suites: ['tests/unit/file-type-support'],
    functionalSuites: [],
    excludeInstrumentation: /^(tests|node_modules)\//
});

plister/tests/unit/file-type-support.js

define([
    'intern!bdd',
    'intern/chai!expect',
    'intern/dojo/node!fs',
    'intern/dojo/node!path',
    'intern/dojo/node!stream-equal',
    'intern/dojo/node!../../libraries/file-type-support'
], function (bdd, expect, fs, path, streamEqual, fileTypeSupport) {
    'use strict';

    bdd.describe('file-type-support', function doTest() {
        bdd.it('should show that the example output.plist matches the ' + 
                'temp.plist generated by the module', function () {
            var deferred  = this.async(),
                input     = path.normalize('tests/resources/input.plist'),
                output    = path.normalize('tests/resources/output.plist'),
                temporary = path.normalize('tests/resources/temp.plist');

            // Test deactivate function by checking output produced by 
            // function against test output.
            fileTypeSupport.deactivate(fs.createReadStream(input), 
                    fs.createWriteStream(temporary), 
                    deferred.rejectOnError(function onFinish() {
                streamEqual(fs.createReadStream(output), 
                        fs.createReadStream(temporary), 
                        deferred.callback(function checkEqual(error, equal) {
                    expect(equal).to.be.true;    
                }));
            }));
        });
    });
});

Output:

PASS: main - file-type-support - should show that the example output.plist matches the temp.plist generated by the module (29ms)
1/1 tests passed
1/1 tests passed

Output (on failure):

FAIL: main - file-type-support - should show that the example output.plist matches the temp.plist generated by the module (30ms)
AssertionError: expected true to be false
AssertionError: expected true to be false
0/1 tests passed
0/1 tests passed
npm ERR! Test failed.  See above for more details.
npm ERR! not ok code 0

Output (after removing excludeInstrumentation):

PASS: main - file-type-support - should show that the example output.plist matches the temp.plist generated by the module (25ms)
1/1 tests passed
1/1 tests passed

------------------------------------------+-----------+-----------+-----------+-----------+
File                                      |   % Stmts |% Branches |   % Funcs |   % Lines |
------------------------------------------+-----------+-----------+-----------+-----------+
   node_modules/intern/                   |        70 |        50 |       100 |        70 |
      chai.js                             |        70 |        50 |       100 |        70 |
   node_modules/intern/lib/               |     79.71 |     42.86 |     72.22 |     79.71 |
      Test.js                             |     79.71 |     42.86 |     72.22 |     79.71 |
   node_modules/intern/lib/interfaces/    |        80 |        50 |     63.64 |        80 |
      bdd.js                              |       100 |       100 |       100 |       100 |
      tdd.js                              |     76.19 |        50 |     55.56 |     76.19 |
   node_modules/intern/lib/reporters/     |     56.52 |        35 |     57.14 |     56.52 |
      console.js                          |     56.52 |        35 |     57.14 |     56.52 |
   node_modules/intern/node_modules/chai/ |      37.9 |      8.73 |     26.38 |     39.34 |
      chai.js                             |      37.9 |      8.73 |     26.38 |     39.34 |
   tests/unit/                            |       100 |       100 |       100 |       100 |
      file-type-support.js                |       100 |       100 |       100 |       100 |
------------------------------------------+-----------+-----------+-----------+-----------+
All files                                 |     42.14 |     11.35 |     33.45 |     43.63 |
------------------------------------------+-----------+-----------+-----------+-----------+

My module passes the test and I can make it fail too. It just will not show up in the code coverage. I have done the tutorial hosted on GitHub without any problems.

I tried dissecting the Istanbul and Intern dependencies. I place a console.log where it seems files to be covered go through, but my module doesn't get passed. I have tried every variation of deferred.callback and deferred.rejectOnError with no difference to the code coverage.

Also, any feedback on my use of deferred.callback and deferred.rejectOnError will be greatly appreciated. I am still a little uncertain on their usage.

Thanks!

Upvotes: 0

Views: 1188

Answers (1)

C Snover
C Snover

Reputation: 18766

As of Intern 1.6, only require('vm').runInThisContext is hooked to add code coverage data, not require. Instrumentation of require was added in Intern 2.0.

The use of callback/rejectOnError in the above code is correct.

Upvotes: 0

Related Questions