Javascript: Mocking Constructor using Sinon

I needed a solution for this because my code was calling the new operator. I wanted to mock the object that the new call created. var MockExample = sinon.stub(); MockExample.prototype.test = sinon.stub().returns(“42”); var example = new MockExample(); console.log(“example: ” + example.test()); // outputs 42 Then I used rewire to inject it into the code that …

Read more

How do I stub new Date() using sinon?

I suspect you want the useFakeTimers function: var now = new Date(); var clock = sinon.useFakeTimers(now.getTime()); //assertions clock.restore(); This is plain JS. A working TypeScript/JavaScript example: var now = new Date(); beforeEach(() => { sandbox = sinon.sandbox.create(); clock = sinon.useFakeTimers(now.getTime()); }); afterEach(() => { sandbox.restore(); clock.restore(); });

Sinon JS “Attempted to wrap ajax which is already wrapped”

You have to remove the spy after every test. Take a look at the example from the sinon docs: { setUp: function () { sinon.spy(jQuery, “ajax”); }, tearDown: function () { jQuery.ajax.restore(); // Unwraps the spy }, “test should inspect jQuery.getJSON’s usage of jQuery.ajax”: function () { jQuery.getJSON(“/some/resource”); assert(jQuery.ajax.calledOnce); assertEquals(“/some/resource”, jQuery.ajax.getCall(0).args[0].url); assertEquals(“json”, jQuery.ajax.getCall(0).args[0].dataType); } } …

Read more

How does one stub promise with sinon?

At current sinon version v2.3.1, you can use stub.resolves(value) and stub.rejects(value) function For example, you can stub myClass.myFunction with following code sinon.stub(myClass, ‘myFunction’).resolves(‘the value you want to return’); or sinon.stub(myClass, ‘myFunction’).rejects(‘the error information you want to return’);

How to stub process.env in node.js?

From my understanding of process.env, you can simply treat it like any other variable when setting its properties. Keep in mind, though, that every value in process.env must be a string. So, if you need a particular value in your test: it(‘does something interesting’, () => { process.env.NODE_ENV = ‘test’; // … }); To avoid …

Read more

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 …

Read more

Sinon error Attempted to wrap function which is already wrapped

You should restore the getObj in after() function, please try it as below. describe(‘App Functions’, function(){ var mockObj; before(function () { mockObj = sinon.stub(testApp, ‘getObj’, () => { console.log(‘this is sinon test 1111’); }); }); after(function () { testApp.getObj.restore(); // Unwraps the spy }); it(‘get results’,function(done) { testApp.getObj(); }); }); describe(‘App Errors’, function(){ var mockObj; …

Read more