【问题标题】:How to fix issue of code coverage not finding functions when run tests do?如何解决运行测试时找不到函数的代码覆盖问题?
【发布时间】:2019-08-06 15:08:35
【问题描述】:

运行我的测试时,它们都通过了。运行代码覆盖率 4 时,由于“未定义”功能而失败。它们被定义(当它们通过时,测试清楚地表明)。我无法获得包含在这些测试中测试的 4 个函数的文件的代码覆盖率,因为我需要知道这些函数是否经过全面测试。

我在 PHPStorm。这些测试的 jsTestDriver 有一个我制作的文件,Sinon v7.3.2、jQuery v1.11.1 和 D3.js v5.9.7 作为依赖项。

load:
  - buttonActions.js
  - tests/dependencies/jquery.js
  - tests/dependencies/sinon732.js
  - tests/dependencies/d3.js

test:
  - tests/Functional/javascriptTests/actionsTestButtonActionsFunctions.test.js

timeout: 30

来自 buttonActions.js 的部分代码:

function _hideOrShow(table){

    var classNames = table[0]["attributes"][0]['nodeValue'];
    var secondClass = classNames.replace("sortable table-striped table-responsive ",'');

if (secondClass === "veryHidden" ) {
    table.removeClass("veryHidden").addClass("veryShown");
} else {
    table.removeClass("veryShown").addClass("veryHidden");
}

}

我测试的部分代码:

 ActionsTestButton = TestCase("ActionsTestButtonActions");

 ActionsTestButton.prototype.setUp = function() {
//stuff done here. Not important
}

ActionsTestButton.prototype.testHideOrShow = function () {

var table = {
    class: "sortable table-striped table-responsive veryHidden",
    0: {
        attributes: {
            0: {
                nodeValue: "sortable table-striped table-responsive veryHidden"
            }
        }
    },
    removeClass: function (name){
        table.class = table.class.replace(name, '');
        table[0]["attributes"][0]['nodeValue'] = table.class;
        return {
            addClass: function (name){
                table.class = table.class+name;
                table[0]["attributes"][0]['nodeValue'] = table.class;
            }
        }
    }

 };

assertEquals("sortable table-striped table-responsive veryHidden", table.class);
assertEquals("sortable table-striped table-responsive veryHidden", table[0]["attributes"][0]['nodeValue']);

_hideOrShow(table);

assertEquals("sortable table-striped table-responsive veryShown", table.class);
assertEquals("sortable table-striped table-responsive veryShown", table[0]["attributes"][0]['nodeValue']);

_hideOrShow(table);

 assertEquals("sortable table-striped table-responsive veryHidden", table.class);
 assertEquals("sortable table-striped table-responsive veryHidden", table[0]["attributes"][0]['nodeValue']);

 };

     ActionsTestButton.prototype.tearDown = function() {
document.getElementById.restore();
window.$.restore();
window.d3.select.restore();

 };

错误消息我得到运行代码覆盖率:

     line 178:4 mismatched input 'const' expecting RBRACE
     line 1022:12 no viable alternative at input 'throws'
     line 1236:13 mismatched input 'throws' expecting Identifier
     line 1236:31 extraneous input 'throws' expecting LPAREN
     line 3998:12 no viable alternative at input 'function'
     line 5123:14 missing Identifier at 'in'
     line 5123:17 no viable alternative at input '='
     line 5413:8 no viable alternative at input '}'
     line 5415:30 no viable alternative at input ']'

     ReferenceError: _removeIcons is not defined
at b.ActionsTestButton.testPrivateRemoveIcons (http://127.0.0.1:9876/test/tests/Functional/javascriptTests/actionsTestButtonActionsFunctions.test.js:179:5)
at TestResultIterator.runTest (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:201:28)
at TestResultIterator.runNext (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:292:10)
at InstrumentedTestCaseRunner.run (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:250:27)
at InstrumentedTestCaseRunnerPlugin.runTestConfiguration (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:221:20)



     ReferenceError: _reAddIcon is not defined
at b.ActionsTestButton.testPrivateReAddIcons (http://127.0.0.1:9876/test/tests/Functional/javascriptTests/actionsTestButtonActionsFunctions.test.js:196:5)
at TestResultIterator.runTest (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:201:28)
at TestResultIterator.runNext (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:292:10)
at InstrumentedTestCaseRunner.run (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:250:27)



     ReferenceError: _removeIcons is not defined
at b.ActionsTestButton.testRemoveThenAddRevertsBackToBlock (http://127.0.0.1:9876/test/tests/Functional/javascriptTests/actionsTestButtonActionsFunctions.test.js:214:5)
at TestResultIterator.runTest (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:201:28)
at TestResultIterator.runNext (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:292:10)
at InstrumentedTestCaseRunner.run (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:250:27)



     ReferenceError: _hideOrShow is not defined
at b.ActionsTestButton.testHideOrShow (http://127.0.0.1:9876/test/tests/Functional/javascriptTests/actionsTestButtonActionsFunctions.test.js:251:5)
at TestResultIterator.runTest (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:201:28)
at TestResultIterator.runNext (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:292:10)
at InstrumentedTestCaseRunner.run (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:250:27)



     Process finished with exit code 0

我只想获取此文件的代码覆盖率。它适用于我的所有其他文件测试。只是没有这个。

【问题讨论】:

  • 似乎单元测试是在浏览器的上下文中运行的,load 下的每个文件都可以访问全局命名空间。您的代码覆盖工具可能会在入口点文件中使用 importrequire 语句来访问外部模块。这意味着如果actionsTestButtonActionsFunctions.test.js 不导入buttonActions.js,它将无法访问该文件的方法。
  • @SteamDev 我的任何文件中都没有 require 并且它们可以工作 :( 如果我尝试包含“require”,我会收到错误。jsTestDriver 是正在运行的文件,它会在之前加载 buttonActions测试开始了,所以我认为没关系(这是用于测试,但代码覆盖率似乎没有看到)。
  • 抱歉,require 是 CommonJS (NodeJS) 特有的,如果这不是节点项目,请尝试在 test.js 文件中使用 ES6 import 来加载未定义的引用。
  • @SteamDev,它只会导致语法错误并使我的测试根本无法运行:(我认为 jstd 不喜欢它运行的文件中的 import 语句

标签: javascript unit-testing phpstorm code-coverage js-test-driver


【解决方案1】:

JSTestDriver 中的代码覆盖率不喜欢letconst。它不会对包含它的任何文件运行代码覆盖,并且不允许包含它的文件中的函数。

对于我的特殊情况,我有一个使用 const 的函数。使用它的函数在我正在测试的函数中没有被调用,因此根本没有被测试过。这意味着测试通过了。但是,在同一个文件中足以使代码覆盖率中断。

我的解决方案?将letconst 都更改为var。从语义上讲,这可能不是最好的主意,但就我而言,它对我的​​代码或行为没有明显的影响。

【讨论】:

    猜你喜欢
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 2020-12-07
    • 2019-09-01
    • 2023-04-07
    • 1970-01-01
    • 2020-03-30
    相关资源
    最近更新 更多