angular-cn/aio/dist/generated/docs/guide/testing-code-coverage.json

5 lines
3.9 KiB
JSON

{
"id": "guide/testing-code-coverage",
"title": "Find out how much code you're testing",
"contents": "\n\n\n<div class=\"github-links\">\n <a href=\"https://github.com/angular/angular/edit/master/aio/content/guide/testing-code-coverage.md?message=docs%3A%20describe%20your%20change...\" aria-label=\"Suggest Edits\" title=\"Suggest Edits\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">mode_edit</i></a>\n</div>\n\n\n<div class=\"content\">\n <a id=\"code-coverage\"></a>\n<h1 id=\"find-out-how-much-code-youre-testing\">Find out how much code you're testing<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/testing-code-coverage#find-out-how-much-code-youre-testing\"><i class=\"material-icons\">link</i></a></h1>\n<p>The CLI can run unit tests and create code coverage reports.\nCode coverage reports show you any parts of your code base that may not be properly tested by your unit tests.</p>\n<div class=\"alert is-helpful\">\n<p> For the sample app that the testing guides describe, see the <live-example name=\"testing\" embedded-style=\"\" nodownload=\"\">sample app</live-example>.</p>\n<p> For the tests features in the testing guides, see <live-example name=\"testing\" stackblitz=\"specs\" nodownload=\"\">tests</live-example>.</p>\n</div>\n<p>To generate a coverage report run the following command in the root of your project.</p>\n<code-example language=\"sh\" class=\"code-shell\">\n ng test --no-watch --code-coverage\n</code-example>\n<p>When the tests are complete, the command creates a new <code>/coverage</code> folder in the project. Open the <code>index.html</code> file to see a report with your source code and code coverage values.</p>\n<p>If you want to create code-coverage reports every time you test, you can set the following option in the CLI configuration file, <code>angular.json</code>:</p>\n<code-example>\n \"test\": {\n \"options\": {\n \"codeCoverage\": true\n }\n }\n</code-example>\n<h2 id=\"code-coverage-enforcement\">Code coverage enforcement<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/testing-code-coverage#code-coverage-enforcement\"><i class=\"material-icons\">link</i></a></h2>\n<p>The code coverage percentages let you estimate how much of your code is tested.\nIf your team decides on a set minimum amount to be unit tested, you can enforce this minimum with the Angular CLI.</p>\n<p>For example, suppose you want the code base to have a minimum of 80% code coverage.\nTo enable this, open the <a href=\"https://karma-runner.github.io\">Karma</a> test platform configuration file, <code>karma.conf.js</code>, and add the <code>check</code> property in the <code>coverageReporter:</code> key.</p>\n<code-example language=\"js\">\ncoverageReporter: {\n dir: require('path').join(__dirname, './coverage/&#x3C;project-name>'),\n subdir: '.',\n reporters: [\n { type: 'html' },\n { type: 'text-summary' }\n ],\n check: {\n <a href=\"api/core/global\" class=\"code-anchor\">global</a>: {\n statements: 80,\n branches: 80,\n functions: 80,\n lines: 80\n }\n }\n}\n</code-example>\n<p>The <code>check</code> property causes the tool to enforce a minimum of 80% code coverage when the unit tests are run in the project.</p>\n<p>You can find more info about the different coverage configuration options <a href=\"https://github.com/karma-runner/karma-coverage/blob/master/docs/configuration.md\">here</a>.</p>\n\n \n</div>\n\n<!-- links to this doc:\n - guide/testing\n-->\n<!-- links from this doc:\n - api/core/global\n - guide/testing-code-coverage#code-coverage-enforcement\n - guide/testing-code-coverage#find-out-how-much-code-youre-testing\n - https://github.com/angular/angular/edit/master/aio/content/guide/testing-code-coverage.md?message=docs%3A%20describe%20your%20change...\n - https://github.com/karma-runner/karma-coverage/blob/master/docs/configuration.md\n - https://karma-runner.github.io\n-->"
}