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

How to mock localStorage in JavaScript unit tests?

Here is a simple way to mock it with Jasmine: let localStore; beforeEach(() => { localStore = {}; spyOn(window.localStorage, ‘getItem’).and.callFake((key) => key in localStore ? localStore[key] : null ); spyOn(window.localStorage, ‘setItem’).and.callFake( (key, value) => (localStore[key] = value + ”) ); spyOn(window.localStorage, ‘clear’).and.callFake(() => (localStore = {})); }); If you want to mock the local storage … 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

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