Istanbul ignore next
Coverage
Vitest supports Native code coverage via and instrumented code coverage via .
Coverage Providers
Both and support are optional. By default, will be used.
You can pick the coverage tool by setting to or :
When you begin the Vitest process, it will prompt you to install the corresponding support package automatically.
Or if you prefer to install them manually:
V8 Provider
INFO
The description of V8 coverage below is Vitest specific and does not put on to other test runners. Since Vitest has used AST based coverage remapping for V8 coverage, which produces identical coverage reports to Istanbul.
This allows users to have the speed of V8 coverage with accuracy of Istanbul coverage.
By default Vitest uses coverage provider. V8 engine, such as NodeJS, Deno or any Chromium based browsers such as Google Chrome.
Coverage collection is performed during runtime by instructing V8 using and Chrome DevTools Protocol in browsers. User's source files can be executed as-is without any pre-instrumentation steps.
- ✅ Recommended option to use
- ✅ No pre-transpile step. Check files can be executed as-is.
- ✅ Faster execute times than Istanbul.
- ✅ Lower memory usag
Sonar doesn't respect istanbul ignore comments
Version: Sonar Scanner 4.6 and hosted sonarcloud.io
Reproduction steps
- create a simple TypeScript project that uses jest, with two source files and 1 test, as illustrated above:
- the test partially covers , and none of
- contains comment at the top of the file
- alternatively, it can contain before the function block
- generate an LCOV coverage report using and the reporter
- upload the report with Sonar Scanner using
Expected
- should report 100% coverage because the function is covered by tests, while the function is ignored through an comment
- should not be included in the coverage report because it is ignored through an comment, or it should report 100%
Actual
Coverage for is reported as expected, but shows 0%, not respecting the comments. Both and comments do not have any effect.
Notes
is the tool used to generate coverage reports for JS/TS. It supports ignoring parts of the code using special comments: istanbul/ignoring-code-for-coverage.md at master · gotwarlost/istanbul · GitHub
1 Like
Coverage
WebdriverIO's browser runner supports code coverage reporting using . The testrunner will automatically instrument your code and capture code coverage for you.
Setup
In order to enable code coverage reporting, enable it through the WebdriverIO browser runner configuration, e.g.:
Checkout all coverage options, to learn how to properly configure it.
Ignoring Code
There may be some sections of your codebase that you wish to purposefully exclude from coverage tracking, to do so you can use the following parsing hints:
- : ignore the next if statement.
- : ignore the else portion of an if statement.
- : ignore the next thing in the source-code ( functions, if statements, classes, you name it).
- : ignore an entire source-file (this should be placed at the top of the file).
info
It is recommended to exclude your test files from the coverage reporting as it could cause errors, e.g. when calling or commands. If you like to keep them in your report, ensure your exclude instrumenting them via:
Istanbul is the tool Jest uses to calculate test coverage. Sometimes we need to exclude some code from the coverage calculations. This is done with special comments which are parsed by Istanbul. There are a few variations of the syntax.
Ignore a Function
/* istanbul ignore next */const f = () => { return 'abc' }This will exclude the entire function from code coverage requirements.
Ignore a Whole File
/* istanbul ignore file */ ... file contents ...Use this as the first line of the file. The entire file will be excluded from code coverage.
Ignore a Method in a Class
class A { f() { console.log("f called") } /* istanbul ignore next */ g() { console.log("g called") } }The comment must be on or above the line defining the method so it is not part of the coverage requirement.
Function Inside an Exported Object
Sometimes we have a module which exports some functions inside an object.
module.exports = { f: () => { console.log('f called') }, g: /* istanbul ignore next */ () => { console.log('g called') }, h: /* istanbul ignore next */ a
The example below shows how to ignore these for coverage. The comment must be right before the function definition.
Writing Tests: Code Coverage
You can run tests with code coverage using the flag:
In the config you can define code coverage thresholds, the test run fails if you drop below this level. You can also configure where and if the detailed test report is written to disk.
Example config:
Ignoring uncovered lines
Web Test Runner uses to covert V8 based code coverage to a form that can be reported by Istanbul. allows for ignoring uncovered lines when calculating code coverage through the use of the following custom comment:
This is somewhat different than other tools where you might have specifically targeted / branches of logic with an ignore statement. Particularly, V8 does not create phantom statements when calculating coverage, so it is likely that you will be able to use less of these statements than in the past.
In this way, you can skip the rest of a line:
You can skip the entire next line:
Or, you could skip multiple ensuing lines:
Coverage browser support
The default coverage of the test runner uses the ability of Chromium to do native code coverage instrumentation. This gives us the best speed. When testing multiple browsers this should still be fine, you don't ne
- create a simple TypeScript project that uses jest, with two source files and 1 test, as illustrated above: