renekton
renekton

Reputation: 13

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

Upvotes: 1

Views: 391

Answers (2)

ArtBindu
ArtBindu

Reputation: 2016

create a global object like:

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

Now you can access performance.now()

Upvotes: 1

Jordan Kasper
Jordan Kasper

Reputation: 13273

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.

Upvotes: 0

Related Questions