### Combining Shared Configurations in nyc.config.js Source: https://github.com/istanbuljs/nyc/blob/main/README.md An advanced configuration example showing how to merge multiple shared nyc configurations and override specific options. This is useful for creating complex, reusable nyc setups. ```javascript 'use strict'; const babelConfig = require('@istanbuljs/nyc-config-babel'); const hookRunInThisContextConfig = require('@istanbuljs/nyc-config-hook-run-in-this-context'); module.exports = { ...babelConfig, ...hookRunInThisContextConfig, all: true, 'check-coverage': true }; ``` -------------------------------- ### Quick Start with npx Source: https://github.com/istanbuljs/nyc/blob/main/docs/setup-codecov.md Execute this command in your CI runner if your npm test does not run nyc and you have npx available. ```shell npx nyc --reporter=lcov npm test && npx codecov ``` -------------------------------- ### Install Coveralls and NYC Dependencies Source: https://github.com/istanbuljs/nyc/blob/main/docs/setup-coveralls.md Add coveralls and nyc as development dependencies to your project using npm. ```shell npm install coveralls nyc --save-dev ``` -------------------------------- ### Setup NYC for Self-Coverage Source: https://github.com/istanbuljs/nyc/blob/main/docs/profiling.md Link the local development version of NYC globally and generate self-coverage instrumented files. This is the first step in profiling NYC's performance. ```sh cd /user/dev/nyc npm link node ./build-self-coverage ``` -------------------------------- ### Install Codecov and NYC Dependencies Source: https://github.com/istanbuljs/nyc/blob/main/docs/setup-codecov.md Install the codecov and nyc packages as development dependencies for your project. ```shell npm install codecov nyc --save-dev ``` -------------------------------- ### Setup Test Project to Use Local NYC Source: https://github.com/istanbuljs/nyc/blob/main/docs/profiling.md Link the globally linked NYC into the test project's local node_modules. This ensures the test project uses your development version of NYC for profiling. ```sh cd /user/dev/ava npm link nyc ``` -------------------------------- ### Stream Instrumented Source Source: https://github.com/istanbuljs/nyc/blob/main/docs/instrument.md This example demonstrates how to use `nyc instrument` to stream instrumented source code directly to stdout, which can then be piped to another process. This is useful for creating servers that dynamically instrument files on request. ```javascript app.use((req, res, next) => { const myOptions = "" const filename = myHelper.getFilename(req) const nyc = cp.spawn(`nyc instrument ${myOptions} ${filename}`) nyc.stdout.pipe(res) }) ``` -------------------------------- ### Run Tests with NYC Coverage Source: https://github.com/istanbuljs/nyc/blob/main/README.md Configure npm scripts to run your tests with nyc for coverage reporting. This is a common setup for integrating coverage into your CI/CD pipeline. ```json { "scripts": { "test": "mocha", "coverage": "nyc npm run test" } } ``` -------------------------------- ### Use Latest NYC via npx Source: https://github.com/istanbuljs/nyc/blob/main/README.md Execute nyc using npx to always use the latest version without installing it globally. Pinning to a specific major version like 'nyc@latest' is recommended to avoid unexpected updates. ```json { "scripts": { "test": "npx nyc@latest mocha" } } ``` -------------------------------- ### Configure Include and Exclude Arrays Source: https://github.com/istanbuljs/nyc/blob/main/README.md Use 'include' to specify paths for instrumentation and 'exclude' to omit specific files. The 'exclude' array can also contain negated paths to re-include previously excluded files. ```json { "all": true, "include": [ "src/**/*.js" ], "exclude": [ "**/*.spec.js" ] } ``` -------------------------------- ### Pre-instrument Source Files Source: https://github.com/istanbuljs/nyc/blob/main/docs/instrument.md Use this command to create pre-instrumented source code. Specify an input file or directory and an optional output directory. If no output is specified, instrumented code is sent to stdout. ```bash nyc instrument [output] ``` -------------------------------- ### Configure Coverage Thresholds Source: https://github.com/istanbuljs/nyc/blob/main/README.md Define minimum coverage percentages for branches, lines, functions, and statements. If 'check-coverage' is true, the build will fail if coverage drops below these thresholds. Set 'per-file' to true for per-file checks. ```json { "branches": 80, "lines": 80, "functions": 80, "statements": 80 } ``` -------------------------------- ### Configure NYC Reporters and Runner Source: https://github.com/istanbuljs/nyc/blob/main/README.md Run a test runner (e.g., ava) with nyc, specifying multiple reporters for different coverage output formats like lcov and text-summary. ```shell nyc --reporter=lcov --reporter=text-summary ava ``` -------------------------------- ### Run Test Suite and Generate NYC Self-Coverage Report Source: https://github.com/istanbuljs/nyc/blob/main/docs/profiling.md Clear existing self-coverage and cache, run the test suite, and then generate a self-coverage report in the NYC directory. This process captures performance data. ```sh cd /user/dev/nyc trash ./.self_coverage cd /user/dev/ava trash ./.nyc_cache npm test cd /user/dev/nyc npm run report ``` -------------------------------- ### Configure npm Scripts for Coverage Source: https://github.com/istanbuljs/nyc/blob/main/docs/setup-codecov.md Update your package.json scripts to include testing with nyc and running codecov. ```json { "scripts": { "test": "nyc --reporter=lcov mocha", "coverage": "codecov" } } ``` -------------------------------- ### Combine NYC Reports from Multiple Runs Source: https://github.com/istanbuljs/nyc/blob/main/README.md Configure npm scripts to run multiple test suites with nyc and combine their coverage reports. Use the '--no-clean' flag for subsequent runs to accumulate results. ```json { "scripts": { "cover": "npm run cover:unit && npm run cover:integration && npm run cover:report", "cover:unit": "nyc --silent npm run test:unit", "cover:integration": "nyc --silent --no-clean npm run test:integration", "cover:report": "nyc report --reporter=lcov --reporter=text" } } ``` -------------------------------- ### Extend NYC Configuration with Overrides Source: https://github.com/istanbuljs/nyc/blob/main/README.md Define custom nyc configurations by extending a preset (e.g., '@istanbuljs/nyc-config-typescript') and adding specific overrides like enabling 'all' coverage and 'check-coverage'. ```json { "extends": "@istanbuljs/nyc-config-typescript", "all": true, "check-coverage": true } ``` -------------------------------- ### Configure Travis CI for Coverage Reports Source: https://github.com/istanbuljs/nyc/blob/main/docs/setup-coveralls.md Add the 'after_success' hook to your .travis.yml file to automatically run the coverage script after successful test execution. ```yaml after_success: npm run coverage ``` -------------------------------- ### Update package.json Scripts for Coverage Source: https://github.com/istanbuljs/nyc/blob/main/docs/setup-coveralls.md Configure the 'test' and 'coverage' scripts in your package.json to run nyc tests and generate coverage reports piped to coveralls. ```json { "scripts": { "test": "nyc mocha", "coverage": "nyc report --reporter=text-lcov | coveralls" } } ``` -------------------------------- ### Customizing File Exclusion in nyc.config.js Source: https://github.com/istanbuljs/nyc/blob/main/README.md Configure nyc to exclude platform-specific files based on the operating system. This snippet demonstrates how to dynamically determine which file to exclude and combine it with default exclusions. ```javascript 'use strict'; const defaultExclude = require('@istanbuljs/schema/default-exclude'); const isWindows = require('is-windows'); let platformExclude = [ isWindows() ? 'lib/posix.js' : 'lib/win32.js' ]; module.exports = { exclude: platformExclude.concat(defaultExclude) }; ``` -------------------------------- ### Require Additional Modules Source: https://github.com/istanbuljs/nyc/blob/main/README.md Use the --require flag to load additional modules in the subprocess collecting coverage. This is particularly useful when working with tools like esm or when using --all with Babel. ```shell nyc --require esm mocha ``` -------------------------------- ### Configure High and Low Watermarks in NYC Source: https://github.com/istanbuljs/nyc/blob/main/README.md Specify custom high and low watermarks for coverage reports in nyc's configuration file. This helps in visualizing test coverage levels. ```json { "watermarks": { "lines": [80, 95], "functions": [80, 95], "branches": [80, 95], "statements": [80, 95] } } ``` -------------------------------- ### Ignore Specific Methods in NYC Configuration Source: https://github.com/istanbuljs/nyc/blob/main/README.md Add method names to the 'ignore-class-method' array in your nyc configuration to exclude all instances of these methods from coverage tracking. ```json { "ignore-class-method": ["render"] } ``` -------------------------------- ### Modify Test Script for Direct NYC Usage Source: https://github.com/istanbuljs/nyc/blob/main/docs/profiling.md Update the test script in the test project's package.json to use the `nyc` binary directly and disable Tap's version with `--no-cov`. This is necessary when using a globally linked NYC. ```json { "scripts" : { "test": "nyc tap --no-cov test/*.js" } } ``` -------------------------------- ### Include Files within node_modules Source: https://github.com/istanbuljs/nyc/blob/main/README.md Set 'excludeNodeModules' to false to prevent '**/node_modules/**' from being added to the exclude list. This allows for more granular control over which files within node_modules are instrumented, often used in conjunction with specific 'include' paths. ```json { "all": true, "include": [ "lib/**", "node_modules/@my-org/**" ], "exclude": [ "node_modules/@my-org/something/unwanted.js", "**/test/**" ], "excludeNodeModules": false } ``` -------------------------------- ### Merge NYC Coverage Reports Source: https://github.com/istanbuljs/nyc/blob/main/README.md Use the 'nyc merge' command to produce a single raw coverage output file by combining results from multiple test runs. This is useful for external tools that require a unified coverage report. ```json { "scripts": { "cover:merge": "npm run cover:unit && npm run cover:integration && nyc merge .nyc_output coverage.json" } } ``` -------------------------------- ### Clean NYC Self-Coverage Scripts Source: https://github.com/istanbuljs/nyc/blob/main/docs/profiling.md Remove the self-coverage scripts from the NYC directory to revert to using regular files. This is useful when self-coverage is no longer needed for profiling. ```sh cd /user/dev/nyc npm run clean ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.