Search code examples
javascriptember.jssinonqunitember-qunit

stub performance.now() using Sinon.js


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


Solution

  • 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.