What is @istanbuljs/nyc-config-typescript?
@istanbuljs/nyc-config-typescript is a configuration preset for the NYC code coverage tool, specifically tailored for TypeScript projects. It simplifies the setup process for collecting code coverage metrics in TypeScript applications by providing a pre-configured set of options that work out of the box.
What are @istanbuljs/nyc-config-typescript's main functionalities?
Pre-configured NYC settings for TypeScript
This feature allows you to extend the default NYC configuration with settings optimized for TypeScript. By adding this to your `nyc` configuration in `package.json`, you can easily integrate code coverage into your TypeScript project.
{
"extends": "@istanbuljs/nyc-config-typescript"
}
Source map support
This configuration ensures that source maps are properly handled, allowing for accurate code coverage reports that map back to the original TypeScript source files. It includes all TypeScript files in the `src` directory and excludes declaration files.
{
"nyc": {
"extends": "@istanbuljs/nyc-config-typescript",
"all": true,
"check-coverage": true,
"reporter": ["html", "text-summary"],
"include": ["src/**/*.ts"],
"exclude": ["**/*.d.ts"]
}
}
Other packages similar to @istanbuljs/nyc-config-typescript
ts-jest
ts-jest is a Jest transformer that allows you to use TypeScript with Jest. It provides a seamless integration for running tests written in TypeScript and generating code coverage reports. Unlike @istanbuljs/nyc-config-typescript, which is specific to NYC, ts-jest is tailored for Jest users.
babel-plugin-istanbul
babel-plugin-istanbul is a Babel plugin that instruments your code with Istanbul for code coverage. It works with any JavaScript project that uses Babel, including those that use TypeScript via Babel. This plugin is more flexible but requires a Babel setup, unlike @istanbuljs/nyc-config-typescript, which is a straightforward configuration preset.
jest
Jest is a popular testing framework that includes built-in support for code coverage. It can be used with TypeScript through the ts-jest transformer. Jest provides an all-in-one solution for testing and code coverage, whereas @istanbuljs/nyc-config-typescript is focused solely on configuring NYC for TypeScript.
nyc-config-typescript
Handy default configuration for instrumenting your TypeScript-backed
project with test coverage using nyc.
First install the dependencies:
npm i -D nyc source-map-support ts-node @istanbuljs/nyc-config-typescript
Your tsconfig.json
must be configured to produce source maps, either inline or as sibling files.
.nycrc
And write a .nycrc
that looks like this:
{
"extends": "@istanbuljs/nyc-config-typescript",
"all": true
}
This package specifies the cache
, exclude
, and extension
options for you - only override those if you absolutely must.
If you are going to modify include
or exclude
and you have specified a separate outDir
in tsconfig.json
, make sure that it remains included so that source mapping is possible.
Running Tests
If you're using mocha
In test/mocha.opts
:
--require ts-node/register #replace with ts-node/register/transpile-only if you have custom types
--require source-map-support/register
--recursive
<glob for your test files>
Now setup the test scripts in your package.json
like so (with the equivalent for your test runner):
{
"test": "tsc && nyc mocha"
}
If you're using Jasmine
In package.json
:
{
"test": "tsc && nyc --require ts-node/register jasmine"
}
License
ISC