【问题标题】:How to stop Code Coverage from crashing because of sinon even though tests without code coverage pass?即使没有代码覆盖的测试通过,如何阻止代码覆盖因为 sinon 而崩溃?
【发布时间】:2019-08-06 00:05:07
【问题描述】:

问题:当 sinon 是依赖项时,代码覆盖不起作用。

我目前有 3 个测试文件。运行所有测试导致 17 个测试通过,0 个测试失败。其中 2 个文件需要第 3 个不需要的额外依赖项(我们将这些文件称为 testDates.test.js 和 testButtons.test.js)。其中包括 JQuery、Sinon (v7.3.2) 和 D3。

运行第三个文件的测试有 9 个测试通过,0 个失败。运行代码覆盖率返回 78% 的代码覆盖率(这很好,因为未测试的函数只是返回硬编码数据作为占位符的函数,用于将来进行的实际数据检索)。

返回此代码覆盖率的 jsTestDriver.jstd 是:

 load:

   - allCharts.js

 test:

  - tests/Functional/javascriptTests/actionsTestAllChartsFunctions.test.js

 timeout: 60

(^ 实际需要的文件)

load:

  - allCharts.js

  - tests/dependencies/jquery.js

test:

  - tests/Functional/javascriptTests/actionsTestAllChartsFunctions.test.js

timeout: 60

(^ 检查 jquery 是否是代码覆盖率的问题,不是)

load:

  - allCharts.js

  - tests/dependencies/jquery.js

  - tests/dependencies/sinon732.js

test:

   - tests/Functional/javascriptTests/actionsTestAllChartsFunctions.test.js

timeout: 60

(^ 检查 sinon 是否是代码覆盖率的问题,是的)

最后一次尝试返回错误信息:

com.google.jstestdriver.coverage.CodeInstrumentor$InstrumentationException: error instrumenting C:\Users\smazor\PhpstormProjects\project1\tests\dependencies\sinon732.jsline 179: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 4992:12 mismatched input 'function' expecting RBRACE
line 4992:20 mismatched input ':' expecting Identifier
line 4992:30 extraneous input '(' expecting Identifier
line 4992:54 no viable alternative at input ')'
line 4993:36 missing RPAREN at ';'
line 4996:18 mismatched input ':' expecting RPAREN
line 4996:28 extraneous input '(' expecting Identifier

Can't execute test due to unknown reason.
Exception in thread "main" 
Can't execute test due to unknown reason.

Can't execute test due to unknown reason.

Can't execute test due to unknown reason.

at com.google.jstestdriver.coverage.CodeInstrumentor.instrument(CodeInstrumentor.java:74)
at com.google.jstestdriver.coverage.CoverageInstrumentingProcessor.process(CoverageInstrumentingProcessor.java:62)
at com.google.jstestdriver.ProcessingFileLoader.postProcessFile(ProcessingFileLoader.java:78)
at com.google.jstestdriver.ProcessingFileLoader.loadFiles(ProcessingFileLoader.java:67)
at com.google.jstestdriver.model.JstdTestCaseDelta.loadFiles(JstdTestCaseDelta.java:59)
at com.google.jstestdriver.FileUploader.uploadToServer(FileUploader.java:218)
at com.google.jstestdriver.action.UploadAction.run(UploadAction.java:39)
at com.google.jstestdriver.ActionRunner.runActions(ActionRunner.java:81)
at com.google.jstestdriver.embedded.JsTestDriverImpl.runConfigurationWithFlags(JsTestDriverImpl.java:342)
at com.google.jstestdriver.embedded.JsTestDriverImpl.runConfiguration(JsTestDriverImpl.java:233)
at com.google.jstestdriver.idea.rt.TestRunner.runTests(TestRunner.java:195)
at com.google.jstestdriver.idea.rt.TestRunner.executeTests(TestRunner.java:96)
at com.google.jstestdriver.idea.rt.TestRunner.executeAll(TestRunner.java:83)
at com.google.jstestdriver.idea.rt.TestRunner.main(TestRunner.java:276)
Caused by: java.lang.IllegalArgumentException: replace: range invalid: 43095..43093(size=371669)
at org.antlr.runtime.TokenRewriteStream.replace(TokenRewriteStream.java:263)
at org.antlr.runtime.TokenRewriteStream.replace(TokenRewriteStream.java:250)
at com.google.jstestdriver.coverage.es3.ES3InstrumentParser.statement(ES3InstrumentParser.java:4107)
at com.google.jstestdriver.coverage.es3.ES3InstrumentParser.sourceElement(ES3InstrumentParser.java:6693)
at com.google.jstestdriver.coverage.es3.ES3InstrumentParser.functionBody(ES3InstrumentParser.java:6531)
at com.google.jstestdriver.coverage.es3.ES3InstrumentParser.functionDeclaration(ES3InstrumentParser.java:6318)
at com.google.jstestdriver.coverage.es3.ES3InstrumentParser.sourceElement(ES3InstrumentParser.java:6682)
at com.google.jstestdriver.coverage.es3.ES3InstrumentParser.program(ES3InstrumentParser.java:6612)
at com.google.jstestdriver.coverage.CodeInstrumentor.instrument(CodeInstrumentor.java:72)
... 13 more

Process finished with exit code 1

并且没有生成代码覆盖率

我正在使用 jsTestDriver 在 PHPStorm 上运行它。

actionsTestAllChartsFunctions.test.js 只需要我创建的文件。 testDates.test.js 和 testButtons.test.js 都需要 jQuery 和 sinon。 testButtons 也需要 D3。

实际测试和我的代码似乎不是问题。正如我所说,测试完全通过。过去使用“let”时出现错误,但切换到所有“var”虽然不理想,但确实解决了错误。

load:
  - allCharts.js
  - tests/dependencies/sinon732.js
test:
  - tests/Functional/javascriptTests/actionsTestAllChartsFunctions.test.js

timeout: 60 

足以破坏代码覆盖率。

我希望获得所有文件的代码覆盖率(最初我的 testDriver 应该一起运行所有测试,直到我将其拆分以根除问题),但任何需要 sinon 的东西都无法再获得代码覆盖率。除非有其他可以存根、模拟和监视函数的库,否则不使用 sinon 是不可能的。

【问题讨论】:

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


    【解决方案1】:

    在 sinon 文件中(适用于我测试过的每个版本)去 var TYPE_MAP = {

    在那里:

    • 改变功能:到'功能':
    • 将数字:更改为“数字”:
    • 将对象:更改为“对象”:
    • 将正则表达式:更改为“正则表达式”:
    • 将字符串:更改为“字符串”:

    这解决了这个问题。

    【讨论】:

    • 就个人而言,它修复了 testDates,但导致 testButtons 中出现不同的错误,即使不是更奇怪,也同样奇怪。它现在不再在不运行代码覆盖时可以在 testButtons 中找到函数。虽然与这个问题无关,所以你不太可能得到这个问题。可能与 d3 导入有关
    • stackoverflow.com/questions/57379043/… 澄清我之前的评论
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2014-01-08
    • 2018-05-21
    相关资源
    最近更新 更多