question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

coverage on decorated parameters is incorrect when `isolatedModules: true`

See original GitHub issue

Issue :

Coverage on decorated parameters is incorrect when isolatedModules: true

Expected behavior :

Coverage on decorated parameters are correct when isolatedModules option is not set. If isolatedModules: true, coverage is incorrect with branch not covered.

Minimal repo :

https://github.com/hoonoh/jest-decorator-coverage/tree/master

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:14

github_iconTop GitHub Comments

3reactions
vad3xcommented, Dec 9, 2021

I’ve ended up with this workaround (similar to what @LoicMahieu has):

// fix-istanbul-decorators.js

const { default: tsJest } = require('ts-jest');

module.exports = fixIstanbulDecoratorCoverageTransformer();

function fixIstanbulDecoratorCoverageTransformer() {
  const transformer = tsJest.createTransformer();
  const process = transformer.process.bind(transformer);
  transformer.process = (...args) => {
    let result = process(...args);
    // Ignore decorators on methods and properties
    result = result.replace(
      /__decorate/g,
      '/* istanbul ignore next */__decorate',
    );

    // When constructor parameters have decorated properties (eg @inject), TS adds
    // a typeof branch check, which we don't want to instrument
    result = result.replace(
      /(?<=__metadata\("design:paramtypes".*?)(typeof \(_\w\s*=)/g,
      '/* istanbul ignore next */$1',
    );

    return result;
  };

  return transformer;
}
// jest.config.js

...

  globals: {
    'ts-jest': {
      isolatedModules: true,
    },
  },
  transform: {
    '\\.(ts|tsx)$': '<rootDir>/fix-istanbul-decorators.js',
  },
...

Ref: https://github.com/istanbuljs/istanbuljs/issues/70#issuecomment-778275926

2reactions
mandarinicommented, Jul 25, 2022

We are facing the same issue, and this happens only when we’re using

    'ts-jest': {
      isolatedModules: true,
    },

The solutions provided in the comments (eg. this and this) do not seem to have any effect.

@micalevisk @vad3x these solutions do not seem to have an effect on my end.

Edit:

This is how I solved the issue, in an Angular app. I had to specifically add /* istanbul ignore next */ before every single constructor parameter, and add /* istanbul ignore next */ before the constructor, too.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TSConfig Reference - Docs on every TSConfig option
undefined (default) provide suggestions as warnings to editors; true unreachable code is ignored; false raises compiler errors about unreachable code.
Read more >
Can't run my Node.js Typescript project TypeError ...
When I try to start my app on Heroku I got the following stack trace. It is just a basic ts.app like you...
Read more >
Understanding TypeScript Configuration Options
We will cover the following configuration options in the docs: ... When setting isolatedModules to true , TypeScript will warn you if you...
Read more >
基于Typescript和Jest刷题环境搭建与使用
Interop Constraints */ // "isolatedModules": true, /* Ensure that each ... error messages // errorOnDeprecated: false, // Force coverage ...
Read more >
inversify/InversifyJS - Gitter
It is not impossible to pass parameters to middleware(It is possible but only by ... If it is approved for development I will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found