Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for decorator coverage #488

Merged
merged 8 commits into from
Apr 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Eric Anderson <e@ericlanderson.com>
Felipe Matos <felipems@yahoo.com.br>
Forbes Lindesay <forbes@lindesay.co.uk>
Gino Zhang <whitetrefoil@gmail.com>
Gregor Stamac <1668205+gstamac@users.noreply.github.com>
Gustav Wengel <gustavwengel@gmail.com>
Henry Zektser <japhar81@gmail.com>
Ihor Chulinda <ichulinda@gmail.com>
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the name for this configuration option is a bad choice. I'd recommend something such as enableDecoratorIgnoreComment. Maybe this should simply be true always and not configurable.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is basically a fix for using typescript with istanbul I think both should default to true.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not set to true by default because ts-jest is not the right place for this. It is only being put in here because it seems like the fastest option to help address this issue and this shouldn't affect users who don't care about this part

"ignoreCoverageForAllDecorators": true
}
}
}
}
```

## Use cases

### React Native
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/jest-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,6 @@ export interface TsJestConfig {
enableInternalCache?: boolean;
enableTsDiagnostics?: boolean;
disableSourceMapSupport?: boolean;
ignoreCoverageForDecorators?: boolean;
ignoreCoverageForAllDecorators?: boolean;
}
18 changes: 16 additions & 2 deletions src/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,28 @@ 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,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it's ts-jest which is parsing the /* istanbul ignore decorator */ comments, maybe it should be named /* ts-jest ignore decorator coverage */.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the comment is actually processed by istanbul and not by ts-jest so the current form is correct

'/* istanbul ignore next$2*/$1',
);
}

const postHook = getPostProcessHook(
compilerOptions,
jestConfig,
tsJestConfig,
);

const outputText = postHook(
tsTranspiled.outputText,
tsTranspiledText,
filePath,
jestConfig,
transformOptions,
Expand All @@ -71,7 +85,7 @@ export function process(
const modified =
tsJestConfig.disableSourceMapSupport === true
? outputText
: injectSourcemapHook(filePath, tsTranspiled.outputText, outputText);
: injectSourcemapHook(filePath, tsTranspiledText, outputText);

flushLogs();

Expand Down