Best JavaScript code snippet using sinon
lolex-test.js
Source:lolex-test.js  
...112        beforeEach(function () {113            this.clock = lolex.createClock();114        });115        it("returns numeric id or object with numeric id", function () {116            var result = this.clock.setImmediate(NOOP);117            if (typeof result === 'object') {118                assert.isNumber(result.id);119            } else {120                assert.isNumber(result);121            }122        });123        it("calls the given callback immediately", function () {124            var stub = sinon.stub();125            this.clock.setImmediate(stub);126            this.clock.tick(0);127            assert(stub.called);128        });129        it("throws if no arguments", function () {130            var clock = this.clock;131            assert.exception(function () {132                clock.setImmediate();133            });134        });135        it("manages separate timers per clock instance", function () {136            var clock1 = lolex.createClock();137            var clock2 = lolex.createClock();138            var stubs = [sinon.stub(), sinon.stub()];139            clock1.setImmediate(stubs[0]);140            clock2.setImmediate(stubs[1]);141            clock2.tick(0);142            assert.isFalse(stubs[0].called);143            assert(stubs[1].called);144        });145        it("passes extra parameters through to the callback", function () {146            var stub = sinon.stub();147            this.clock.setImmediate(stub, 'value1', 2);148            this.clock.tick(1);149            assert(stub.calledWithExactly('value1', 2));150        });151        it("calls the given callback before setTimeout", function () {152            var stub1 = sinon.stub.create();153            var stub2 = sinon.stub.create();154            this.clock.setTimeout(stub1, 0);155            this.clock.setImmediate(stub2);156            this.clock.tick(0);157            assert(stub1.calledOnce);158            assert(stub2.calledOnce);159            assert(stub2.calledBefore(stub1));160        });161        it("does not stuck next tick even if nested", function () {162            var clock = this.clock;163            clock.setImmediate(function f() {164                clock.setImmediate(f);165            });166            clock.tick(0);167        });168    });169    describe("clearImmediate", function () {170        beforeEach(function () {171            this.clock = lolex.createClock();172        });173        it("removes immediate callbacks", function () {174            var callback = sinon.stub();175            var id = this.clock.setImmediate(callback);176            this.clock.clearImmediate(id);177            this.clock.tick(1);178            assert.isFalse(callback.called);179        });180        it("does not remove timeout", function () {181            var callback = sinon.stub();182            var id = this.clock.setTimeout(callback, 50);183            assert.exception(function() {184                this.clock.clearImmediate(id);185            });186            this.clock.tick(55);187            assert.isTrue(callback.called);188        });189        it("does not remove interval", function () {190            var callback = sinon.stub();191            var id = this.clock.setInterval(callback, 50);192            assert.exception(function() {193                this.clock.clearImmediate(id);194            });195            this.clock.tick(55);196            assert.isTrue(callback.called);197        });198    });199    describe("tick", function () {200        beforeEach(function () {201            this.clock = lolex.install(0);202        });203        afterEach(function () {204            this.clock.uninstall();205        });206        it("triggers immediately without specified delay", function () {207            var stub = sinon.stub();208            this.clock.setTimeout(stub);209            this.clock.tick(0);210            assert(stub.called);211        });212        it("does not trigger without sufficient delay", function () {213            var stub = sinon.stub();214            this.clock.setTimeout(stub, 100);215            this.clock.tick(10);216            assert.isFalse(stub.called);217        });218        it("triggers after sufficient delay", function () {219            var stub = sinon.stub();220            this.clock.setTimeout(stub, 100);221            this.clock.tick(100);222            assert(stub.called);223        });224        it("triggers simultaneous timers", function () {225            var spies = [sinon.spy(), sinon.spy()];226            this.clock.setTimeout(spies[0], 100);227            this.clock.setTimeout(spies[1], 100);228            this.clock.tick(100);229            assert(spies[0].called);230            assert(spies[1].called);231        });232        it("triggers multiple simultaneous timers", function () {233            var spies = [sinon.spy(), sinon.spy(), sinon.spy(), sinon.spy()];234            this.clock.setTimeout(spies[0], 100);235            this.clock.setTimeout(spies[1], 100);236            this.clock.setTimeout(spies[2], 99);237            this.clock.setTimeout(spies[3], 100);238            this.clock.tick(100);239            assert(spies[0].called);240            assert(spies[1].called);241            assert(spies[2].called);242            assert(spies[3].called);243        });244        it("triggers multiple simultaneous timers with zero callAt", function () {245            var test = this;246            var spies = [247                sinon.spy(function () {248                    test.clock.setTimeout(spies[1], 0);249                }),250                sinon.spy(),251                sinon.spy()252            ];253            // First spy calls another setTimeout with delay=0254            this.clock.setTimeout(spies[0], 0);255            this.clock.setTimeout(spies[2], 10);256            this.clock.tick(10);257            assert(spies[0].called);258            assert(spies[1].called);259            assert(spies[2].called);260        });261        it("waits after setTimeout was called", function () {262            this.clock.tick(100);263            var stub = sinon.stub();264            this.clock.setTimeout(stub, 150);265            this.clock.tick(50);266            assert.isFalse(stub.called);267            this.clock.tick(100);268            assert(stub.called);269        });270        it("mini integration test", function () {271            var stubs = [sinon.stub(), sinon.stub(), sinon.stub()];272            this.clock.setTimeout(stubs[0], 100);273            this.clock.setTimeout(stubs[1], 120);274            this.clock.tick(10);275            this.clock.tick(89);276            assert.isFalse(stubs[0].called);277            assert.isFalse(stubs[1].called);278            this.clock.setTimeout(stubs[2], 20);279            this.clock.tick(1);280            assert(stubs[0].called);281            assert.isFalse(stubs[1].called);282            assert.isFalse(stubs[2].called);283            this.clock.tick(19);284            assert.isFalse(stubs[1].called);285            assert(stubs[2].called);286            this.clock.tick(1);287            assert(stubs[1].called);288        });289        it("triggers even when some throw", function () {290            var clock = this.clock;291            var stubs = [sinon.stub().throws(), sinon.stub()];292            clock.setTimeout(stubs[0], 100);293            clock.setTimeout(stubs[1], 120);294            assert.exception(function () {295                clock.tick(120);296            });297            assert(stubs[0].called);298            assert(stubs[1].called);299        });300        it("calls function with global object or null (strict mode) as this", function () {301            var clock = this.clock;302            var stub = sinon.stub().throws();303            clock.setTimeout(stub, 100);304            assert.exception(function () {305                clock.tick(100);306            });307            assert(stub.calledOn(global) || stub.calledOn(null));308        });309        it("triggers in the order scheduled", function () {310            var spies = [sinon.spy(), sinon.spy()];311            this.clock.setTimeout(spies[0], 13);312            this.clock.setTimeout(spies[1], 11);313            this.clock.tick(15);314            assert(spies[1].calledBefore(spies[0]));315        });316        it("creates updated Date while ticking", function () {317            var spy = sinon.spy();318            this.clock.setInterval(function () {319                spy(new Date().getTime());320            }, 10);321            this.clock.tick(100);322            assert.equals(spy.callCount, 10);323            assert(spy.calledWith(10));324            assert(spy.calledWith(20));325            assert(spy.calledWith(30));326            assert(spy.calledWith(40));327            assert(spy.calledWith(50));328            assert(spy.calledWith(60));329            assert(spy.calledWith(70));330            assert(spy.calledWith(80));331            assert(spy.calledWith(90));332            assert(spy.calledWith(100));333        });334        it("fires timer in intervals of 13", function () {335            var spy = sinon.spy();336            this.clock.setInterval(spy, 13);337            this.clock.tick(500);338            assert.equals(spy.callCount, 38);339        });340        it("fires timers in correct order", function () {341            var spy13 = sinon.spy();342            var spy10 = sinon.spy();343            this.clock.setInterval(function () {344                spy13(new Date().getTime());345            }, 13);346            this.clock.setInterval(function () {347                spy10(new Date().getTime());348            }, 10);349            this.clock.tick(500);350            assert.equals(spy13.callCount, 38);351            assert.equals(spy10.callCount, 50);352            assert(spy13.calledWith(416));353            assert(spy10.calledWith(320));354            assert(spy10.getCall(0).calledBefore(spy13.getCall(0)));355            assert(spy10.getCall(4).calledBefore(spy13.getCall(3)));356        });357        it("triggers timeouts and intervals in the order scheduled", function () {358            var spies = [sinon.spy(), sinon.spy()];359            this.clock.setInterval(spies[0], 10);360            this.clock.setTimeout(spies[1], 50);361            this.clock.tick(100);362            assert(spies[0].calledBefore(spies[1]));363            assert.equals(spies[0].callCount, 10);364            assert.equals(spies[1].callCount, 1);365        });366        it("does not fire canceled intervals", function () {367            var id;368            var callback = sinon.spy(function () {369                if (callback.callCount === 3) {370                    clearInterval(id);371                }372            });373            id = this.clock.setInterval(callback, 10);374            this.clock.tick(100);375            assert.equals(callback.callCount, 3);376        });377        it("passes 6 seconds", function () {378            var spy = sinon.spy();379            this.clock.setInterval(spy, 4000);380            this.clock.tick("08");381            assert.equals(spy.callCount, 2);382        });383        it("passes 1 minute", function () {384            var spy = sinon.spy();385            this.clock.setInterval(spy, 6000);386            this.clock.tick("01:00");387            assert.equals(spy.callCount, 10);388        });389        it("passes 2 hours, 34 minutes and 12 seconds", function () {390            var spy = sinon.spy();391            this.clock.setInterval(spy, 10000);392            this.clock.tick("02:34:10");393            assert.equals(spy.callCount, 925);394        });395        it("throws for invalid format", function () {396            var spy = sinon.spy();397            this.clock.setInterval(spy, 10000);398            var test = this;399            assert.exception(function () {400                test.clock.tick("12:02:34:10");401            });402            assert.equals(spy.callCount, 0);403        });404        it("throws for invalid minutes", function () {405            var spy = sinon.spy();406            this.clock.setInterval(spy, 10000);407            var test = this;408            assert.exception(function () {409                test.clock.tick("67:10");410            });411            assert.equals(spy.callCount, 0);412        });413        it("throws for negative minutes", function () {414            var spy = sinon.spy();415            this.clock.setInterval(spy, 10000);416            var test = this;417            assert.exception(function () {418                test.clock.tick("-7:10");419            });420            assert.equals(spy.callCount, 0);421        });422        it("treats missing argument as 0", function () {423            this.clock.tick();424            assert.equals(this.clock.now, 0);425        });426        it("fires nested setTimeout calls properly", function () {427            var i = 0;428            var clock = this.clock;429            var callback = function () {430                ++i;431                clock.setTimeout(function () {432                    callback();433                }, 100);434            };435            callback();436            clock.tick(1000);437            assert.equals(i, 11);438        });439        it("does not silently catch errors", function () {440            var clock = this.clock;441            clock.setTimeout(function () {442                throw new Error("oh no!");443            }, 1000);444            assert.exception(function () {445                clock.tick(1000);446            });447        });448        it("returns the current now value", function () {449            var clock = this.clock;450            var value = clock.tick(200);451            assert.equals(clock.now, value);452        });453    });454    describe("clearTimeout", function () {455        beforeEach(function () {456            this.clock = lolex.createClock();457        });458        it("removes timeout", function () {459            var stub = sinon.stub();460            var id = this.clock.setTimeout(stub, 50);461            this.clock.clearTimeout(id);462            this.clock.tick(50);463            assert.isFalse(stub.called);464        });465        it("does not remove interval", function () {466            var stub = sinon.stub();467            var id = this.clock.setInterval(stub, 50);468            assert.exception(function() {469                this.clock.clearTimeout(id);470            });471            this.clock.tick(50);472            assert.isTrue(stub.called);473        });474        it("does not remove immediate", function () {475            var stub = sinon.stub();476            var id = this.clock.setImmediate(stub);477            assert.exception(function() {478                this.clock.clearTimeout(id);479            });480            this.clock.tick(50);481            assert.isTrue(stub.called);482        });483        it("ignores null argument", function () {484            this.clock.clearTimeout(null);485            assert(true); // doesn't fail486        });487    });488    describe("reset", function () {489        beforeEach(function () {490            this.clock = lolex.createClock();491        });492        it("empties timeouts queue", function () {493            var stub = sinon.stub();494            this.clock.setTimeout(stub);495            this.clock.reset();496            this.clock.tick(0);497            assert.isFalse(stub.called);498        });499    });500    describe("setInterval", function () {501        beforeEach(function () {502            this.clock = lolex.createClock();503        });504        it("throws if no arguments", function () {505            var clock = this.clock;506            assert.exception(function () {507                clock.setInterval();508            });509        });510        it("returns numeric id or object with numeric id", function () {511            var result = this.clock.setInterval("");512            if (typeof result === 'object') {513                assert.isNumber(result.id);514            } else {515                assert.isNumber(result);516            }517        });518        it("returns unique id", function () {519            var id1 = this.clock.setInterval("");520            var id2 = this.clock.setInterval("");521            refute.equals(id2, id1);522        });523        it("schedules recurring timeout", function () {524            var stub = sinon.stub();525            this.clock.setInterval(stub, 10);526            this.clock.tick(99);527            assert.equals(stub.callCount, 9);528        });529        it("is not influenced by forward system clock changes", function () {530            var stub = sinon.stub();531            this.clock.setInterval(stub, 10);532            this.clock.tick(11);533            assert.equals(stub.callCount, 1);534            this.clock.setSystemTime((new this.clock.Date()).getTime() + 1000);535            this.clock.tick(8);536            assert.equals(stub.callCount, 1);537            this.clock.tick(3);538            assert.equals(stub.callCount, 2);539        });540        it("is not influenced by backward system clock changes", function () {541            var stub = sinon.stub();542            this.clock.setInterval(stub, 10);543            this.clock.tick(5);544            this.clock.setSystemTime((new this.clock.Date()).getTime() - 1000);545            this.clock.tick(6);546            assert.equals(stub.callCount, 1);547            this.clock.tick(10);548            assert.equals(stub.callCount, 2);549        });550        it("does not schedule recurring timeout when cleared", function () {551            var clock = this.clock;552            var id;553            var stub = sinon.spy(function () {554                if (stub.callCount === 3) {555                    clock.clearInterval(id);556                }557            });558            id = this.clock.setInterval(stub, 10);559            this.clock.tick(100);560            assert.equals(stub.callCount, 3);561        });562        it("passes setTimeout parameters", function () {563            var clock = lolex.createClock();564            var stub = sinon.stub();565            clock.setInterval(stub, 2, "the first", "the second");566            clock.tick(3);567            assert.isTrue(stub.calledWithExactly("the first", "the second"));568        });569    });570    describe("clearInterval", function () {571        beforeEach(function () {572            this.clock = lolex.createClock();573        });574        it("removes interval", function () {575            var stub = sinon.stub();576            var id = this.clock.setInterval(stub, 50);577            this.clock.clearInterval(id);578            this.clock.tick(50);579            assert.isFalse(stub.called);580        });581        it("does not remove timeout", function () {582            var stub = sinon.stub();583            var id = this.clock.setTimeout(stub, 50);584            assert.exception(function() {585                this.clock.clearInterval(id);586            });587            this.clock.tick(50);588            assert.isTrue(stub.called);589        });590        it("does not remove immediate", function () {591            var stub = sinon.stub();592            var id = this.clock.setImmediate(stub);593            assert.exception(function() {594                this.clock.clearInterval(id);595            });596            this.clock.tick(50);597            assert.isTrue(stub.called);598        });599        it("ignores null argument", function () {600            this.clock.clearInterval(null);601            assert(true); // doesn't fail602        });603    });604    describe("date", function () {605        beforeEach(function () {606            this.now = new GlobalDate().getTime() - 3000;...Using AI Code Generation
1describe('Test', function() {2  before(function() {3    this.clock = sinon.useFakeTimers();4  });5  after(function() {6    this.clock.restore();7  });8  it('should do something', function() {9    this.clock.setImmediate(function() {10      console.log('Hello');11    });12    this.clock.tick(1);13  });14});15describe('Test', function() {16  before(function() {17    this.clock = sinon.useFakeTimers();18  });19  after(function() {20    this.clock.restore();21  });22  it('should do something', function() {23    setTimeout(function() {24      console.log('Hello');25    }, 0);26    this.clock.tick(1);27  });28});29describe('Test', function() {30  before(function() {31    this.clock = sinon.useFakeTimers();32  });33  after(function() {34    this.clock.restore();35  });36  it('should do something', function() {37    setTimeout(function() {38      console.log('Hello');39    }, 0);40    this.clock.tick(1);41  });42});43describe('Test', function() {44  it('should do something', function() {45    setTimeout(function() {46      console.log('Hello');47    }, 0);48  });49});50describe('Test', function() {51  it('should do something', function() {52    setTimeout(function() {53      console.log('Hello');54    }, 0);55  });56});57describe('Test', function() {58  it('should do something', function() {59    setTimeout(function() {60      console.log('Hello');61    }, 0);62  });63});64describe('Test', function() {65  it('should do something', function() {66    setTimeout(function() {67      console.log('Hello');68    }, 0);69  });70});71describe('Test', function() {72  it('should do somethingUsing AI Code Generation
1import { expect } from 'chai';2import sinon from 'sinon';3import { test } from './test';4describe('test', () => {5  beforeEach(function() {6    this.clock = sinon.useFakeTimers();7  });8  afterEach(function() {9    this.clock.restore();10  });11  it('should call the callback function after 1000 ms', () => {12    const callback = sinon.spy();13    test(callback);14    this.clock.tick(1000);15    expect(callback.called).to.equal(true);16  });17});18export function test(callback) {19  setTimeout(() => {20    callback();21  }, 1000);22}23The above code does not work as expected. The callback function is not called after 1000 ms. I have tried using this.clock.tick(1000) and this.clock.runAll() but none of them works. Can someone please help me out?24I have updated the code. Now it is using sinon's fake timers. But still the test does not pass. I have tried using both this.clock.tick(Using AI Code Generation
1this.clock.setImmediate(function() {2});3this.clock.tick(0);4this.clock.setImmediate(function() {5});6this.clock.tick(0);7this.clock.setImmediate(function() {8});9this.clock.tick(0);10this.clock.setImmediate(function() {11});12this.clock.tick(0);13this.clock.setImmediate(function() {14});15this.clock.tick(0);16this.clock.setImmediate(function() {17});18this.clock.tick(0);19this.clock.setImmediate(function() {20});21this.clock.tick(0);22this.clock.setImmediate(function() {23});24this.clock.tick(0);25this.clock.setImmediate(function() {26});27this.clock.tick(0);28this.clock.setImmediate(function() {29});30this.clock.tick(0);31this.clock.setImmediate(function() {32});33this.clock.tick(0);34this.clock.setImmediate(function() {35});36this.clock.tick(0);Using AI Code Generation
1describe("Test", function() {2  beforeEach(function() {3    this.clock = sinon.useFakeTimers();4  });5  afterEach(function() {6    this.clock.restore();7  });8  it("should call the callback", function() {9    var callback = sinon.spy();10    setImmediate(callback);11    this.clock.tick(1);12    sinon.assert.calledOnce(callback);13  });14  it("should call the callback with arguments", function() {15    var callback = sinon.spy();16    setImmediate(callback, 1, 2);17    this.clock.tick(1);18    sinon.assert.calledWith(callback, 1, 2);19  });20});21describe("Test", function() {22  beforeEach(function() {23    this.clock = sinon.useFakeTimers();24  });25  afterEach(function() {26    this.clock.restore();27  });28  it("should call the callback", function() {29    var callback = sinon.spy();30    setImmediate(callback);31    this.clock.tick(1);32    sinon.assert.calledOnce(callback);33  });34  it("should call the callback with arguments", function() {35    var callback = sinon.spy();36    setImmediate(callback, 1, 2);37    this.clock.tick(1);38    sinon.assert.calledWith(callback, 1, 2);39  });40});41describe("Test", function() {42  beforeEach(function() {43    this.clock = sinon.useFakeTimers();44  });45  afterEach(function() {46    this.clock.restore();47  });48  it("should call the callback", function() {49    var callback = sinon.spy();50    setImmediate(callback);51    this.clock.tick(1);52    sinon.assert.calledOnce(callback);53  });54  it("should call the callback with arguments", function() {55    var callback = sinon.spy();56    setImmediate(callback, 1, 2);57    this.clock.tick(1);58    sinon.assert.calledWith(callback, 1, 2);59  });60});61describe("Test", function() {62  beforeEach(function() {63    this.clock = sinon.useFakeTimers();64  });65  afterEach(function() {66    this.clock.restore();67  });68  it("should call the callback", function()Using AI Code Generation
1var clock = sinon.useFakeTimers();2clock.setImmediate(function() {3  console.log('executed immediately');4});5clock.tick(0);6process.nextTick(function() {7  console.log('executed immediately');8});9setImmediate(function() {10  console.log('executed immediately');11});12setTimeout(function() {13  console.log('executed immediately');14}, 0);Using AI Code Generation
1var clock = sinon.useFakeTimers();2var obj = {3  method: function () {}4};5var spy = sinon.spy(obj, 'method');6obj.method();7clock.tick(100);8assert(spy.called);9clock.restore();10var clock = sinon.useFakeTimers();11var obj = {12  method: function () {}13};14var spy = sinon.spy(obj, 'method');15obj.method();16clock.tick(100);17assert(spy.called);18clock.restore();19var clock = sinon.useFakeTimers();20var obj = {21  method: function () {}22};23var spy = sinon.spy(obj, 'method');24obj.method();25clock.tick(100);26assert(spy.called);27clock.restore();28var clock = sinon.useFakeTimers();29var obj = {30  method: function () {}31};32var spy = sinon.spy(obj, 'method');33obj.method();34clock.tick(100);35assert(spy.called);36clock.restore();37var clock = sinon.useFakeTimers();38var obj = {39  method: function () {}40};41var spy = sinon.spy(obj, 'method');42obj.method();43clock.tick(100);44assert(spy.called);45clock.restore();46var clock = sinon.useFakeTimers();47var obj = {48  method: function () {}49};50var spy = sinon.spy(obj, 'method');51obj.method();52clock.tick(100);53assert(spy.called);54clock.restore();55var clock = sinon.useFakeTimers();56var obj = {57  method: function () {}58};59var spy = sinon.spy(obj, 'method');60obj.method();61clock.tick(100);62assert(spy.called);63clock.restore();64var clock = sinon.useFakeTimers();65var obj = {66  method: function () {}67};68var spy = sinon.spy(obj, 'method');69obj.method();70clock.tick(100);71assert(spy.called);72clock.restore();Using AI Code Generation
1var app = require('../app');2var sinon = require('sinon');3var assert = require('assert');4describe('app.js', function() {5    describe('test', function() {6        beforeEach(function() {7            this.clock = sinon.useFakeTimers();8        });9        afterEach(function() {10            this.clock.restore();11        });12        it('test', function() {13            var callback = sinon.spy();14            app.test(callback);15            assert(callback.calledOnce);16            this.clock.tick(100);17            assert(callback.calledTwice);18        });19    });20});21var test = function(callback) {22    callback();23    setTimeout(function() {24        callback();25    }, 100);26};27exports.test = test;28I have also tried using sinon.useFakeTimers(100, 'setLearn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
