1

I am writing unit tests in Ember-qunit. I want to set a custom value on performance.now.

I tried sinon.stub(performance,'now', 60000); but this didn't work. I get TypeError: stub(obj, 'meth', fn) has been removed.

how do i stub performance.now() using sinon.js?

Thanks

renekton
  • 13
  • 4

2 Answers2

1

create a global object like:

global.performance = {
   now() {
    return <returning_value>; // use Date.now() or any value
};

Now you can access performance.now()

Art Bindu
  • 769
  • 4
  • 14
0

Not sure what your third argument (60000) is supposed to be as I am not familiar with performance.now(), but that's not a valid call to Sinon.stub() (there is no 3rd parameter). Per the documentation though, you should be able to capture the stub function and then call a method on it to indicate the desired return value:

const stub = sinon.stub(performance, 'now');
stub.returns(60000);

Then, when the stub is called, you should get:

console.log( stub() );  // 60000

You can see this functionality in this jsfiddle example.

Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55