【问题标题】:How to include modules for code coverage for unit testing?如何包含用于单元测试的代码覆盖率模块?
【发布时间】:2015-05-24 12:05:19
【问题描述】:

我的假设是使用 Intern 测试的任何模块都将自动被伊斯坦布尔的代码覆盖率覆盖。由于我不知道的原因,我的模块没有被包括在内。

我是:

  • 运行 Intern 1.6.2(本地安装 npm)
  • 测试 NodeJS 代码
  • 使用回调,而不是承诺
  • 使用 CommonJS 模块,而不是 AMD 模块

目录结构(只显示相关文件):

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;    
                }));
            }));
        });
    });
});

输出:

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

输出(失败时):

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

输出(移除 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 |
------------------------------------------+-----------+-----------+-----------+-----------+

我的模块通过了测试,我也可以让它失败。它只是不会出现在代码覆盖率中。我已经完成了托管在 GitHub 上的教程,没有任何问题。

我尝试剖析伊斯坦布尔和实习生的依赖关系。我将console.log 放置在似乎要覆盖的文件通过的地方,但我的模块没有通过。我已经尝试了deferred.callbackdeferred.rejectOnError 的所有变体,代码覆盖率没有任何差异。

此外,对于我使用 deferred.callbackdeferred.rejectOnError 的任何反馈,我们将不胜感激。我对它们的用法仍然有些不确定。

谢谢!

【问题讨论】:

    标签: intern


    【解决方案1】:

    从 Intern 1.6 开始,只有 require('vm').runInThisContext 被挂钩来添加代码覆盖率数据,而不是 require。在 Intern 2.0 中添加了 require 的检测。

    以上代码中callback/rejectOnError的使用是正确的。

    【讨论】:

    • 为了在问题跟踪器中表达清楚的要求,我要求对 CommonJS 模块的代码覆盖率支持?
    • 是的,先生!就是这样。
    猜你喜欢
    • 1970-01-01
    • 2012-01-18
    • 2017-11-18
    • 2021-02-03
    • 2010-10-14
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多