istanbuljs / nyc

the Istanbul command line interface
https://istanbul.js.org/
ISC License
5.56k stars 356 forks source link

nyc-config-typescript and decorators #1437

Open cexbrayat opened 5 years ago

cexbrayat commented 5 years ago

Considering a simple TS file like src/main.ts

function Foo(constructorFunction: Function) {
  console.log('Foo decorator')
}

@Foo
class Main {
  constructor() {
    console.log('Running')
  }
}

new Main();

then trying to instrument it by running:

yarn nyc instrument --compact=false src instrumented

with the following configuration:

"nyc": {
  "extends": "@istanbuljs/nyc-config-typescript",
  "all": true
}

then the result file instrumented/main.ts is not instrumented.

If we comment the @Foo decorator, then the file is instrumented again.

function Foo(constructorFunction: Function) {
  console.log('Foo decorator')
}

// @Foo
class Main {
  constructor() {
    console.log('Running')
  }
}

new Main();

result instrumented/main.ts:

// ...
function Foo(constructorFunction: Function) {
  cov_1eomntdplb.f[0]++;
  cov_1eomntdplb.s[0]++;
  console.log('Foo decorator');
} // @Foo

class Main {
  constructor() {
    cov_1eomntdplb.f[1]++;
    cov_1eomntdplb.s[1]++;
    console.log('Running');
  }
}

cov_1eomntdplb.s[2]++;
new Main();

Am I missing something or is there an issue with decorators?

coreyfarrell commented 4 years ago

Parsing decorators is not enabled by default for nyc, by default we only enable parser plugins for features which are natively supported in a released version of node.js - classProperties for example. You will need to add "parserPlugins": ["decorators"] or "parserPlugins": ["decorators-legacy"] to your nyc config. See https://babeljs.io/docs/en/babel-parser#plugins for a list of all possible plugins, see https://github.com/istanbuljs/istanbuljs/blob/istanbul-lib-instrument%403.3.0/packages/istanbul-lib-instrument/src/instrumenter.js#L26 for the parser plugins used by default in nyc@14.1.1.

doctorRush commented 4 years ago

where i will find nyc config i just need to instrument a simple .ts file with decorator . i had installed nyc and instanbul . Or there is something we need to change in Package.json of our angularApp? Please Help

OnTheWay111 commented 4 years ago

@coreyfarrell

Hi, can you help me? I add "parserPlugins": ["decorators"] like this, but is does not work!!

image

jixiang-zhang commented 3 years ago

@coreyfarrell

Hi, can you help me? I add "parserPlugins": ["decorators"] like this, but is does not work!!

image look here ,maybe work for you https://github.com/istanbuljs/nyc/issues/1334#issuecomment-725369527

OnTheWay111 commented 3 years ago

ok thanks

------------------ Original ------------------ From: zhangyanqi1993 <notifications@github.com> Date: Thu,Dec 24,2020 4:34 PM To: istanbuljs/istanbuljs <istanbuljs@noreply.github.com> Cc: OnTheWay111 <851462306@qq.com>, Comment <comment@noreply.github.com> Subject: Re: [istanbuljs/istanbuljs] nyc-config-typescript and decorators (#459)

molten-firescar96 commented 3 years ago

I had, I believe the same problem working with istanbul (as provided by jest) and trying to use vue-class-component decorators. It was resolved by adding these plugins to my babel config

'@babel/plugin-proposal-decorators', '@babel/plugin-transform-classes'

making the full config

module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset',
  ],
  plugins: [
    ['@babel/plugin-proposal-decorators', { legacy: true }], '@babel/plugin-transform-classes'],
};
wyp0011 commented 2 years ago

I found that extends will replace parser-plugins in loading nyc config.

So two way to solve this problem.

First one, use parser-plugins and remove extends from your nyc config.

"nyc": {
  "parser-plugins": [
        "typescript",
        "decorators-legacy"
    ]
  "all": true
}

Second one, new nyc-config.js and extends from this file.

'use strict';

const { parserPlugins } = require('@istanbuljs/schema').defaults.nyc;

module.exports = {
    cache: false,
    parserPlugins: parserPlugins.concat('typescript', 'decorators-legacy')
};
"nyc": {
  "extends": "./nyc-config.js",
  "all": true
}
bcoe commented 2 years ago

@wyp0011 any interest in submitting a patch that adds docs to the README.md with your example?

rishi1117 commented 2 years ago

hey All, @wyp0011 i have tried the first option that you suggested. That does instrument the .ts files with decorators but it does not work properly. It is giving the issue export is moved before decorators which gives error for compiling

Can anyone please help me with this.

gaollard commented 1 year ago

same issue

krishollenbeck commented 1 week ago

I would like to confirm this is still an issue. I ran into this while trying to instrument angular component files. I was able to get them to instrument properly with @wyp0011 suggestion. This can be easily reproduce by running npx nyc instrument src/ instrumented from angular project. You will see the instrumented folder is created. But only the typescript files without decorators are properly instrumented.