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.

nyc-config-typescript and decorators

See original GitHub issue

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?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
wyp0011commented, Nov 11, 2021

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
}
0reactions
rishi1117commented, Dec 30, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

nyc-config-typescript and decorators · Issue #1437 · istanbuljs ...
where i will find nyc config i just need to instrument a simple .ts file with decorator . i had installed nyc and...
Read more >
@istanbuljs/nyc-config-typescript - npm
nyc configuration that works with typescript. ... Start using @istanbuljs/nyc-config-typescript in your project by running `npm i ...
Read more >
getting nyc/istanbul coverage report to work with typescript
Recently I found a satisfiable solution by using "target": "es6" instead of es5 in tsconfig.json 's compilerOptions .
Read more >
sequelize-typescript | Yarn - Package Manager
sequelize-typescript. Build Status codecov NPM. Decorators and some other features for sequelize (v6). Installation; Model Definition.
Read more >
@istanbuljs/nyc-config-typescript - npm package | Snyk
nyc -config-typescript ... Handy default configuration for instrumenting your TypeScript-backed project with test coverage using nyc. ... Your tsconfig.json must be ...
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