How to properly unit test jQuery’s .ajax() promises using Jasmine and/or Sinon?

It is not that complex actually. It suffices to return a promise and resolve it according to your case. For example: spyOn($, ‘ajax’).andCallFake(function (req) { var d = $.Deferred(); d.resolve(data_you_expect); return d.promise(); }); for a success, or spyOn($, ‘ajax’).andCallFake(function (req) { var d = $.Deferred(); d.reject(fail_result); return d.promise(); }); for a failure. For Jasmine 2.0 … Read more

How to Unit Test React-Redux Connected Components?

A prettier way to do this, is to export both your plain component, and the component wrapped in connect. The named export would be the component, the default is the wrapped component: export class Sample extends Component { render() { let { verification } = this.props; return ( <h3>This is my awesome component.</h3> ); } … 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’);

Proxyquire, rewire, SandboxedModule, and Sinon: pros & cons

It totally feels like cheating, but since no one else is answering the question, here goes: Proxyquire takes over require and lets you inject fakes anywhere in the dependency chain. For requires you don’t take over and for methods you don’t define for requires you do take over, it’ll fall back to the original. This … Read more

How to mock/replace getter function of object with Jest?

For anyone else stumbling across this answer, Jest 22.1.0 introduced the ability to spy on getter and setter methods. Edit: like in scieslak’s answer below, because you can spy on getter and setter methods, you can use Jest mocks with them, just like with any other function: class MyClass { get something() { return ‘foo’ … Read more

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)