Test-Spy Implementation I have tried to write a test-spy myself. Just for getting more familar with the topic.

Here's the code:



// --------- SPY - Start -----------------------------------
class Spy {
  constructor(func) {
    this.func = func;
    this.returnValue = null;
    this.result = null;
    this.countFuncCalled = 0;
  }

  invoke(...givenArgs) {
    this.receivedArgs = givenArgs;
    this.returnValue = this.func(...givenArgs);
    this.countFuncCalled++;
  }
}
// --------- SPY - End ------------------------------------

const calc = {
  add: (a, b) =&gt; {
    return a + b;
  },
  sub: (a, b) =&gt; {
    return a - b;
  }
}

const addSpy = new Spy(calc.sub);

addSpy.invoke(9, 4);
console.log(`Used arguments: ${addSpy.receivedArgs.join(", ")}`);
console.log(`Return value: ${addSpy.returnValue}`);
console.log(`Count of function-calls: ${addSpy.countFuncCalled}`);

addSpy.invoke(8, 7);
console.log(`Used arguments: ${addSpy.receivedArgs.join(", ")}`);
console.log(`Return value: ${addSpy.returnValue}`);
console.log(`Count of function-calls: ${addSpy.countFuncCalled}`);




What do you think about my implementation? Is is done in a basically correct way?

I'm I using the ES6-features (Classes, Rest, Spread) right?

What would you have done differently and why?

Looking forward to reading your comments and answers.
What do you think about my implementation? Is is done in a basically correct way?


Yeah who is to say what is "correct"? I mostly have worked with the chaiJS spies API and it feels like a basic version of that library. 


  I'm I using the ES6-features (Classes, Rest, Spread) right?


While there isn't much to the code, I would say it looks like those features are used correctly. You could consider using more class features like getters and setters



In the constructor, there exists this line:


this.result = null;



but the result property doesn't appear to be used anywhere. What is that for?