1

This is where I define my functions and export them using module.exports

class GITHelper extends Helper {

addLatestReport(){
  simpleGitPromise.addRemote(date,remote);
    return simpleGitPromise.add('.')
      .then(
          (addSuccess) => {
            console.log(addSuccess);
          }, (failedAdd) => {
            console.log('adding files failed');
      });
}

commitLatestReport(){
  console.log("Committing...");
  return simpleGit.commit("Latest output from Automated UI Testing", "./output");
}

pushLatestReport() {
    console.log('Pushing...');
    return simpleGit.push(remote);
}

}

module.exports = GITHelper;

I require this module in another node dependency file (mochawesome) using this

var gitHelper = require('../../../helpers/GITHelper.js');

and then I call the functions like so:

async function gitSender()
{
 await gitHelper.addLatestReport();
 await gitHelper.commitLatestReport();
 await gitHelper.pushlatestReport();
 console.log("Upload completed");
}

"TypeError: gitHelper.addLatestReport is not a function"

Are these not defined functions? Are they not being exported correctly?

1 Answers1

1

The methods in your GITHelper class are instance method (not static method). addLatestReport, commitLatestReport, pushLatestReport are in GITHelper.prototype. So the methods become meaningful from instance (const gitHelper = new GitHelper; gitHelper.addLatestReport();).

Try make a instance of GitHelper to use the methods

const GitHelper = require('../../../helpers/GITHelper.js');
const gitHelper = new GitHelper();

async function gitSender()
{
 await gitHelper.addLatestReport();
 await gitHelper.commitLatestReport();
 await gitHelper.pushlatestReport();
 console.log("Upload completed");
}

Another options is to make static method. If the methods don't use this keyword, the methods can be static.

class ConsoleExample {
   static sayHello() {
      console.log('hello')
   }
}


ConsoleExample.sayHello();
  • 1
    as a third alternative, you can export the class instance. `export default new GitHelper()`, then all importing modules can share that instance instead of creating their own. – rlemon Sep 25 '19 at 19:02