Best JavaScript code snippet using sinon
test-media-performance-metrics-service.js
Source:test-media-performance-metrics-service.js
...46 it('should record and flush metrics', () => {47 const flushStub = env.sandbox.stub(service.performanceService_, 'flush');48 const video = win.document.createElement('video');49 service.startMeasuring(video);50 clock.tick(20);51 video.dispatchEvent(new Event('playing'));52 clock.tick(100);53 video.dispatchEvent(new Event('waiting'));54 clock.tick(300);55 service.stopMeasuring(video);56 expect(tickStub).to.have.callCount(6);57 expect(flushStub).to.have.been.calledOnce;58 });59 it('should record and flush metrics on error', () => {60 const flushStub = env.sandbox.stub(service.performanceService_, 'flush');61 const video = win.document.createElement('video');62 service.startMeasuring(video);63 clock.tick(2000);64 video.dispatchEvent(new Event('error'));65 clock.tick(10000);66 service.stopMeasuring(video);67 expect(tickStub).to.have.callCount(2);68 expect(flushStub).to.have.been.calledOnce;69 });70 it('should record and flush metrics for multiple media', () => {71 const flushStub = env.sandbox.stub(service.performanceService_, 'flush');72 const video1 = win.document.createElement('video');73 const video2 = win.document.createElement('video');74 service.startMeasuring(video1);75 service.startMeasuring(video2);76 clock.tick(100);77 video1.dispatchEvent(new Event('playing'));78 clock.tick(200);79 service.stopMeasuring(video1);80 video2.dispatchEvent(new Event('waiting'));81 clock.tick(300);82 video2.dispatchEvent(new Event('playing'));83 service.stopMeasuring(video2);84 expect(tickStub).to.have.callCount(11);85 expect(flushStub).to.have.been.calledTwice;86 });87 it('should not flush metrics if sendMetrics is false', () => {88 const flushStub = env.sandbox.stub(service.performanceService_, 'flush');89 const video = win.document.createElement('video');90 service.startMeasuring(video);91 clock.tick(20);92 video.dispatchEvent(new Event('playing'));93 clock.tick(100);94 video.dispatchEvent(new Event('waiting'));95 clock.tick(300);96 service.stopMeasuring(video, false /** sendMetrics */);97 expect(flushStub).to.not.have.been.called;98 });99 describe('Joint latency', () => {100 it('should record joint latency if playback starts with no wait', () => {101 const video = win.document.createElement('video');102 service.startMeasuring(video);103 clock.tick(100);104 video.dispatchEvent(new Event('playing'));105 clock.tick(200);106 service.stopMeasuring(video);107 expect(tickStub).to.have.been.calledWithExactly('vjl', 100);108 });109 it('should record joint latency when waiting first', () => {110 const video = win.document.createElement('video');111 service.startMeasuring(video);112 clock.tick(100);113 video.dispatchEvent(new Event('waiting'));114 clock.tick(200);115 video.dispatchEvent(new Event('playing'));116 service.stopMeasuring(video);117 expect(tickStub).to.have.been.calledWithExactly('vjl', 300);118 });119 it('should not record joint latency if playback does not start', () => {120 const video = win.document.createElement('video');121 service.startMeasuring(video);122 clock.tick(100);123 video.dispatchEvent(new Event('waiting'));124 clock.tick(200);125 service.stopMeasuring(video);126 expect(tickStub).to.not.have.been.calledWith('vjl');127 });128 it('should record joint latency for multiple media', () => {129 const video1 = win.document.createElement('video');130 const video2 = win.document.createElement('video');131 service.startMeasuring(video1);132 service.startMeasuring(video2);133 clock.tick(100);134 video1.dispatchEvent(new Event('playing'));135 clock.tick(200);136 service.stopMeasuring(video1);137 video2.dispatchEvent(new Event('waiting'));138 clock.tick(300);139 video2.dispatchEvent(new Event('playing'));140 service.stopMeasuring(video2);141 expect(tickStub).to.have.been.calledWithExactly('vjl', 100);142 expect(tickStub).to.have.been.calledWithExactly('vjl', 600);143 });144 });145 describe('Watch time', () => {146 it('should record watch time', () => {147 const video = win.document.createElement('video');148 service.startMeasuring(video);149 clock.tick(100);150 video.dispatchEvent(new Event('playing'));151 clock.tick(200);152 service.stopMeasuring(video);153 expect(tickStub).to.have.been.calledWithExactly('vwt', 200);154 });155 it('should record watch time and handle pause events', () => {156 const video = win.document.createElement('video');157 service.startMeasuring(video);158 clock.tick(100);159 video.dispatchEvent(new Event('playing'));160 clock.tick(200);161 video.dispatchEvent(new Event('pause'));162 clock.tick(300);163 video.dispatchEvent(new Event('playing'));164 clock.tick(400);165 service.stopMeasuring(video);166 expect(tickStub).to.have.been.calledWithExactly('vwt', 600);167 });168 it('should record watch time and handle ended events', () => {169 const video = win.document.createElement('video');170 service.startMeasuring(video);171 clock.tick(100);172 video.dispatchEvent(new Event('playing'));173 clock.tick(200);174 video.dispatchEvent(new Event('ended'));175 clock.tick(300);176 video.dispatchEvent(new Event('playing'));177 clock.tick(400);178 service.stopMeasuring(video);179 expect(tickStub).to.have.been.calledWithExactly('vwt', 600);180 });181 it('should record watch time and handle rebuffers', () => {182 const video = win.document.createElement('video');183 service.startMeasuring(video);184 clock.tick(100);185 video.dispatchEvent(new Event('playing'));186 clock.tick(200);187 video.dispatchEvent(new Event('waiting'));188 clock.tick(300);189 video.dispatchEvent(new Event('playing'));190 clock.tick(400);191 service.stopMeasuring(video);192 expect(tickStub).to.have.been.calledWithExactly('vwt', 600);193 });194 });195 describe('Rebuffers', () => {196 it('should count rebuffers', () => {197 const video = win.document.createElement('video');198 service.startMeasuring(video);199 clock.tick(100);200 video.dispatchEvent(new Event('playing'));201 clock.tick(200);202 video.dispatchEvent(new Event('waiting'));203 clock.tick(300);204 video.dispatchEvent(new Event('playing'));205 clock.tick(400);206 video.dispatchEvent(new Event('waiting'));207 clock.tick(500);208 service.stopMeasuring(video);209 expect(tickStub).to.have.been.calledWithExactly('vrb', 2);210 });211 it('should record rebuffer rate', () => {212 const video = win.document.createElement('video');213 service.startMeasuring(video);214 clock.tick(100);215 video.dispatchEvent(new Event('playing'));216 clock.tick(200);217 video.dispatchEvent(new Event('waiting'));218 clock.tick(300);219 video.dispatchEvent(new Event('playing'));220 clock.tick(400);221 video.dispatchEvent(new Event('waiting'));222 clock.tick(500);223 service.stopMeasuring(video);224 // playing: 600; waiting: 800225 // (800 / (600 + 800)) * 100 ~= 57226 expect(tickStub).to.have.been.calledWithExactly('vrbr', 57);227 });228 it('should record mean time between rebuffers', () => {229 const video = win.document.createElement('video');230 service.startMeasuring(video);231 clock.tick(100);232 video.dispatchEvent(new Event('playing'));233 clock.tick(200);234 video.dispatchEvent(new Event('waiting'));235 clock.tick(300);236 video.dispatchEvent(new Event('playing'));237 clock.tick(400);238 video.dispatchEvent(new Event('waiting'));239 clock.tick(500);240 service.stopMeasuring(video);241 // 600ms playing divided by 2 rebuffer events.242 expect(tickStub).to.have.been.calledWithExactly('vmtbrb', 300);243 });244 it('should count the initial buffering as a rebuffer', () => {245 const video = win.document.createElement('video');246 service.startMeasuring(video);247 clock.tick(300);248 video.dispatchEvent(new Event('waiting'));249 clock.tick(400);250 video.dispatchEvent(new Event('playing'));251 clock.tick(500);252 video.dispatchEvent(new Event('waiting'));253 clock.tick(600);254 video.dispatchEvent(new Event('playing'));255 clock.tick(700);256 service.stopMeasuring(video);257 expect(tickStub).to.have.been.calledWithExactly('vrb', 2);258 });259 it('should exclude very brief rebuffers', () => {260 const video = win.document.createElement('video');261 service.startMeasuring(video);262 clock.tick(100);263 video.dispatchEvent(new Event('playing'));264 clock.tick(200);265 video.dispatchEvent(new Event('waiting'));266 clock.tick(10);267 video.dispatchEvent(new Event('playing'));268 clock.tick(300);269 service.stopMeasuring(video);270 expect(tickStub).to.have.been.calledWithExactly('vrb', 0);271 });272 it('should record rebuffer rate even with no rebuffer events', () => {273 const video = win.document.createElement('video');274 service.startMeasuring(video);275 clock.tick(100);276 video.dispatchEvent(new Event('playing'));277 clock.tick(200);278 service.stopMeasuring(video);279 expect(tickStub).to.have.been.calledWithExactly('vrbr', 0);280 });281 it('should not send mean time between rebuffers when no rebuffer', () => {282 const video = win.document.createElement('video');283 service.startMeasuring(video);284 clock.tick(100);285 video.dispatchEvent(new Event('playing'));286 clock.tick(200);287 service.stopMeasuring(video);288 expect(tickStub).to.not.have.been.calledWith('vmtbrb');289 });290 });291 describe('Errors', () => {292 it('should detect the video as already errored', (done) => {293 const video = win.document.createElement('video');294 video.onerror = () => {295 service.startMeasuring(video);296 service.stopMeasuring(video);297 expect(tickStub).to.have.been.calledWithExactly('verr', 4);298 done();299 };300 // MediaError.code = 4 (MEDIA_ERR_SRC_NOT_SUPPORTED)301 video.src = '404.mp4';302 });303 it('should detect the video as already errored from <source>', () => {304 const video = win.document.createElement('video');305 video[MEDIA_LOAD_FAILURE_SRC_PROPERTY] = '';306 service.startMeasuring(video);307 service.stopMeasuring(video);308 expect(tickStub).to.have.been.calledWithExactly('verr', 0);309 });310 it('should detect that the video errors', () => {311 const video = win.document.createElement('video');312 service.startMeasuring(video);313 clock.tick(100);314 video.dispatchEvent(new Event('playing'));315 clock.tick(200);316 video.dispatchEvent(new Event('error'));317 clock.tick(300);318 service.stopMeasuring(video);319 expect(tickStub).to.have.been.calledWithExactly('verr', 0);320 });321 });322 describe('Cache state', () => {323 it('should register the video as playing from origin', () => {324 const video = win.document.createElement('video');325 const source = win.document.createElement('source');326 source.setAttribute('src', 'foo.mp4');327 video.appendChild(source);328 env.sandbox.stub(video, 'currentSrc').value('foo.mp4');329 service.startMeasuring(video);330 service.stopMeasuring(video);331 expect(tickStub).to.have.been.calledWithExactly('vcs', 0);...
test-poller.js
Source:test-poller.js
...51 it('should execute work', () => {52 workStub.returns(Promise.resolve());53 expect(workStub).to.have.not.been.called;54 poller.start();55 clock.tick(4000);56 expect(workStub).to.be.calledOnce;57 return poller.lastWorkPromise_58 .then(() => {59 clock.tick(2000);60 expect(workStub).to.be.calledOnce;61 clock.tick(2000);62 expect(workStub).to.have.callCount(2);63 poller.stop();64 })65 .then(() => {66 clock.tick(8000);67 expect(workStub).to.have.callCount(2);68 });69 });70 it('should execute work w/o initial delay', () => {71 workStub.returns(Promise.resolve());72 expect(workStub).to.have.not.been.called;73 poller.start(true);74 expect(workStub).to.be.calledOnce;75 return poller.lastWorkPromise_76 .then(() => {77 expect(workStub).to.be.calledOnce;78 clock.tick(4000);79 expect(workStub).to.have.callCount(2);80 return poller.lastWorkPromise_.then(() => {81 expect(workStub).to.have.callCount(2);82 clock.tick(4000);83 expect(workStub).to.have.callCount(3);84 poller.stop();85 });86 })87 .then(() => {88 clock.tick(8000);89 expect(workStub).to.have.callCount(3);90 });91 });92 it('should not double any work if already started', () => {93 workStub.returns(Promise.resolve());94 expect(workStub).to.have.not.been.called;95 poller.start();96 clock.tick(4000);97 expect(workStub).to.be.calledOnce;98 poller.start();99 return poller.lastWorkPromise_100 .then(() => {101 expect(workStub).to.be.calledOnce;102 poller.start();103 clock.tick(4000);104 expect(workStub).to.have.callCount(2);105 return poller.lastWorkPromise_;106 })107 .then(() => {108 expect(workStub).to.have.callCount(2);109 });110 });111 it('should run backoff and recover on retriable error', () => {112 const retriableErr = new Error('HTTP Error');113 retriableErr.retriable = true;114 workStub.onCall(0).returns(Promise.resolve());115 workStub.onCall(1).returns(Promise.resolve());116 workStub.onCall(2).returns(Promise.reject(retriableErr));117 workStub.onCall(3).returns(Promise.reject(retriableErr));118 workStub.onCall(4).returns(Promise.reject(retriableErr));119 workStub.returns(Promise.resolve());120 expect(workStub).to.have.not.been.called;121 poller.start();122 clock.tick(4000);123 expect(workStub).to.be.calledOnce;124 return poller.lastWorkPromise_125 .then(() => {126 expect(workStub).to.be.calledOnce;127 clock.tick(4000);128 expect(workStub).to.have.callCount(2);129 return poller.lastWorkPromise_;130 })131 .then(() => {132 expect(workStub).to.have.callCount(2);133 clock.tick(4000);134 expect(workStub).to.have.callCount(3);135 return poller.lastWorkPromise_;136 })137 .then(() => {138 expect(workStub).to.have.callCount(3);139 clock.tick(700);140 expect(workStub).to.have.callCount(4);141 return poller.lastWorkPromise_;142 })143 .then(() => {144 expect(workStub).to.have.callCount(4);145 clock.tick(700);146 expect(workStub).to.have.callCount(4);147 clock.tick(700);148 expect(workStub).to.have.callCount(5);149 return poller.lastWorkPromise_;150 })151 .then(() => {152 expect(workStub).to.have.callCount(5);153 clock.tick(2800);154 expect(workStub).to.have.callCount(6);155 return poller.lastWorkPromise_;156 })157 .then(() => {158 expect(workStub).to.have.callCount(6);159 clock.tick(4000);160 expect(workStub).to.have.callCount(7);161 return poller.lastWorkPromise_;162 })163 .then(() => {164 expect(workStub).to.have.callCount(7);165 clock.tick(4000);166 expect(workStub).to.have.callCount(8);167 });168 });169 it('should stop work if stopped', () => {170 workStub.returns(Promise.resolve());171 expect(workStub).to.have.not.been.called;172 poller.start();173 clock.tick(4000);174 expect(workStub).to.be.calledOnce;175 return poller.lastWorkPromise_176 .then(() => {177 clock.tick(4000);178 expect(workStub).to.have.callCount(2);179 poller.stop();180 return poller.lastWorkPromise_;181 })182 .then(() => {183 clock.tick(4000);184 expect(workStub).to.have.callCount(2);185 return poller.lastWorkPromise_;186 })187 .then(() => {188 clock.tick(4000);189 expect(workStub).to.have.callCount(2);190 return poller.lastWorkPromise_;191 });192 });193 it('should shutoff backoff if stopped', () => {194 const retriableErr = new Error('HTTP Error');195 retriableErr.retriable = true;196 workStub.returns(Promise.reject(retriableErr));197 expect(workStub).to.have.not.been.called;198 poller.start();199 clock.tick(4000);200 expect(workStub).to.be.calledOnce;201 return poller.lastWorkPromise_202 .then(() => {203 expect(workStub).to.be.calledOnce;204 clock.tick(700);205 expect(workStub).to.have.callCount(2);206 return poller.lastWorkPromise_;207 })208 .then(() => {209 expect(workStub).to.have.callCount(2);210 clock.tick(1400);211 expect(workStub).to.have.callCount(3);212 return poller.lastWorkPromise_;213 })214 .then(() => {215 poller.stop();216 clock.tick(2800);217 expect(workStub).to.have.callCount(3);218 return poller.lastWorkPromise_;219 })220 .then(() => {221 clock.tick(5600);222 expect(workStub).to.have.callCount(3);223 return poller.lastWorkPromise_;224 })225 .then(() => {226 clock.tick(11200);227 expect(workStub).to.have.callCount(3);228 return poller.lastWorkPromise_;229 });230 });231 it('should clear timeout ids if stopped', () => {232 const delaySpy = window.sandbox.spy(timer, 'delay');233 const retriableErr = new Error('HTTP Error');234 retriableErr.retriable = true;235 workStub.onCall(0).returns(Promise.reject(retriableErr));236 workStub.onCall(1).returns(Promise.reject(retriableErr));237 workStub.returns(Promise.resolve());238 const clearSpy = window.sandbox.spy(timer, 'cancel');239 expect(poller.lastTimeoutId_).to.be.null;240 poller.start();241 expect(delaySpy.lastCall.args[1]).to.equal(4000);242 clock.tick(4000);243 expect(poller.lastTimeoutId_).to.be.a('number');244 let {lastTimeoutId_} = poller;245 // Reject 1246 return poller.lastWorkPromise_.then(() => {247 expect(delaySpy.lastCall.args[1]).to.equal(700);248 expect(poller.lastTimeoutId_).to.not.equal(lastTimeoutId_);249 expect(poller.lastTimeoutId_).to.be.a('number');250 lastTimeoutId_ = poller.lastTimeoutId_;251 clock.tick(700);252 // Reject 2253 return poller.lastWorkPromise_.then(() => {254 expect(delaySpy.lastCall.args[1]).to.equal(1400);255 // Should have cancelled next queued exponential tick256 expect(poller.lastTimeoutId_).to.not.equal(lastTimeoutId_);257 expect(poller.lastTimeoutId_).to.be.a('number');258 lastTimeoutId_ = poller.lastTimeoutId_;259 clock.tick(1400);260 return poller.lastWorkPromise_.then(() => {261 expect(delaySpy.lastCall.args[1]).to.equal(4000);262 expect(clearSpy.getCall(2)).to.be.null;263 expect(poller.lastTimeoutId_).to.not.equal(lastTimeoutId_);264 expect(poller.lastTimeoutId_).to.be.a('number');265 lastTimeoutId_ = poller.lastTimeoutId_;266 expect(clearSpy).to.have.not.been.called;267 poller.stop();268 expect(clearSpy).to.be.calledOnce;269 expect(clearSpy.getCall(0).args[0]).to.equal(lastTimeoutId_);270 });271 });272 });273 });...
test-rate-limit.js
Source:test-rate-limit.js
...25 const throttledCallback = throttle(window, callback, 100);26 throttledCallback(1);27 expect(callback).to.be.calledWith(1); // let 1st call through immediately28 callback.resetHistory();29 clock.tick(20);30 throttledCallback(2);31 clock.tick(20);32 throttledCallback(3);33 clock.tick(20);34 throttledCallback(4);35 clock.tick(20);36 throttledCallback(5);37 clock.tick(19);38 expect(callback).not.to.be.called; // not 100ms yet39 clock.tick(1);40 expect(callback).to.be.calledOnce;41 expect(callback).to.be.calledWith(5);42 callback.resetHistory();43 clock.tick(10);44 throttledCallback(6);45 expect(callback).not.to.be.called;46 clock.tick(89);47 throttledCallback(7, 'another param');48 expect(callback).not.to.be.called;49 clock.tick(1);50 expect(callback).to.be.calledOnce;51 expect(callback).to.be.calledWith(7, 'another param');52 });53 it('should throttle recursive callback', () => {54 let totalCalls = 0;55 function recursive(countdown) {56 totalCalls++;57 if (countdown > 0) {58 throttledCallback(countdown - 1);59 }60 }61 const throttledCallback = throttle(window, recursive, 100);62 // recursive 3 times63 throttledCallback(3);64 // should immediately invoke callback only once.65 expect(totalCalls).to.equal(1);66 // 2nd invocation happen after the min interval67 clock.tick(100);68 expect(totalCalls).to.equal(2);69 // 3rd invocation70 clock.tick(100);71 expect(totalCalls).to.equal(3);72 });73 });74 describe('debounce', () => {75 let clock;76 beforeEach(() => {77 clock = window.sandbox.useFakeTimers();78 });79 it('should wait before calling', () => {80 const callback = window.sandbox.spy();81 const debounced = debounce(window, callback, 100);82 debounced(1);83 expect(callback).to.not.have.been.called;84 clock.tick(100);85 expect(callback).to.have.been.calledWith(1);86 callback.resetHistory();87 debounced(1);88 expect(callback).to.not.have.been.called;89 debounced(2);90 expect(callback).to.not.have.been.called;91 clock.tick(10);92 debounced(3);93 expect(callback).to.not.have.been.called;94 clock.tick(99);95 expect(callback).to.not.have.been.called;96 clock.tick(1);97 expect(callback).to.have.been.calledWith(3);98 });99 it('should debounce recursive callback', () => {100 let totalCalls = 0;101 function recursive(countdown) {102 totalCalls++;103 if (countdown > 0) {104 debounced(countdown - 1);105 }106 }107 const debounced = debounce(window, recursive, 100);108 // recursive 3 times109 debounced(2);110 expect(totalCalls).to.equal(0);111 // 1st invocation happen after the min interval112 clock.tick(100);113 expect(totalCalls).to.equal(1);114 // 2nd invocation115 clock.tick(100);116 expect(totalCalls).to.equal(2);117 });118 });...
Using AI Code Generation
1import {expect} from 'chai';2import sinon from 'sinon';3import {foo} from './foo';4describe('foo', () => {5 it('should call setTimeout', () => {6 const clock = sinon.useFakeTimers();7 const callback = sinon.spy();8 foo(callback);9 clock.tick(200);10 expect(callback.called).to.equal(true);11 clock.restore();12 });13});14export const foo = (callback) => {15 setTimeout(() => {16 callback();17 }, 100);18};19import {expect} from 'chai';20import sinon from 'sinon';21import setTimeoutPromise from 'set-timeout-promise';22import {foo} from './foo';23describe('foo', () => {24 it('should call setTimeout', async () => {25 const clock = sinon.useFakeTimers();26 const callback = sinon.spy();27 foo(callback);28 await setTimeoutPromise(100);29 clock.tick(100);30 expect(callback.called).to.equal(true);31 clock.restore();32 });33});34import {expect} from 'chai';35import sinon from 'sinon';36import {foo} from './foo';37describe('foo', () => {38 it('should call setTimeout', () => {39 const clock = sinon.useFakeTimers();40 const callback = sinon.spy();41 const mock = sinon.mock(global);42 mock.expects('setTimeout').once().callsArg(0);43 foo(callback);44 clock.tick(100);45 expect(callback.called).to.equal(true);46 clock.restore();
Using AI Code Generation
1var clock = sinon.useFakeTimers();2clock.tick(1000);3clock.restore();4var clock = sinon.useFakeTimers();5clock.tick(1000);6clock.restore();7var clock = sinon.useFakeTimers();8clock.tick(1000);9clock.restore();10var clock = sinon.useFakeTimers();11clock.tick(1000);12clock.restore();13var clock = sinon.useFakeTimers();14clock.tick(1000);15clock.restore();16var clock = sinon.useFakeTimers();17clock.tick(1000);18clock.restore();19var clock = sinon.useFakeTimers();20clock.tick(1000);21clock.restore();22var clock = sinon.useFakeTimers();23clock.tick(1000);24clock.restore();25var clock = sinon.useFakeTimers();26clock.tick(1000);27clock.restore();28var clock = sinon.useFakeTimers();29clock.tick(1000);30clock.restore();31var clock = sinon.useFakeTimers();32clock.tick(1000);33clock.restore();34var clock = sinon.useFakeTimers();35clock.tick(1000);36clock.restore();37var clock = sinon.useFakeTimers();38clock.tick(1000);39clock.restore();40var clock = sinon.useFakeTimers();41clock.tick(1000);42clock.restore();43var clock = sinon.useFakeTimers();44clock.tick(1000);45clock.restore();
Using AI Code Generation
1var sinon = require('sinon');2var sinonChai = require('sinon-chai');3var chai = require('chai');4var expect = chai.expect;5chai.use(sinonChai);6var clock = sinon.useFakeTimers();7describe('Test', function() {8 it('should test', function() {9 var spy = sinon.spy();10 setTimeout(spy, 100);11 clock.tick(100);12 expect(spy).to.have.been.called;13 });14});15var clock = sinon.useFakeTimers();16clock.tick(100);17clock.restore();18var sinon = require('sinon');19var sinonChai = require('sinon-chai');20var chai = require('chai');21var expect = chai.expect;22chai.use(sinonChai);23describe('Test', function() {24 it('should test', function() {25 var spy = sinon.spy();26 setTimeout(spy, 100);27 clock.tick(100);28 expect(spy).to.have.been.called;29 });30});
Using AI Code Generation
1var sinon = require('sinon');2var clock = sinon.useFakeTimers();3var assert = require('assert');4var foo = { bar: 0 };5setTimeout(function() {6 foo.bar = 1;7}, 1000);8clock.tick(1000);9assert.equal(foo.bar, 1);10clock.restore();11var sinon = require('sinon');12var server = sinon.fakeServer.create();13server.respondWith("GET", "/some/url", [14 { "Content-Type": "application/json" },15 '{"foo": "bar"}'16]);17server.respond();18console.log(server.requests[0].url);19console.log(server.requests[0].method);20console.log(server.requests[0].requestBody);21console.log(server.requests[0].requestHeaders);22console.log(server.requests[0].async);23console.log(server.requests[0].username);24console.log(server.requests[0].password);25server.restore();26var sinon = require('sinon');27var server = sinon.fakeServer.create();28server.respondWith("GET", "/some/url", [29 { "Content-Type": "application/json" },30 '{"foo": "bar"}'31]);32server.respond();33console.log(server.requests[0].url);34console.log(server.requests[0].method);35console.log(server.requests[0].requestBody);36console.log(server.requests[0].requestHeaders);37console.log(server.requests[0].async);38console.log(server.requests[0].username);39console.log(server.requests[0].password);40server.restore();
Using AI Code Generation
1var sinon = require('sinon');2var clock = sinon.useFakeTimers();3var assert = require('assert');4var myObj = require('./myObj');5describe('myObj', function() {6 it('should call the callback after 1000 ms', function() {7 var callback = sinon.spy();8 myObj.method(callback);9 clock.tick(1000);10 assert(callback.called);11 });12});13var myObj = {14 method: function(callback) {15 setTimeout(callback, 1000);16 }17};18module.exports = myObj;19it('should return a promise', function() {20 var promise = new Promise(function(resolve, reject) {21 reject('error');22 });23 var stub = sinon.stub(myObj, 'myMethod').returns(promise);24 var result = myObj.myMethod();25 assert(result instanceof Promise);26});27it('should return a promise', function() {28 var promise = new Promise(function(resolve, reject) {29 reject('error');30 });31 var stub = sinon.stub(myObj, 'myMethod').returns(promise);32 var result = myObj.myMethod();33 assert(result instanceof Promise);34});35it('should return a promise', function() {36 var promise = new Promise(function(resolve, reject) {37 reject('error');38 });39 var stub = sinon.stub(myObj, 'myMethod').returns
Using AI Code Generation
1setTimeout(function() {2 console.log("Hello World");3}, 1000);4setInterval(function() {5 console.log("Hello World");6}, 1000);7var timeoutId = setTimeout(function() {8 console.log("Hello World");9}, 1000);10clearTimeout(timeoutId);11var intervalId = setInterval(function() {12 console.log("Hello World");13}, 1000);14clearInterval(intervalId);15process.nextTick(function() {16 console.log("Hello World");17});18process.exit(0);19process.kill(12345);20process.abort();
Learn 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!!