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

4.0.0.Beta: Must call super constructor in derived class before accessing 'this' or returning from derived constructor #2474

Closed
nicojs opened this issue Sep 11, 2020 · 2 comments · Fixed by #2484
Labels
🐛 Bug Something isn't working

Comments

@nicojs
Copy link
Member

nicojs commented Sep 11, 2020

When running Stryker on its own @stryker-mutator/karma-runner, we now get this error:

ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor
ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor
    at new StrykerReporter (/home/nicojs/stryker/packages/karma-runner/.stryker-tmp/sandbox1223810/src/karma-plugins/StrykerReporter.ts:51:10)
    at Object.<anonymous> (/home/nicojs/stryker/packages/karma-runner/.stryker-tmp/sandbox1223810/src/karma-plugins/StrykerReporter.ts:68:39

Cause:

This:

export default class StrykerReporter extends EventEmitter implements karma.Reporter {
  public adapters: any[] = [];

  private constructor() {
    super();
  }
  // ...
}

Is mutated to:

export default class StrykerReporter extends EventEmitter implements karma.Reporter {
  public adapters: any[] = __global_69fa48.__activeMutant__ === 10 ? ["Stryker was here"] : (__global_69fa48.__coverMutant__(10), []);

  private constructor() {
    switch (__global_69fa48.__activeMutant__) {
      case 11:
        {}
        break;

      default:
        __global_69fa48.__coverMutant__(11);
        {
          super();
        }
        break;
    }
  }
 // ...
}

Which is then transpiled to:

class StrykerReporter extends events_1.EventEmitter {
    constructor() {
        this.adapters = __global_69fa48.__activeMutant__ === 10 ? ["Stryker was here"] : (__global_69fa48.__coverMutant__(10), []);
        // ...
        switch (__global_69fa48.__activeMutant__) {
            case 11:
                { }
                break;
            default:
                __global_69fa48.__coverMutant__(11);
                {
                    super();
                }
                break;
        }
    }
   // ...
}
@nicojs nicojs added the 🐛 Bug Something isn't working label Sep 11, 2020
nicojs added a commit that referenced this issue Sep 13, 2020
Enable mutation testing in the workflow again for the packages that work again 🎉

Still pending: mocha-runner,  jasmine-runner (#2473) and karma-runner (#2474).

Closes #2426
nicojs added a commit that referenced this issue Sep 17, 2020
…lass properties

Don't mutate constructor block statements when the class contains initialized class properties.

Fixes #2474
nicojs added a commit that referenced this issue Sep 17, 2020
…lass properties (#2484)

Don't mutate constructor block statements when the class contains initialized class properties. For example:

```ts
class Foo extends Baz {
  bar = 'bar';
  constructor() {  // Don't mutate this block!
     super();
  }
}
```

If you mutate the constructor block above, and TypeScript transpiles the code, it will call `this.bar` before the `super()` is initiated. Resulting in a runtime error.

Fixes #2474
@dimitrilahaye
Copy link

Hi!
Even with the 6.2.0-beta.0 a class like that:

export class BuyerRoute extends PbRoute {
  #traveler: string;
  
    constructor(input: BuyerRouteInput) {
        super(input);
        this.#traveler = input.traveler;
    }
}

Is still compiled by Stryker like that:

export class BuyerRoute extends PbRoute {
    #traveler: string;

    constructor(input: BuyerRouteInput) {
        if (stryMutAct_9fa48("647")) { {}
        } else {
            stryCov_9fa48("647");
            super(input);
            this.#traveler = input.traveler;
        }
    }
}

Here my stryker.conf.ts file:

// @ts-check
/** @type {import('@stryker-mutator/api/core').PartialStrykerOptions} */
const config = {
    _comment:
        "This config was generated using 'stryker init'. Please take a look at: https://stryker-mutator.io/docs/stryker-js/configuration/ for more information",
    packageManager: "yarn",
    reporters: ["html", "clear-text", "progress"],
    testRunner: "jest",
    coverageAnalysis: "perTest",
    checkers: ["typescript"],
    tsconfigFile: "tsconfig.json",
    transpilers: [
        "typescript", // Specify that your typescript code needs to be transpiled before tests can be run. Not needed if you're using ts-node Just-in-time compilation.
    ],
    jest: {
        projectType: "custom",
        configFile: "./jest.config.ts",
        enableFindRelatedTests: true,
    },
    disableTypeChecks: "{__tests__,src,lib}/**/*.{js,ts,jsx,tsx,html,vue}",
};
export default config;

@nicojs
Copy link
Member Author

nicojs commented Jul 3, 2022

This seems like old config, because i see you've configured transpilers. This is no longer supported. Can you confirm the Stryker version for me? What does npm ls @stryker-mutator/core say?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Bug Something isn't working
Projects
None yet
2 participants