即使没有代码覆盖率的测试通过,如何阻止代码覆盖率因 sinon 而崩溃?
How to stop Code Coverage from crashing because of sinon even though tests without code coverage pass?
问题:当 sinon 是依赖项时,代码覆盖率不起作用。
我目前有 3 个测试文件。 运行 所有测试导致 17 项测试通过,0 项失败。其中 2 个文件需要额外的依赖项(让我们称这些文件为 testDates.test.js 和 testButtons.test.js),而第 3 个文件则不需要。其中包括 JQuery、Sinon (v7.3.2) 和 D3。
运行 第三个文件的测试有 9 个测试通过,0 个测试失败。 运行 代码覆盖率 returns 78% 的代码覆盖率(这很好,因为未测试的函数只是返回硬编码数据作为占位符的函数,用于将来进行的实际数据检索)。
returns这段代码覆盖率的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
并且没有生成代码覆盖率
我 运行 在 PHPStorm 上使用 jsTestDriver。
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 是不可能的,除非有一些其他的库可以存根、模拟和监视函数。
在 sinon 文件中(适用于我测试过的每个版本)转到 var TYPE_MAP = {
去过那里:
- 更改函数:更改为 'function':
- 更改号码:至'number':
- 更改对象:为'object':
- 将正则表达式:更改为 'regexp':
- 将字符串:更改为 'string':
这解决了这个问题。
问题:当 sinon 是依赖项时,代码覆盖率不起作用。
我目前有 3 个测试文件。 运行 所有测试导致 17 项测试通过,0 项失败。其中 2 个文件需要额外的依赖项(让我们称这些文件为 testDates.test.js 和 testButtons.test.js),而第 3 个文件则不需要。其中包括 JQuery、Sinon (v7.3.2) 和 D3。
运行 第三个文件的测试有 9 个测试通过,0 个测试失败。 运行 代码覆盖率 returns 78% 的代码覆盖率(这很好,因为未测试的函数只是返回硬编码数据作为占位符的函数,用于将来进行的实际数据检索)。
returns这段代码覆盖率的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
并且没有生成代码覆盖率
我 运行 在 PHPStorm 上使用 jsTestDriver。
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 是不可能的,除非有一些其他的库可以存根、模拟和监视函数。
在 sinon 文件中(适用于我测试过的每个版本)转到 var TYPE_MAP = {
去过那里:
- 更改函数:更改为 'function':
- 更改号码:至'number':
- 更改对象:为'object':
- 将正则表达式:更改为 'regexp':
- 将字符串:更改为 'string':
这解决了这个问题。