Sinon.JS – How can I get arguments from a stub?

Thanks to Matt Mokary (if you want to submit an answer I’ll give you the credit for this one) I edited my test as follows… it(“Can show a fail message”, Sinon.test(function() { $(“body”).append(`<a class=”js-delete-button” data-id=”123″ data-delete-uri=”/delete/123″>Delete</a>`); let objUt = new DeleteButton($(“.js-delete-button”).get()[0]); let bootboxAlertStub = this.stub(Bootbox, ‘alert’); objUt.showFailed(); Sinon.assert.called(bootboxAlertStub); let options = bootboxAlertStub.getCall(0).args[0]; expect(options.message).to.equal(objUt.errorMessage); })); The … Read more

stubbing a function using jest

With jest you should use jest.spyOn: jest .spyOn(jQuery, “ajax”) .mockImplementation(({ success }) => success([ 1, 2, 3 ])); Full example: const spy = jest.fn(); const payload = [1, 2, 3]; jest .spyOn(jQuery, “ajax”) .mockImplementation(({ success }) => success(payload)); jQuery.ajax({ url: “https://example.api”, success: data => spy(data) }); expect(spy).toHaveBeenCalledTimes(1); expect(spy).toHaveBeenCalledWith(payload); You can try live example on codesandbox: … Read more

Calling original function from Sinon.js Stub

You could use a closure. For example: var obj = { foo: function () { console.log(‘foo’); } }; var stub = (function () { var originalFoo = obj.foo; return sinon.stub(obj, ‘foo’, function () { console.log(‘stub’); originalFoo(); }); }()); JSFiddle

How to stub a private method of a class written in typescript using sinon

The problem is that the definition for sinon uses the following definition for the stub function : interface SinonStubStatic { <T>(obj: T, method: keyof T): SinonStub; } This means that the second parameter must be the name of a member (a public one) of the T type. This is probably a good restriction generally, but … Read more

Mocking JavaScript constructor with Sinon.JS

You can either create a namespace or create a stub instance using sinon.createStubInstance (this will not invoke the constructor). Creating a namespace: const namespace = { Service: require(‘./service’) }; describe(‘Service’, function() { it(‘getData’, function() { sinon.stub(namespace, ‘Service’).returns(0); console.log(new namespace.Service()); // Service {} }); }); Creating a stub instance: let Service = require(‘./service’); describe(‘Service’, function() { … Read more

Understanding Sinon.js’s yield(), yields(), and callsArg()

I believe the methods, as outlined in the documentation, are as follows: spy.yield stub.yields stub.callsArg The main difference between yields and callsArg can be found in sinon’s documentation for yields: If a method accepts more than one callback, you need to use callsArg to have the stub invoke other callbacks than the first one. yields … Read more

Possible to stub method twice within a single test to return different results?

Sinon has a nice API for handling multiple calls (stub.onCall(n);) to the same stubbed method. Example from stub api doc: “test should stub method differently on consecutive calls”: function () { var callback = sinon.stub(); callback.onCall(0).returns(1); callback.onCall(1).returns(2); callback.returns(3); callback(); // Returns 1 callback(); // Returns 2 callback(); // All following calls return 3 } You … Read more

tech