diff --git a/AUTHORS b/AUTHORS index 98c3556f34..1b83bd0a75 100644 --- a/AUTHORS +++ b/AUTHORS @@ -25,6 +25,7 @@ Eric Anderson Felipe Matos Forbes Lindesay Gino Zhang +Gregor Stamac <1668205+gstamac@users.noreply.github.com> Gustav Wengel Henry Zektser Ihor Chulinda diff --git a/README.md b/README.md index 520ca68c25..0fca6fc51a 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ ts-jest is a TypeScript preprocessor with source map support for Jest that lets - [Using `.babelrc`](#using-babelrc) - [Using a custom Babel config](#using-a-custom-babel-config) - [TS compiler & error reporting](#ts-compiler--error-reporting) + - [Ignore coverage on decorators](#ignore-coverage-on-decorators) - [Use cases](#use-cases) - [React Native](#react-native) - [Angular 2](#angular-2) @@ -233,6 +234,25 @@ If you want to enable Syntactic & Semantic TypeScript error reporting you can en } ``` +### Ignore coverage on decorators + +**Note:** This is an experimental feature, comes with no guarantees and could be removed if it causes more problems than it solves + +If you want to ignore coverage on decorators you can enable this through `ignoreCoverageForDecorators` and `ignoreCoverageForAllDecorators` flags. If you enable the first option you have to add the `/* istanbul ignore decorator */` comment after the decorator. If you choose the second option all decorators will be ignored. + +```json +{ + "jest": { + "globals": { + "ts-jest": { + "ignoreCoverageForDecorators": true, + "ignoreCoverageForAllDecorators": true + } + } + } +} +``` + ## Use cases ### React Native diff --git a/package.json b/package.json index aa511c0847..0143d4b991 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-jest", - "version": "22.4.3", + "version": "22.4.4", "main": "index.js", "types": "./dist/index.d.ts", "description": "A preprocessor with sourcemap support to help use Typescript with Jest", diff --git a/src/jest-types.ts b/src/jest-types.ts index cc60c276c7..a21320758d 100644 --- a/src/jest-types.ts +++ b/src/jest-types.ts @@ -86,4 +86,6 @@ export interface TsJestConfig { enableInternalCache?: boolean; enableTsDiagnostics?: boolean; disableSourceMapSupport?: boolean; + ignoreCoverageForDecorators?: boolean; + ignoreCoverageForAllDecorators?: boolean; } diff --git a/src/preprocessor.ts b/src/preprocessor.ts index 3026b43f0a..1933b995d5 100644 --- a/src/preprocessor.ts +++ b/src/preprocessor.ts @@ -55,6 +55,20 @@ export function process( fileName: filePath, }); + let tsTranspiledText = tsTranspiled.outputText; + if (tsJestConfig.ignoreCoverageForAllDecorators === true) { + tsTranspiledText = tsTranspiledText.replace( + /__decorate/g, + '/* istanbul ignore next */__decorate', + ); + } + if (tsJestConfig.ignoreCoverageForDecorators === true) { + tsTranspiledText = tsTranspiledText.replace( + /(__decorate\(\[\r?\n[^\n\r]*)\/\*\s*istanbul\s*ignore\s*decorator(.*)\*\//g, + '/* istanbul ignore next$2*/$1', + ); + } + const postHook = getPostProcessHook( compilerOptions, jestConfig, @@ -62,7 +76,7 @@ export function process( ); const outputText = postHook( - tsTranspiled.outputText, + tsTranspiledText, filePath, jestConfig, transformOptions, @@ -71,7 +85,7 @@ export function process( const modified = tsJestConfig.disableSourceMapSupport === true ? outputText - : injectSourcemapHook(filePath, tsTranspiled.outputText, outputText); + : injectSourcemapHook(filePath, tsTranspiledText, outputText); flushLogs();