版博士V2.0程序
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # c8 - native V8 code-coverage
  2. [![ci](https://github.com/bcoe/c8/actions/workflows/ci.yaml/badge.svg)](https://github.com/bcoe/c8/actions/workflows/ci.yaml)
  3. ![nycrc config on GitHub](https://img.shields.io/nycrc/bcoe/c8)
  4. [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://www.conventionalcommits.org/)
  5. Code-coverage using [Node.js' built in functionality](https://nodejs.org/dist/latest-v10.x/docs/api/cli.html#cli_node_v8_coverage_dir)
  6. that's compatible with [Istanbul's reporters](https://istanbul.js.org/docs/advanced/alternative-reporters/).
  7. Like [nyc](https://github.com/istanbuljs/nyc), c8 just magically works:
  8. ```sh
  9. npm i c8 -g
  10. c8 node foo.js
  11. ```
  12. The above example will output coverage metrics for `foo.js`.
  13. ## CLI Options / Configuration
  14. c8 can be configured via command-line flags, a `c8` section in `package.json`, or a JSON configuration file on disk.
  15. A configuration file can be specified by passing its path on the command line with `--config` or `-c`. If no config option is provided, c8 searches for files named `.c8rc`, `.c8rc.json`, `.nycrc`, or `.nycrc.json`, starting from
  16. `cwd` and walking up the filesystem tree.
  17. When using `package.json` configuration or a dedicated configuration file, omit the `--` prefix from the long-form of the desired command-line option.
  18. Here is a list of common options. Run `c8 --help` for the full list and documentation.
  19. | Option | Description | Type | Default |
  20. | ------ | ----------- | ---- | ------- |
  21. | `-c`, `--config` | path to JSON configuration file | `string` | See above |
  22. | `-r`, `--reporter` | coverage reporter(s) to use | `Array<string>` | `['text']` |
  23. | `-o`, `--reports-dir`, `--report-dir` | directory where coverage reports will be output to | `string` | `./coverage` |
  24. | `--all` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `boolean` | `false` |
  25. | `--src` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `Array<string>` | `[process.cwd()]`|
  26. | `-n`, `--include` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `Array<string>` | `[]` (include all files) |
  27. | `-x`, `--exclude` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `Array<string>` | [list](https://github.com/istanbuljs/schema/blob/master/default-exclude.js) |
  28. | `--exclude-after-remap` | see [section below](#exclude-after-remap) for more info | `boolean` | `false` |
  29. | `-e`, `--extension` | only files matching these extensions will show coverage | `string \| Array<string>` | [list](https://github.com/istanbuljs/schema/blob/master/default-extension.js) |
  30. | `--skip-full` | do not show files with 100% statement, branch, and function coverage | `boolean` | `false` |
  31. | `--check-coverage` | check whether coverage is within thresholds provided | `boolean` | `false` |
  32. | `--per-file` | check thresholds per file | `boolean` | `false` |
  33. | `--temp-directory` | directory V8 coverage data is written to and read from | `string` | `process.env.NODE_V8_COVERAGE` |
  34. | `--clean` | should temp files be deleted before script execution | `boolean` | `true` |
  35. ## Checking for "full" source coverage using `--all`
  36. By default v8 will only give us coverage for files that were loaded by the engine. If there are source files in your
  37. project that are flexed in production but not in your tests, your coverage numbers will not reflect this. For example,
  38. if your project's `main.js` loads `a.js` and `b.js` but your unit tests only load `a.js` your total coverage
  39. could show as `100%` for `a.js` when in fact both `main.js` and `b.js` are uncovered.
  40. By supplying `--all` to c8, all files in directories specified with `--src` (defaults to `cwd`) that pass the `--include`
  41. and `--exclude` flag checks, will be loaded into the report. If any of those files remain uncovered they will be factored
  42. into the report with a default of 0% coverage.
  43. ## SourceMap Support
  44. `c8` can handle source-maps, for remapping coverage from generated code to original source files (_useful for TypeScript, JSX, etc_).
  45. ### Source map files versus inline source maps
  46. Just-in-time instrumented codebases will often insert source maps inline with the `.js` code they generate at runtime (e.g, `@babel/register` can be configured to insert a source map footer).
  47. Pre-instrumented codebases, e.g., running `tsc` to generate `.js` in a build folder, may generate either inline source maps, or a separate `.map` file stored on disk.
  48. `c8` can handle loading both types of source maps.
  49. ### Exclude after remap
  50. Depending on the size and configuration of your project, it may be preferable to apply exclusion logic either before or after source-maps are used to remap compiled to original source files.
  51. `--exclude-after-remap` is used to control this behaviour.
  52. ## c8 report
  53. run `c8 report` to regenerate reports after `c8` has already been run.
  54. ## Checking coverage
  55. c8 can fail tests if coverage falls below a threshold.
  56. After running your tests with c8, simply run:
  57. ```sh
  58. c8 check-coverage --lines 95 --functions 95 --branches 95
  59. ```
  60. c8 also accepts a `--check-coverage` shorthand, which can be used to
  61. both run tests and check that coverage falls within the threshold provided:
  62. ```sh
  63. c8 --check-coverage --lines 100 npm test
  64. ```
  65. The above check fails if coverage falls below 100%.
  66. To check thresholds on a per-file basis run:
  67. ```sh
  68. c8 check-coverage --lines 95 --per-file
  69. ```
  70. If you want to check for 100% coverage across all dimensions, use `--100`:
  71. ```sh
  72. c8 --100 npm test
  73. ```
  74. Is equivalent to
  75. ```sh
  76. c8 --check-coverage --lines 100 --functions 100 --branches 100 --statements 100 npm test
  77. ```
  78. The `--100` flag can be set for the `check-coverage` as well:
  79. ```sh
  80. c8 check-coverage --100
  81. ```
  82. ## Ignoring Uncovered Lines, Functions, and Blocks
  83. Sometimes you might find yourself wanting to ignore uncovered portions of your
  84. codebase. For example, perhaps you run your tests on Linux, but
  85. there's some logic that only executes on Windows.
  86. To ignore lines, blocks, and functions, use the special comment:
  87. `/* c8 ignore next */`.
  88. ### Ignoring the next line
  89. ```js
  90. const myVariable = 99
  91. /* c8 ignore next */
  92. if (process.platform === 'win32') console.info('hello world')
  93. ```
  94. ### Ignoring the next N lines
  95. ```js
  96. const myVariable = 99
  97. /* c8 ignore next 3 */
  98. if (process.platform === 'win32') {
  99. console.info('hello world')
  100. }
  101. ```
  102. ### Ignoring all lines until told
  103. ```js
  104. /* c8 ignore start */
  105. function dontMindMe() {
  106. // ...
  107. }
  108. /* c8 ignore stop */
  109. ```
  110. ### Ignoring a block on the current line
  111. ```js
  112. const myVariable = 99
  113. const os = process.platform === 'darwin' ? 'OSXy' /* c8 ignore next */ : 'Windowsy'
  114. ```
  115. ## Supported Node.js Versions
  116. c8 uses [native V8 coverage](https://github.com/nodejs/node/pull/22527),
  117. make sure you're running Node.js `>= 10.12.0`.
  118. ## Contributing to `c8`
  119. See the [contributing guide here](./CONTRIBUTING.md).