0

I declared a property in class constructor and accessing through methods, which is declared as 'static', with 'this' and it is not accessible. How to get access to constructor (class) variables inside static methods?

export class Reporter {
    constructor() {
        this.jsonReports = path.join(process.cwd(), "/reports/json")

        this.cucumberReporterOptions = {
            jsonFile: targetJson,
            output: htmlReports + "/cucumber_reporter.html",
            reportSuiteAsScenarios: true,
            theme: "bootstrap",
        }
    }

    static createHTMLReport() {
        try {
            reporter.generate(this.cucumberReporterOptions);
        } catch (err) {

        }
    }
}

Updated:

As per "@CodingIntrigue", I have done like this in 'reporter.js' file and called the method as Reporter.createHTMLReport() in my config file and its working as expected. But not sure if this is best practice.

const jsonReports = path.join(process.cwd(), "/reports/json")

const cucumberReporterOptions = {
    jsonFile: targetJson,
    output: htmlReports + "/cucumber_reporter.html",
    reportSuiteAsScenarios: true,
    theme: "bootstrap",
}

export class Reporter {
    static createHTMLReport() {
        try {
            reporter.generate(cucumberReporterOptions);
        } catch (err) {

        }
    }
}
6
  • 4
    You don't, that's not what static methods are for. Commented May 25, 2018 at 12:15
  • 3
    You can't, that's why the method is static, there is no access to instance members. Commented May 25, 2018 at 12:15
  • 3
    Reporter doesn't sound like it should be a class at all. Commented May 25, 2018 at 12:17
  • 1
    Possible duplicate of: stackoverflow.com/questions/44091167/… Commented May 25, 2018 at 12:20
  • 1
    @mmar Can you link the docs of your framework about this? I really can't believe it requires a class. Commented May 25, 2018 at 13:21

1 Answer 1

1

If you want to continue using class syntax, you can just make jsonReports and cucubmerReporterOptions static properties too:

export class Reporter {
    static createHTMLReport() {
        try {
            reporter.generate(Reporter.cucumberReporterOptions);
        } catch (err) {

        }
    }
}

Reporter.jsonReports = path.join(process.cwd(), "/reports/json")

Reporter.cucumberReporterOptions = {
    jsonFile: targetJson,
    output: htmlReports + "/cucumber_reporter.html",
    reportSuiteAsScenarios: true,
    theme: "bootstrap",
}
Sign up to request clarification or add additional context in comments.

4 Comments

I have updated the question with the solution I have done as per your suggestion and its working too. But not sure if this could be a good practice to follow.
That practice is fine. You should post your code in an answer
@CodingIntrigue Using a class without a constructor, without instance properties, without any instance methods, is not a "fine practice".
@Bergi I did say in the comments above that it shouldn't be a class at all, but I was going on the possibly incorrect assumption that not everything was going to be static in the class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.