8

Is there a way to stub an ES6 class method using Mocha/Sinon?

I'm trying to do this...

sinon.stub(Factory, 'announce');

but I just get the following error...

TypeError: Attempted to wrap undefined property announce as function
michael
  • 4,427
  • 6
  • 38
  • 57

1 Answers1

22

Instance methods are still placed on the prototype object of a class to be inherited from, not on its constructor, even if the class syntax obscures that a bit. Use

sinon.stub(Factory.prototype, 'announce');
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • and you can use `stub(obj, 'meth').callsFake(fn)` if you need to override the function. https://sinonjs.org/releases/v6.1.5/stubs/ – mikey Aug 16 '18 at 00:50