Stubbing a class method with Sinon.js

Your code is attempting to stub a function on Sensor, but you have defined the function on Sensor.prototype.

sinon.stub(Sensor, "sample_pressure", function() {return 0})

is essentially the same as this:

Sensor["sample_pressure"] = function() {return 0};

but it is smart enough to see that Sensor["sample_pressure"] doesn't exist.

So what you would want to do is something like these:

// Stub the prototype's function so that there is a spy on any new instance
// of Sensor that is created. Kind of overkill.
sinon.stub(Sensor.prototype, "sample_pressure").returns(0);

var sensor = new Sensor();
console.log(sensor.sample_pressure());

or

// Stub the function on a single instance of 'Sensor'.
var sensor = new Sensor();
sinon.stub(sensor, "sample_pressure").returns(0);

console.log(sensor.sample_pressure());

or

// Create a whole fake instance of 'Sensor' with none of the class's logic.
var sensor = sinon.createStubInstance(Sensor);
console.log(sensor.sample_pressure());

The top answer is deprecated. You should now use:

sinon.stub(YourClass.prototype, 'myMethod').callsFake(() => {
    return {}
})

Or for static methods:

sinon.stub(YourClass, 'myStaticMethod').callsFake(() => {
    return {}
})

Or for simple cases just use returns:

sinon.stub(YourClass.prototype, 'myMethod').returns({})

sinon.stub(YourClass, 'myStaticMethod').returns({})

Or if you want to stub a method for an instance:

const yourClassInstance = new YourClass();
sinon.stub(yourClassInstance, 'myMethod').returns({})