### Quick Start Example Source: https://istanbul.js.org/docs/tutorials/mocha A simplified example of adding 'nyc' to the test script for basic coverage. ```bash npm install --save-dev nyc ``` ```json { "scripts": { "test": "nyc mocha" } } ``` -------------------------------- ### Quick Start - Basic Integration Source: https://istanbul.js.org/docs/tutorials/ava A simplified example of adding coverage to AVA tests by prepending 'nyc' to the test command. ```json { "scripts": { "test": "nyc ava" } } ``` -------------------------------- ### Install Dependencies Source: https://istanbul.js.org/docs/tutorials/es2015 This command installs the necessary development dependencies for setting up test coverage with ES2015+ and Babel. ```bash npm i @babel/cli @babel/register babel-plugin-istanbul @babel/preset-env cross-env mocha chai nyc --save-dev ``` -------------------------------- ### Writing Tests with ES2015+ Source: https://istanbul.js.org/docs/tutorials/es2015 An example of writing tests using ES2015+ syntax, leveraging @babel/register for automatic compilation. ```javascript import CoverageBabel from './index' require('chai').should() describe('CoverageBabel', function () { it('returns hello world message', function () { const cls = new CoverageBabel('Ben') cls.helloMessage().should.equal('hello Ben') }) }) ``` -------------------------------- ### Add Travis CI statements for Codecov Source: https://istanbul.js.org/docs/tutorials/intern Add statements to `.travis.yml` to install codecov and upload results. ```yaml # .travis.yml install: - npm install - npm install -g codecov script: - npm run ci && codecov ``` -------------------------------- ### Using Alternative Reporters Source: https://istanbul.js.org/docs/tutorials/ava Specify alternative or multiple reporters using the --reporter flag. This example outputs both text and HTML reports. ```json { "scripts": { "test": "nyc --reporter=html --reporter=text ava" } } ``` -------------------------------- ### Integrating with Coveralls Source: https://istanbul.js.org/docs/tutorials/ava Steps to integrate Istanbul with Coveralls and Travis CI, including installing the coveralls dependency and updating package.json scripts. ```bash npm install coveralls --save-dev ``` ```json { "script": { "test": "nyc --reporter=html --reporter=text ava", "coverage": "nyc report --reporter=text-lcov | coveralls" } } ``` -------------------------------- ### Add coverage to mocha tests Source: https://istanbul.js.org/ Install nyc as a dev dependency and prepend the 'nyc' command to your existing test command in package.json. ```bash $ npm install --save-dev nyc ``` ```json { "scripts": { "test": "nyc mocha" } } ``` -------------------------------- ### Coverage summary output Source: https://istanbul.js.org/docs/tutorials/intern Intern's Node-based reporters output a coverage summary to the console if any coverage data was collected. This is an example of the output. ```text Total coverage -------------------------|----------|----------|----------|----------|----------------| File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines | -------------------------|----------|----------|----------|----------|----------------| All files | 91.22 | 80.8 | 88.95 | 91.17 | | src | 100 | 100 | 100 | 100 | | index.ts | 100 | 100 | 100 | 100 | | src/bin | 93.55 | 68.75 | 100 | 92.86 | | intern.ts | 93.55 | 68.75 | 100 | 92.86 | 24,39,76,92 | src/lib | 90.87 | 82.77 | 86.74 | 91.08 | | BenchmarkSuite.ts | 100 | 100 | 100 | 100 | | BenchmarkTest.ts | 91.3 | 74.29 | 81.82 | 91.11 |... 214,254,255 | Channel.ts | 100 | 75 | 100 | 100 | 25 | -------------------------|----------|----------|----------|----------|----------------| ``` -------------------------------- ### LCOV Reporter Output Source: https://istanbul.js.org/docs/advanced/alternative-reporters Example of the LCOV output, a standard format for code coverage data. ```lcov TN: SF:$PROJECT_PATH/src/bar/index.ts FN:7,baz FNF:1 FNH:1 FNDA:2,baz DA:1,1 DA:2,1 DA:3,1 DA:5,1 DA:8,2 DA:9,1 DA:13,1 DA:16,1 DA:20,1 DA:21,1 LF:10 LH:10 BRDA:8,0,0,1 BRDA:8,0,1,1 BRF:2 BRH:2 end_of_record ``` -------------------------------- ### TeamCity Reporter Output Source: https://istanbul.js.org/docs/advanced/alternative-reporters Example of the TeamCity reporter output, which provides build statistics for code coverage. ```teamcity ##teamcity[blockOpened name='Code Coverage Summary'] ##teamcity[buildStatisticValue key='CodeCoverageAbsBCovered' value='166'] ##teamcity[buildStatisticValue key='CodeCoverageAbsBTotal' value='166'] ##teamcity[buildStatisticValue key='CodeCoverageAbsRCovered' value='18'] ##teamcity[buildStatisticValue key='CodeCoverageAbsRTotal' value='24'] ##teamcity[buildStatisticValue key='CodeCoverageAbsMCovered' value='49'] ##teamcity[buildStatisticValue key='CodeCoverageAbsMTotal' value='49'] ##teamcity[buildStatisticValue key='CodeCoverageAbsLCovered' value='161'] ##teamcity[buildStatisticValue key='CodeCoverageAbsLTotal' value='161'] ##teamcity[blockClosed name='Code Coverage Summary'] ``` -------------------------------- ### Text Reporter Output Source: https://istanbul.js.org/docs/advanced/alternative-reporters Example of the detailed plain text reporter output, showing coverage breakdown by file. ```text ------------------|----------|----------|----------|----------|----------------| File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines | ------------------|----------|----------|----------|----------|----------------| All files | 100 | 75 | 100 | 100 | | src | 100 | 100 | 100 | 100 | | errors.ts | 100 | 100 | 100 | 100 | | test.ts | 100 | 100 | 100 | 100 | | src/config | 100 | 50 | 100 | 100 | | index.ts | 100 | 50 | 100 | 100 | 2,5,6,7 | src/crud | 100 | 100 | 100 | 100 | | index.test.ts | 100 | 100 | 100 | 100 | | src/crud/insert | 100 | 87.5 | 100 | 100 | | index.test.ts | 100 | 50 | 100 | 100 | 25 | index.ts | 100 | 100 | 100 | 100 | | ------------------|----------|----------|----------|----------|----------------| ``` -------------------------------- ### Text Summary Reporter Output Source: https://istanbul.js.org/docs/advanced/alternative-reporters Example of the plain text summary output, providing an overview of coverage percentages. ```text =============================== Coverage summary =============================== Statements : 100% ( 166/166 ) Branches : 75% ( 18/24 ) Functions : 100% ( 49/49 ) Lines : 100% ( 161/161 ) ================================================================================ ``` -------------------------------- ### JSON Reporter Output Source: https://istanbul.js.org/docs/advanced/alternative-reporters Example of the JSON output generated by the JSON reporter, showing coverage details for specific files. ```json {"$PROJECT_PATH/src/errors.ts": {"path":"$PROJECT_PATH/src/errors.ts","statementMap":{"0":{"start":{"line":1,"column":32},"end":{"line":1,"column":72}},"1":{"start":{"line":4,"column":2},"end":{"line":4,"column":25}}},"fnMap":{},"branchMap":{},"s":{"0":1,"1":1},"f":{},"b":{}} ," $PROJECT_PATH/src/init/env.ts": {"path":"$PROJECT_PATH/src/init/env.ts","statementMap":{"0":{"start":{"line":1,"column":0},"end":{"line":1,"column":32}},"1":{"start":{"line":2,"column":0},"end":{"line":2,"column":16}}},"fnMap":{},"branchMap":{},"s":{"0":1,"1":1},"f":{},"b":{}} } ``` -------------------------------- ### Configure coverage on the command line Source: https://istanbul.js.org/docs/tutorials/intern Coverage can also be specified on the command line. ```bash $ npx intern coverage='_build/src/**/*.js' ``` -------------------------------- ### Create coverage report with nyc Source: https://istanbul.js.org/docs/tutorials/iotjs Use nyc to create a coverage report, specifying the reporter and the temporary directory containing coverage data. ```bash node_modules/.bin/nyc report --reporter=lcov --temp-directory=/path/to/coverage/data ``` -------------------------------- ### Specify reporters on the command line Source: https://istanbul.js.org/docs/tutorials/intern Reporters can also be specified on the command line. ```bash $npx intern reporters=runner reporters=jsoncoverage ``` -------------------------------- ### Instrument source code with nyc Source: https://istanbul.js.org/docs/tutorials/iotjs Use nyc to transpile source code and provide line counters for instrumentation. ```bash node_modules/.bin/nyc instrument path/to/source /path/to/output ``` -------------------------------- ### Scripts Configuration (package.json) Source: https://istanbul.js.org/docs/tutorials/es2015 This package.json script stanza defines commands for building the project, running tests with nyc and mocha, and a pre-publish script to build before publishing. ```json { "scripts": { "build": "babel index.js -d src", "test": "cross-env NODE_ENV=test nyc mocha test.js", "prepublish": "npm run build" } } ``` -------------------------------- ### Add npm script for CI tests Source: https://istanbul.js.org/docs/tutorials/intern Add an npm script to your project's `package.json` to run CI tests. ```json { "scripts": { "ci": "tsc && npm run test", "test: " "intern" } } ``` -------------------------------- ### Configure reporter options in Intern config Source: https://istanbul.js.org/docs/tutorials/intern Reporter options such as the output directory or watermarks can be specified by using a properties object. ```json { "reporters": [ "runner", { "name": "jsoncoverage", "filename": "coverage.json" } ] } ``` -------------------------------- ### Basic Istanbul Integration Source: https://istanbul.js.org/docs/tutorials/ava To enable Istanbul coverage, prepend the 'nyc' command to your existing test script in package.json. ```json { "scripts": { "test": "nyc ava --timeout=3000" } } ``` -------------------------------- ### Show Process Tree Source: https://istanbul.js.org/docs/advanced/process-tree Command to run a test script with nyc and display the process tree, along with the coverage report. ```bash $ nyc --show-process-tree npm test 3 passed ----------|----------|----------|----------|----------|----------------| File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines | ----------|----------|----------|----------|----------|----------------| All files | 100 | 100 | 100 | 100 | | index.js | 100 | 100 | 100 | 100 | | ----------|----------|----------|----------|----------|----------------| nyc └─┬ /usr/local/bin/node /usr/local/bin/npm test └─┬ /usr/local/bin/node /path/to/your/project/node_modules/.bin/ava └── /usr/local/bin/node /path/to/your/project/node_modules/ava/lib/test-worker.js … ``` -------------------------------- ### Enable Coverage Source: https://istanbul.js.org/docs/tutorials/tap This configuration enables code coverage tracking for tests run with `tap`. ```json { "scripts": { "test": "tap test/*.js --coverage" } } ``` -------------------------------- ### package.json configuration Source: https://istanbul.js.org/docs/tutorials/typescript Minimum recommended configuration for package.json to enable Istanbul coverage for TypeScript with mocha. ```json { "scripts": { "test": "nyc mocha" }, "nyc": { "extension": [ ".ts", ".tsx" ], "exclude": [ "**/*.d.ts" ], "reporter": [ "html" ], "all": true } } ``` -------------------------------- ### Publishable Files Configuration (package.json) Source: https://istanbul.js.org/docs/tutorials/es2015 This package.json configuration specifies which files should be published to npm, ensuring only the compiled ES5 code is included. ```json { "files": [ "src/index.js" ] } ``` -------------------------------- ### Using Alternative Reporters Source: https://istanbul.js.org/docs/tutorials/tap This configuration specifies the `html` reporter for coverage reports, which will be output to `./coverage/index.html` and opened in the web browser by default. ```json { "scripts": { "test": "tap --coverage --coverage-report=html" } } ``` -------------------------------- ### test/mocha.opts configuration Source: https://istanbul.js.org/docs/tutorials/typescript Configuration for test/mocha.opts to enable TypeScript compilation and source map support for mocha tests. ```bash --require ts-node/register # replace with ts-node/register/transpile-only if you have custom types --require source-map-support/register --recursive ``` -------------------------------- ### Instrumenting source files Source: https://istanbul.js.org/docs/advanced/instrument This command instruments source files located in the './lib' directory and outputs the instrumented files to the './output' directory. ```bash nyc instrument ./lib ./output ``` -------------------------------- ### Babel Configuration (.babelrc) Source: https://istanbul.js.org/docs/tutorials/es2015 This .babelrc file configures Babel to use the @babel/preset-env and enables the istanbul plugin for the test environment. ```json { "presets": [ "@babel/preset-env" ], "env": { "test": { "plugins": [ "istanbul" ] } } } ``` -------------------------------- ### Alternative Reporters Source: https://istanbul.js.org/docs/tutorials/mocha This snippet demonstrates how to use multiple reporters, including HTML and text, by specifying the --reporter flag. ```json { "scripts": { "test": "nyc --reporter=html --reporter=text mocha" } } ``` -------------------------------- ### Configure coverage in Intern config file Source: https://istanbul.js.org/docs/tutorials/intern Specify files to be covered using a glob string or an array of glob strings in the Intern config file. ```json { "coverage": "_build/src/**/*.js" } ``` -------------------------------- ### Configure Intern for Codecov.io Source: https://istanbul.js.org/docs/tutorials/intern Configure Intern to gather coverage and output it with a reporter that generates codecov-compatible output, such as lcov or jsoncoverage. ```json { "coverage": "_build/src/**/*.js", "reporters": [ { "name": "jsoncoverage", "filename": "coverage.json" } ] } ``` -------------------------------- ### Coveralls Integration Source: https://istanbul.js.org/docs/tutorials/mocha This snippet shows how to add the coveralls dependency and update package.json scripts for integration with coveralls.io. ```bash npm install coveralls --save-dev ``` ```json { "script": { "test": "nyc --reporter=html --reporter=text mocha", "coverage": "nyc report --reporter=text-lcov | coveralls" } } ``` -------------------------------- ### NYC Configuration (package.json) Source: https://istanbul.js.org/docs/tutorials/es2015 This package.json configuration sets up nyc to require @babel/register for ES2015+ support, disables nyc's source map and instrumentation, and specifies reporters. ```json { "nyc": { "require": [ "@babel/register" ], "reporter": [ "lcov", "text" ], "sourceMap": false, "instrument": false } } ``` -------------------------------- ### Output coverage data Source: https://istanbul.js.org/docs/tutorials/iotjs Modify test files to output gathered coverage data to a file upon process exit. ```javascript process.on('exit', function () { require('fs').writeFileSync('coverage.data', Buffer(JSON.stringify(__coverage__))) }) ``` -------------------------------- ### Generate report from coverage object Source: https://istanbul.js.org/docs/advanced/coverage-object-report Output the contents of window.__coverage__ (assuming it's in Istanbul 1.0 format) to .nyc_output/out.json. ```bash nyc report --reporter=html ``` -------------------------------- ### Enforce 100% Coverage Source: https://istanbul.js.org/docs/tutorials/tap This configuration enforces 100% coverage of lines, branches, functions, and statements. ```json { "scripts": { "test": "tap test/*.js --100" } } ``` -------------------------------- ### Basic Mocha Test Script Source: https://istanbul.js.org/docs/tutorials/mocha This snippet shows how to add 'nyc' to your package.json test script for basic coverage. ```json { "scripts": { "test": "nyc mocha --timeout=3000" } } ``` -------------------------------- ### Pass Istanbul options via Intern config Source: https://istanbul.js.org/docs/tutorials/intern If necessary, Istanbul options can be passed directly to the instrumenter by providing an `instrumenterOptions` property in the Intern config file. ```json { "instrumenterOptions": { "esModules": true } } ``` -------------------------------- ### Specify reporters in Intern config Source: https://istanbul.js.org/docs/tutorials/intern Intern provides access to various Istanbul reporters. Reporters can be specified using the `reporters` config property. ```json { "reporters": [ "runner", "jsoncoverage" ] } ``` -------------------------------- ### Environment Information Script Source: https://istanbul.js.org/contributing A script to capture environment information including Node.js version, npm version, and project dependencies. ```sh sh -c 'node --version; npm --version; npm ls' > output.txt ``` -------------------------------- ### Shipping Code with Babel CLI Source: https://istanbul.js.org/docs/tutorials/es2015 This command uses Babel CLI to compile ES2015+ JavaScript into ES5 compatible code for publishing. ```bash babel index.js -d src ``` -------------------------------- ### JSON Coverage Summary Source: https://istanbul.js.org/docs/advanced/alternative-reporters The coverage summary is generated at `coverage/coverage-summary.json` and provides a detailed breakdown of code coverage metrics. ```json { "total": {"lines":{"total":161,"covered":161,"skipped":0,"pct":100},"statements":{"total":166,"covered":166,"skipped":0,"pct":100},"functions":{"total":49,"covered":49,"skipped":0,"pct":100},"branches":{"total":24,"covered":18,"skipped":0,"pct":75}} ,"$PROJECT_PATH/src/errors.ts": {"lines":{"total":2,"covered":2,"skipped":0,"pct":100},"functions":{"total":0,"covered":0,"skipped":0,"pct":100},"statements":{"total":2,"covered":2,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}} ,"$PROJECT_PATH/src/test.ts": {"lines":{"total":9,"covered":9,"skipped":0,"pct":100},"functions":{"total":4,"covered":4,"skipped":0,"pct":100},"statements":{"total":9,"covered":9,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}} ,"$PROJECT_PATH/src/config/index.ts": {"lines":{"total":10,"covered":10,"skipped":0,"pct":100},"functions":{"total":0,"covered":0,"skipped":0,"pct":100},"statements":{"total":10,"covered":10,"skipped":0,"pct":100},"branches":{"total":8,"covered":4,"skipped":0,"pct":50}} ,"$PROJECT_PATH/src/crud/index.test.ts": {"lines":{"total":1,"covered":1,"skipped":0,"pct":100},"functions":{"total":0,"covered":0,"skipped":0,"pct":100},"statements":{"total":1,"covered":1,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}} ,"$PROJECT_PATH/src/crud/insert/index.test.ts": {"lines":{"total":36,"covered":36,"skipped":0,"pct":100},"functions":{"total":16,"covered":16,"skipped":0,"pct":100},"statements":{"total":37,"covered":37,"skipped":0,"pct":100},"branches":{"total":2,"covered":1,"skipped":0,"pct":50}} ,"$PROJECT_PATH/src/crud/insert/index.ts": {"lines":{"total":5,"covered":5,"skipped":0,"pct":100},"functions":{"total":2,"covered":2,"skipped":0,"pct":100},"statements":{"total":6,"covered":6,"skipped":0,"pct":100},"branches":{"total":6,"covered":6,"skipped":0,"pct":100}} ,"$PROJECT_PATH/src/customers/index.test.ts": {"lines":{"total":14,"covered":14,"skipped":0,"pct":100},"functions":{"total":4,"covered":4,"skipped":0,"pct":100},"statements":{"total":14,"covered":14,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}} ,"$PROJECT_PATH/src/customers/index.ts": {"lines":{"total":5,"covered":5,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":6,"covered":6,"skipped":0,"pct":100},"branches":{"total":2,"covered":2,"skipped":0,"pct":100}} ,"$PROJECT_PATH/src/init/env.ts": {"lines":{"total":2,"covered":2,"skipped":0,"pct":100},"functions":{"total":0,"covered":0,"skipped":0,"pct":100},"statements":{"total":2,"covered":2,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}} ,"$PROJECT_PATH/src/init/index.test.ts": {"lines":{"total":4,"covered":4,"skipped":0,"pct":100},"functions":{"total":2,"covered":2,"skipped":0,"pct":100},"statements":{"total":4,"covered":4,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}} ,"$PROJECT_PATH/src/init/execute/index.test.ts": {"lines":{"total":26,"covered":26,"skipped":0,"pct":100},"functions":{"total":7,"covered":7,"skipped":0,"pct":100},"statements":{"total":26,"covered":26,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}} ,"$PROJECT_PATH/src/init/execute/index.ts": {"lines":{"total":8,"covered":8,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":9,"covered":9,"skipped":0,"pct":100},"branches":{"total":2,"covered":2,"skipped":0,"pct":100}} ,"$PROJECT_PATH/src/init/gateway/index.ts": {"lines":{"total":2,"covered":2,"skipped":0,"pct":100},"functions":{"total":0,"covered":0,"skipped":0,"pct":100},"statements":{"total":2,"covered":2,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}} ,"$PROJECT_PATH/src/payments/index.test.ts": {"lines":{"total":27,"covered":27,"skipped":0,"pct":100},"functions":{"total":11,"covered":11,"skipped":0,"pct":100},"statements":{"total":28,"covered":28,"skipped":0,"pct":100},"branches":{"total":2,"covered":1,"skipped":0,"pct":50}} ,"$PROJECT_PATH/src/payments/index.ts": {"lines":{"total":10,"covered":10,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":10,"covered":10,"skipped":0,"pct":100},"branches":{"total":2,"covered":2,"skipped":0,"pct":100}} } ``` -------------------------------- ### cobertura Source: https://istanbul.js.org/docs/advanced/alternative-reporters The following is generated at coverage/cobertura-coverage.xml: ```xml $PROJECT_PATH ``` -------------------------------- ### clover Source: https://istanbul.js.org/docs/advanced/alternative-reporters The following is generated at coverage/clover.xml: ```xml ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.