Best JavaScript code snippet using sinon
sinon-1.17.js
Source:sinon-1.17.js
1/*2 * Copyright 2016 The Closure Compiler Authors.3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16/**17 * @fileoverview Externs definitions for the Sinon library, 1.17 branch.18 *19 * Note that this file is incomplete.20 *21 * This file defines some virtual types, please don't use these directly, but22 * follow the official API guidelines.23 *24 * @externs25 * @see http://sinonjs.org/docs/26 */27/** @const */28var sinon = {};29/**30 * @constructor31 * @implements {EventListener}32 * @extends {Function}33 */34var SinonSpyCallApi = function() {};35/**36 * NOTE: This function doesn't exist on the sinon spy, it exists to be able to37 * satisfy the EventListener interface so that the spy can be used with38 * addEventListener calls.39 *40 * @param {!Event} event41 * @override42 * @deprecated This function doesn't exist on the sinon spy.43 */44SinonSpyCallApi.prototype.handleEvent = function(event) {};45/** @type {*} */46SinonSpyCallApi.prototype.thisValue;47/** @type {!Array<*>} */48SinonSpyCallApi.prototype.args;49/** @type {*} */50SinonSpyCallApi.prototype.exception;51/** @type {*} */52SinonSpyCallApi.prototype.returnValue;53/**54 * @param {*} obj55 * @return {boolean}56 */57SinonSpyCallApi.prototype.calledOn = function(obj) {};58/**59 * @param {...*} args60 * @return {boolean}61 */62SinonSpyCallApi.prototype.calledWith = function(args) {};63/**64 * @param {...*} args65 * @return {boolean}66 */67SinonSpyCallApi.prototype.calledWithExactly = function(args) {};68/**69 * @param {...*} args70 * @return {boolean}71 */72SinonSpyCallApi.prototype.calledWithMatch = function(args) {};73/**74 * @param {...*} args75 * @return {boolean}76 */77SinonSpyCallApi.prototype.notCalledWith = function(args) {};78/**79 * @param {...*} args80 * @return {boolean}81 */82SinonSpyCallApi.prototype.notCalledWithMatch = function(args) {};83/**84 * @param {*} value85 * @return {boolean}86 */87SinonSpyCallApi.prototype.returned = function(value) {};88/**89 * @param {*=} obj90 * @return {boolean}91 */92SinonSpyCallApi.prototype.threw = function(obj) {};93/**94 * @param {number} pos95 */96SinonSpyCallApi.prototype.callArg = function(pos) {};97/**98 * @param {number} pos99 * @param {*} obj100 * @param {...*} args101 */102SinonSpyCallApi.prototype.callArgOn = function(pos, obj, args) {};103/**104 * @param {number} pos105 * @param {...*} args106 */107SinonSpyCallApi.prototype.callArgWith = function(pos, args) {};108/**109 * @param {number} pos110 * @param {*} obj111 * @param {...*} args112 */113SinonSpyCallApi.prototype.callArgOnWith = function(pos, obj, args) {};114/**115 * @param {...*} args116 */117SinonSpyCallApi.prototype.yield = function(args) {};118/**119 * @param {*} obj120 * @param {...*} args121 */122SinonSpyCallApi.prototype.yieldOn = function(obj, args) {};123/**124 * @param {string} property125 * @param {...*} args126 */127SinonSpyCallApi.prototype.yieldTo = function(property, args) {};128/**129 * @param {string} property130 * @param {*} obj131 * @param {...*} args132 */133SinonSpyCallApi.prototype.yieldToOn = function(property, obj, args) {};134/**135 * @constructor136 * @extends {SinonSpyCallApi}137 */138var SinonSpyCall = function() {};139/**140 * @param {!SinonSpyCall} call141 * @return {boolean}142 */143SinonSpyCall.prototype.calledBefore = function(call) {};144/**145 * @param {!SinonSpyCall} call146 * @return {boolean}147 */148SinonSpyCall.prototype.calledAfter = function(call) {};149/**150 * @param {!SinonSpyCall} call151 * @return {boolean}152 */153SinonSpyCall.prototype.calledWithNew = function(call) {};154/**155 * @constructor156 * @extends {SinonSpyCallApi}157 */158var SinonSpy = function() {};159/** @type {number} */160SinonSpy.prototype.callCount;161/** @type {boolean} */162SinonSpy.prototype.called;163/** @type {boolean} */164SinonSpy.prototype.notCalled;165/** @type {boolean} */166SinonSpy.prototype.calledOnce;167/** @type {boolean} */168SinonSpy.prototype.calledTwice;169/** @type {boolean} */170SinonSpy.prototype.calledThrice;171/** @type {!SinonSpyCall} */172SinonSpy.prototype.firstCall;173/** @type {!SinonSpyCall} */174SinonSpy.prototype.secondCall;175/** @type {!SinonSpyCall} */176SinonSpy.prototype.thirdCall;177/** @type {!SinonSpyCall} */178SinonSpy.prototype.lastCall;179/** @type {!Array<*>} */180SinonSpy.prototype.thisValues;181/** @type {!Array<!Array<*>>} */182SinonSpy.prototype.args;183/** @type {!Array<*>} */184SinonSpy.prototype.exceptions;185/** @type {!Array<*>} */186SinonSpy.prototype.returnValues;187/**188 * @param {!SinonSpy} anotherSpy189 * @return {boolean}190 */191SinonSpy.prototype.calledBefore = function(anotherSpy) {};192/**193 * @param {!SinonSpy} anotherSpy194 * @return {boolean}195 */196SinonSpy.prototype.calledAfter = function(anotherSpy) {};197/**198 * @return {boolean}199 */200SinonSpy.prototype.calledWithNew = function() {};201/**202 * @param {...*} args203 * @return {!SinonSpy}204 */205SinonSpy.prototype.withArgs = function(args) {};206/**207 * @param {*} obj208 * @return {boolean}209 */210SinonSpy.prototype.alwaysCalledOn = function(obj) {};211/**212 * @param {...*} args213 * @return {boolean}214 */215SinonSpy.prototype.alwaysCalledWith = function(args) {};216/**217 * @param {...*} args218 * @return {boolean}219 */220SinonSpy.prototype.alwaysCalledWithExactly = function(args) {};221/**222 * @param {...*} args223 * @return {boolean}224 */225SinonSpy.prototype.alwaysCalledWithMatch = function(args) {};226/**227 * @param {...*} args228 * @return {boolean}229 */230SinonSpy.prototype.neverCalledWith = function(args) {};231/**232 * @param {...*} args233 * @return {boolean}234 */235SinonSpy.prototype.neverCalledWithMatch = function(args) {};236/**237 * @param {*=} obj238 * @return {boolean}239 */240SinonSpy.prototype.alwaysThrew = function(obj) {};241/**242 * @param {*} obj243 * @return {boolean}244 */245SinonSpy.prototype.alwaysReturned = function(obj) {};246/**247 * @param {...*} args248 */249SinonSpy.prototype.invokeCallback = function(args) {};250/**251 * @param {number} n252 * @return {!SinonSpyCall}253 */254SinonSpy.prototype.getCall = function(n) {};255/**256 * @return {!Array<!SinonSpyCall>}257 */258SinonSpy.prototype.getCalls = function() {};259SinonSpy.prototype.reset = function() {};260/**261 * @param {string} format262 * @param {...*} args263 * @return {string}264 */265SinonSpy.prototype.printf = function(format, args) {};266SinonSpy.prototype.restore = function() {};267/**268 * @param {*=} objectOrFunction269 * @param {string=} method270 * @return {!SinonSpy}271 */272sinon.spy = function(objectOrFunction, method) {};273/**274 * @constructor275 * @extends {SinonSpy}276 */277var SinonStub = function() {};278SinonStub.prototype.resetBehavior = function() {};279/**280 * @param {*} obj281 * @return {!SinonStub}282 */283SinonStub.prototype.returns = function(obj) {};284/**285 * @param {number} index286 * @return {!SinonStub}287 */288SinonStub.prototype.returnsArg = function(index) {};289/**290 * @return {!SinonStub}291 */292SinonStub.prototype.returnsThis = function() {};293/**294 * @param {*=} obj295 * @return {!SinonStub}296 */297SinonStub.prototype.throws = function(obj) {};298/**299 * @param {number} index300 * @return {!SinonStub}301 */302SinonStub.prototype.callsArg = function(index) {};303/**304 * @param {number} index305 * @param {*} context306 * @return {!SinonStub}307 */308SinonStub.prototype.callsArgOn = function(index, context) {};309/**310 * @param {number} index311 * @param {...*} args312 * @return {!SinonStub}313 */314SinonStub.prototype.callsArgWith = function(index, args) {};315/**316 * @param {number} index317 * @param {*} context318 * @param {...*} args319 * @return {!SinonStub}320 */321SinonStub.prototype.callsArgOnWith = function(index, context, args) {};322/**323 * @param {number} index324 * @return {!SinonStub}325 */326SinonStub.prototype.callsArgAsync = function(index) {};327/**328 * @param {number} index329 * @param {*} context330 * @return {!SinonStub}331 */332SinonStub.prototype.callsArgOnAsync = function(index, context) {};333/**334 * @param {number} index335 * @param {...*} args336 * @return {!SinonStub}337 */338SinonStub.prototype.callsArgWithAsync = function(index, args) {};339/**340 * @param {number} index341 * @param {*} context342 * @param {...*} args343 * @return {!SinonStub}344 */345SinonStub.prototype.callsArgOnWithAsync = function(index, context, args) {};346/**347 * @param {number} n348 * @return {!SinonStub}349 */350SinonStub.prototype.onCall = function(n) {};351/**352 * @return {!SinonStub}353 */354SinonStub.prototype.onFirstCall = function() {};355/**356 * @return {!SinonStub}357 */358SinonStub.prototype.onSecondCall = function() {};359/**360 * @return {!SinonStub}361 */362SinonStub.prototype.onThirdCall = function() {};363/**364 * @param {...*} args365 * @return {!SinonStub}366 */367SinonStub.prototype.yields = function(args) {};368/**369 * @param {*} context370 * @param {...*} args371 * @return {!SinonStub}372 */373SinonStub.prototype.yieldsOn = function(context, args) {};374/**375 * @param {string} property376 * @param {...*} args377 * @return {!SinonStub}378 */379SinonStub.prototype.yieldsTo = function(property, args) {};380/**381 * @param {string} property382 * @param {*} context383 * @param {...*} args384 * @return {!SinonStub}385 */386SinonStub.prototype.yieldsToOn = function(property, context, args) {};387/**388 * @param {...*} args389 * @return {!SinonStub}390 */391SinonStub.prototype.yieldsAsync = function(args) {};392/**393 * @param {*} context394 * @param {...*} args395 * @return {!SinonStub}396 */397SinonStub.prototype.yieldsOnAsync = function(context, args) {};398/**399 * @param {string} property400 * @param {...*} args401 * @return {!SinonStub}402 */403SinonStub.prototype.yieldsToAsync = function(property, args) {};404/**405 * @param {string} property406 * @param {*} context407 * @param {...*} args408 * @return {!SinonStub}409 */410SinonStub.prototype.yieldsToOnAsync = function(property, context, args) {};411/**412 * @param {...*} args413 * @return {!SinonStub}414 * @override415 */416SinonStub.prototype.withArgs = function(args) {};417/**418 * @param {*=} obj419 * @param {string=} method420 * @param {*=} func421 * @return {!SinonStub}422 */423sinon.stub = function(obj, method, func) {};424/**425 * @constructor426 * @extends {SinonStub}427 */428var SinonExpectation = function() {};429/**430 * @param {number} n431 * @return {!SinonExpectation}432 */433SinonExpectation.prototype.atLeast = function(n) {};434/**435 * @param {number} n436 * @return {!SinonExpectation}437 */438SinonExpectation.prototype.atMost = function(n) {};439/**440 * @return {!SinonExpectation}441 */442SinonExpectation.prototype.never = function() {};443/**444 * @return {!SinonExpectation}445 */446SinonExpectation.prototype.once = function() {};447/**448 * @return {!SinonExpectation}449 */450SinonExpectation.prototype.twice = function() {};451/**452 * @return {!SinonExpectation}453 */454SinonExpectation.prototype.thrice = function() {};455/**456 * @param {number} n457 * @return {!SinonExpectation}458 */459SinonExpectation.prototype.exactly = function(n) {};460/**461 * @param {...*} args462 * @return {!SinonExpectation}463 * @override464 */465SinonExpectation.prototype.withArgs = function(args) {};466/**467 * @param {...*} args468 * @return {!SinonExpectation}469 */470SinonExpectation.prototype.withExactArgs = function(args) {};471/**472 * @param {*} obj473 * @return {!SinonExpectation}474 */475SinonExpectation.prototype.on = function(obj) {};476/**477 * @return {!SinonExpectation}478 */479SinonExpectation.prototype.verify = function() {};480sinon.expectation = {};481/**482 * @param {string} methodName483 * @return {!SinonExpectation}484 */485sinon.expectation.create = function(methodName) {};486/**487 * @constructor488 */489var SinonMock = function() {};490/**491 * @param {string} method492 * @return {!SinonExpectation}493 */494SinonMock.prototype.expects = function(method) {};495SinonMock.prototype.restore = function() {};496SinonMock.prototype.verify = function() {};497/**498 * @param {*} obj499 * @return {!SinonMock}500 */501sinon.mock = function(obj) {};502/** @interface */503var SinonMatcher = function() {};504/**505 * @param {!SinonMatcher} expr506 * @return {!SinonMatcher}507 */508SinonMatcher.prototype.and = function(expr) {};509/**510 * @param {!SinonMatcher} expr511 * @return {!SinonMatcher}512 */513SinonMatcher.prototype.or = function(expr) {};514/**515 * @param {number|string|RegExp|*|function(*): boolean} value516 * @return {!SinonMatcher}517 */518sinon.match = function(value) {};519/** @type {!SinonMatcher} */520sinon.match.any;521/** @type {!SinonMatcher} */522sinon.match.defined;523/** @type {!SinonMatcher} */524sinon.match.truthy;525/** @type {!SinonMatcher} */526sinon.match.falsy;527/** @type {!SinonMatcher} */528sinon.match.bool;529/** @type {!SinonMatcher} */530sinon.match.number;531/** @type {!SinonMatcher} */532sinon.match.string;533/** @type {!SinonMatcher} */534sinon.match.object;535/** @type {!SinonMatcher} */536sinon.match.func;537/** @type {!SinonMatcher} */538sinon.match.array;539/** @type {!SinonMatcher} */540sinon.match.regexp;541/** @type {!SinonMatcher} */542sinon.match.date;543/**544 * @param {*} obj545 * @return {!SinonMatcher}546 */547sinon.match.same = function(obj) {};548/**549 * @param {string} type550 * @return {!SinonMatcher}551 */552sinon.match.typeOf = function(type) {};553/**554 * @param {*} type555 * @return {!SinonMatcher}556 */557sinon.match.instanceOf = function(type) {};558/**559 * @param {string} property560 * @param {*=} expect561 * @return {!SinonMatcher}562 */563sinon.match.has = function(property, expect) {};564/**565 * @param {string} property566 * @param {*=} expect567 * @return {!SinonMatcher}568 */569sinon.match.hasOwn = function(property, expect) {};570/**571 * @interface572 */573var SinonClock = function() {};574/**575 * @param {!number} ticks576 */577SinonClock.prototype.tick = function(ticks) {};578SinonClock.prototype.restore = function() {};579sinon.sandbox = {};580/**581 * @param {!Object=} opt_config582 * @return {!SinonSandbox}583 */584sinon.sandbox.create = function(opt_config) {};585/**586 * @constructor587 */588var SinonSandbox = function() {};589SinonSandbox.prototype.restore = function() {};590SinonSandbox.prototype.useFakeServer = function() {};591/**592 * @param {*=} objectOrFunction593 * @param {string=} method594 * @return {!SinonSpy}595 */596SinonSandbox.prototype.spy = function(objectOrFunction, method) {};597/**598 * @type {!SinonFakeServer|undefined}599 */600SinonSandbox.prototype.server;601/**602 * @param {*=} obj603 * @param {string=} method604 * @param {*=} func605 * @return {!SinonStub}606 */607SinonSandbox.prototype.stub = function(obj, method, func) {};608/**609 * @param {...?} params610 * @return {SinonClock}611 */612SinonSandbox.prototype.useFakeTimers = function(params) {};613sinon.fakeServer = {};614/**615 * @param {!Object=} opt_config616 * @return {!SinonFakeServer}617 */618sinon.fakeServer.create = function(opt_config) {};619/**620 * @constructor621 */622var SinonFakeServer = function() {};623/**624 * @param {!Object=} opt_config625 */626SinonFakeServer.prototype.configure = function(opt_config) {};627/**628 * @type {!Array<!SinonFakeXmlHttpRequest>}629 */630SinonFakeServer.prototype.requests;631/**632 * @type {boolean|undefined}633 */634SinonFakeServer.prototype.respondImmediately;635SinonFakeServer.prototype.respond = function() {};636SinonFakeServer.prototype.restore = function() {};637/**638 * @type {!SinonFakeXmlHttpRequest}639 */640SinonFakeServer.prototype.xhr;641/**642 * Note: incomplete definition because it is tricky.643 * @param {...*} var_args644 */645SinonFakeServer.prototype.respondWith = function(var_args) {};646/**647 * @return {!SinonFakeXmlHttpRequest}648 */649sinon.useFakeXMLHttpRequest = function() {};650/**651 * @constructor652 * @extends {XMLHttpRequest}653 */654var SinonFakeXmlHttpRequest = function() {};655/**656 * https://sinonjs.org/releases/latest/fake-xhr-and-server/#filtered-requests657 *658 * @type {boolean}659 */660SinonFakeXmlHttpRequest.prototype.useFilters;661/**662 * @type {?string}663 */664SinonFakeXmlHttpRequest.prototype.requestBody;665/**666 * @type {?Object<string, string>}667 */668SinonFakeXmlHttpRequest.prototype.requestHeaders;669/**670 * @type {?string}671 */672SinonFakeXmlHttpRequest.prototype.method;673/**674 * @type {?string}675 */676SinonFakeXmlHttpRequest.prototype.url;677/**678 * https://sinonjs.org/releases/latest/fake-xhr-and-server/#filtered-requests679 *680 * @param {function(string, string, boolean, string, string): boolean} fn681 */682SinonFakeXmlHttpRequest.prototype.addFilter = function(fn) {};683/**684 * @param {?number} status685 * @param {?Object<string, string>} headers686 * @param {?string} body687 */688SinonFakeXmlHttpRequest.prototype.respond = function(status, headers, body) {};689SinonFakeXmlHttpRequest.prototype.restore = function() {};690/**691 * @param {...?} params692 * @return {SinonClock}693 */694sinon.useFakeTimers = function(params) {};695/**696 * Assertions697 * @see http:/sinonjs.org/releases/1.17.7/assertions698 */699sinon.assert = {};700/** @type {string} */701sinon.assert.failException;702/** @param {string} message */703sinon.assert.fail = function(message) {};704/** @param {*} assertion */705sinon.assert.pass = function(assertion) {};706/** @param {!SinonSpy} spy */707sinon.assert.notCalled = function(spy) {};708/** @param {!SinonSpy} spy */709sinon.assert.called = function(spy) {};710/** @param {!SinonSpy} spy */711sinon.assert.calledOnce = function(spy) {};712/** @param {!SinonSpy} spy */713sinon.assert.calledTwice = function(spy) {};714/** @param {!SinonSpy} spy */715sinon.assert.calledThrice = function(spy) {};716/**717 * @param {!SinonSpy} spy718 * @param {number} count719 */720sinon.assert.callCount = function(spy, count) {};721/** @param {...!SinonSpy} spies */722sinon.assert.callOrder = function(...spies) {};723/**724 * @param {!SinonSpy} spy725 * @param {*} obj726 */727sinon.assert.calledOn = function(spy, obj) {};728/**729 * @param {!SinonSpy} spy730 * @param {*} obj731 */732sinon.assert.alwaysCalledOn = function(spy, obj) {};733/**734 * @param {!SinonSpy} spy735 * @param {...*} args736 */737sinon.assert.calledWith = function(spy, args) {};738/**739 * @param {!SinonSpy} spy740 * @param {...*} args741 */742sinon.assert.alwaysCalledWith = function(spy, args) {};743/**744 * @param {!SinonSpy} spy745 * @param {...*} args746 */747sinon.assert.neverCalledWith = function(spy, args) {};748/**749 * @param {!SinonSpy} spy750 * @param {...*} args751 */752sinon.assert.calledWithExactly = function(spy, args) {};753/**754 * @param {!SinonSpy} spy755 * @param {...*} args756 */757sinon.assert.alwaysCalledWithExactly = function(spy, args) {};758/**759 * @param {!SinonSpy} spy760 * @param {...*} args761 */762sinon.assert.calledWithMatch = function(spy, args) {};763/**764 * @param {!SinonSpy} spy765 * @param {...*} args766 */767sinon.assert.alwaysCalledWithMatch = function(spy, args) {};768/**769 * @param {!SinonSpy} spy770 * @param {...*} args771 */772sinon.assert.neverCalledWithMatch = function(spy, args) {};773/**774 * @param {!SinonSpy} spy775 * @param {*=} exception776 */777sinon.assert.threw = function(spy, exception) {};778/**779 * @param {!SinonSpy} spy780 * @param {*=} exception781 *782 * */783sinon.assert.alwaysThrew = function(spy, exception) {};784/**785 * @typedef {{786 * prefix: (string|undefined),787 * includeFail: (boolean|undefined)788 * }}789 */790var SinonExposeOptions;791/**792 * @param {*} obj793 * @param {!SinonExposeOptions=} options794 **/...
polyfill.js
Source:polyfill.js
...4 return;5 }6 var sinonStub = window.parent.sinon.stub;7 window.classListStub = {8 add: sinonStub(),9 remove: sinonStub(),10 toggle: sinonStub(),11 contains: sinonStub(),12 item: sinonStub()13 };14 if ( !( 'classList' in document.createElement( '_' ) ) ) {15 var descriptor = {16 get: function() {17 return window.classListStub;18 },19 enumerable: false,20 configurable: true21 };22 Object.defineProperty( Element.prototype, 'classList', descriptor );23 }...
Using AI Code Generation
1var sinon = require('sinon');2var sinonStub = sinon.stub();3sinonStub.withArgs(1).returns(2);4var sinonStub = sinon.stub();5sinonStub.withArgs(inputValue).returns(returnValue);6sinonStub.withArgs(inputValue).throws(error);7sinonStub.withArgs(inputValue).callsArgWith(index, arg1, arg2, ...);8sinonStub.withArgs(inputValue).callsArgWithAsync(index, arg1, arg2, ...);9sinonStub.withArgs(inputValue).callsArgOnWith(index, context, arg1, arg2, ...);
Using AI Code Generation
1var sinon = require('sinon');2var sinonStub = sinon.stub();3sinonStub.withArgs('foo').returns('bar');4sinonStub.withArgs('baz').throws('quux');5var sinon = require('sinon');6var sinonSpy = sinon.spy();7sinonSpy.withArgs('baz').returns('quux');8var sinon = require('sinon');9var sinonMock = sinon.mock();10sinonMock.expects('foo').once();11sinonMock.expects('bar').twice();12sinonMock.foo();13sinonMock.foo();14sinonMock.bar();15sinonMock.verify();16var sinon = require('sinon');17var sinonTest = sinon.test();18sinonTest(function() {19 this.spy(foo, 'bar');20 foo.bar();21 sinon.assert.calledOnce(foo.bar);22});23var sinon = require('sinon');24var sinonSandbox = sinon.sandbox.create();25sinonSandbox.stub(foo, 'bar');26sinonSandbox.spy(foo, 'baz');27sinonSandbox.mock(foo).expects('quux').once();28sinonSandbox.verifyAndRestore();29var sinon = require('sinon');30var sinonFakeTimers = sinon.useFakeTimers();31sinonFakeTimers.tick(1000);32sinonFakeTimers.restore();33var sinon = require('sinon');34var sinonFakeServer = sinon.fakeServer.create();35sinonFakeServer.respondWith('foo', 'bar');36sinonFakeServer.respond();37sinonFakeServer.restore();
Using AI Code Generation
1var sinon = require('sinon');2var sinonStub = sinon.stub();3sinonStub.returns('hello');4console.log(sinonStub());5var sinon = require('sinon');6var sinonStub = sinon.stub();7sinonStub.returns('hello');8console.log(sinonStub());9var sinon = require('sinon');10var sinonStub = sinon.stub();11sinonStub.returns('hello');12console.log(sinonStub());13var sinon = require('sinon');14var sinonStub = sinon.stub();15sinonStub.returns('hello');16console.log(sinonStub());17var sinon = require('sinon');18var sinonStub = sinon.stub();19sinonStub.returns('hello');20console.log(sinonStub());21var sinon = require('sinon');22var sinonStub = sinon.stub();23sinonStub.returns('hello');24console.log(sinonStub());25var sinon = require('sinon');26var sinonStub = sinon.stub();27sinonStub.returns('hello');28console.log(sinonStub());29var sinon = require('sinon');30var sinonStub = sinon.stub();31sinonStub.returns('hello');32console.log(sinonStub());33var sinon = require('sinon');34var sinonStub = sinon.stub();35sinonStub.returns('hello');36console.log(sinonStub());37var sinon = require('sinon');38var sinonStub = sinon.stub();39sinonStub.returns('hello');40console.log(sinonStub());41var sinon = require('sinon');42var sinonStub = sinon.stub();43sinonStub.returns('hello');44console.log(sinonStub());45var sinon = require('sinon');46var sinonStub = sinon.stub();47sinonStub.returns('hello');48console.log(sinonStub());49var sinon = require('sinon');50var sinonStub = sinon.stub();51sinonStub.returns('hello');52console.log(sinonStub());
Using AI Code Generation
1var sinon = require('sinon');2var expect = require('chai').expect;3var sinonStub = sinon.stub();4sinonStub.returns(10);5expect(sinonStub()).to.equal(10);6var sinonSpy = sinon.spy();7sinonSpy();8expect(sinonSpy.called).to.equal(true);9var sinonMock = sinon.mock();10sinonMock.expects('foo').once();11sinonMock.foo();12expect(sinonMock.verify()).to.equal(undefined);13var sinonSandbox = sinon.sandbox.create();14sinonSandbox.stub().returns(10);15sinonSandbox.spy();16sinonSandbox.mock();17sinonSandbox.restore();18var sinonFakeTimers = sinon.useFakeTimers();19sinonFakeTimers.tick(1000);20sinonFakeTimers.restore();21var sinonFakeServer = sinon.fakeServer.create();22sinonFakeServer.respondWith('foo');23sinonFakeServer.respond();24sinonFakeServer.restore();25var sinonFakeXMLHttpRequest = sinon.useFakeXMLHttpRequest();26sinonFakeXMLHttpRequest.onCreate = function (xhr) {27 xhr.respond(200, { 'Content-Type': 'text/plain' }, 'Hello World');28};29sinonFakeXMLHttpRequest.restore();30var sinonFakeServerWithClock = sinon.fakeServerWithClock.create();31sinonFakeServerWithClock.tick(1000);32sinonFakeServerWithClock.restore();33var sinonFakeServerWithClock = sinon.fakeServerWithClock.create();34sinonFakeServerWithClock.tick(1000);35sinonFakeServerWithClock.restore();36var sinonFakeServerWithClock = sinon.fakeServerWithClock.create();37sinonFakeServerWithClock.tick(1000);38sinonFakeServerWithClock.restore();39var sinonFakeServerWithClock = sinon.fakeServerWithClock.create();40sinonFakeServerWithClock.tick(1000);41sinonFakeServerWithClock.restore();
Using AI Code Generation
1var stub = sinon.stub();2stub.withArgs(42).returns(1);3stub.withArgs(43).throws("TypeError");4var spy = sinon.spy();5spy(42);6var mock = sinon.mock();7mock.expects("foo").once();8mock.expects("bar").once();9mock.verify();10var stub = sinon.stub();11stub.withArgs(42).returns(1);12stub.withArgs(43).throws("TypeError");13var spy = sinon.spy();14spy(42);15var mock = sinon.mock();16mock.expects("foo").once();17mock.expects("bar").once();18mock.verify();19var stub = sinon.stub();20stub.withArgs(42).returns(1);21stub.withArgs(43).throws("TypeError");22var spy = sinon.spy();23spy(42);24var mock = sinon.mock();25mock.expects("foo").once();26mock.expects("bar").once();27mock.verify();28var stub = sinon.stub();29stub.withArgs(42).returns(1);30stub.withArgs(43).throws("TypeError");
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3var myObj = require('./myObj');4var myObj = new myObj();5var stub = sinon.stub(myObj, 'myMethod');6stub.withArgs('foo').returns(100);7stub.withArgs('bar').returns(200);8assert.equal(myObj.myMethod('foo'), 100);9assert.equal(myObj.myMethod('bar'), 200);10assert.equal(myObj.myMethod('baz'), undefined);11var myObj = function() {};12myObj.prototype.myMethod = function(param) {13 return param;14};15module.exports = myObj;16var sinon = require('sinon');17var chai = require('chai');18var sinonChai = require('sinon-chai');19var assert = chai.assert;20var myObj = require('./myObj');21var myObj = new myObj();22var stub = sinon.stub(myObj, 'myMethod');23stub.withArgs('foo').returns(100);24stub.withArgs('bar').returns(200);25assert.equal(myObj.myMethod('foo'), 100);26assert.equal(myObj.myMethod('bar'), 200);27assert.equal(myObj.myMethod('baz'), undefined);28chai.use(sinonChai);29expect(stub).to.have.been.calledWith('foo');
Using AI Code Generation
1import sinon from 'sinon';2import * as foo from './foo';3import { bar } from './foo';4describe('foo', () => {5 it('should call stubbed method', () => {6 const stub = sinon.stub(foo, 'bar').returns(1);7 const result = foo.baz();8 expect(result).toBe(1);9 expect(stub.calledOnce).toBe(true);10 });11});12export function bar() {13 return 1;14}15export function baz() {16 return bar();17}18import sinon from 'sinon';19import Foo from './foo';20describe('foo', () => {21 it('should call stubbed method', () => {22 const stub = sinon.stub(Foo.prototype, 'foo').returns(1);23 const foo = new Foo();24 const result = foo.baz();25 expect(result).toBe(1);26 expect(stub.calledOnce).toBe(true);27 });28});29export default class Foo {30 foo() {31 return 1;32 }33 baz() {34 return this.foo();35 }36}37Stubbing a class method which is imported from a different file
Using AI Code Generation
1const sinon = require('sinon');2const assert = require('assert');3const obj = {4 method: function() {5 console.log('method called');6 }7};8const stub = sinon.stub(obj, 'method');9obj.method();10assert(stub.calledOnce);11stub.restore();12obj.method();13assert(stub.calledOnce);14const sinon = require('sinon');15const assert = require('assert');16const obj = {17 method: function() {18 console.log('method called');19 }20};21const stub = sinon.stub(obj, 'method');22obj.method();23assert(stub.calledOnce);24obj.method();25assert(stub.calledOnce);
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!!