Best JavaScript code snippet using sinon
sinon.js
Source:sinon.js
1/**2 * This file is part of the Unit.js testing framework.3 *4 * (c) Nicolas Tallefourtane <dev@nicolab.net>5 *6 * For the full copyright and license information, please view7 * the LICENSE file distributed with this source code8 * or visit http://unitjs.com.9 *10 * @author Nicolas Tallefourtane <dev@nicolab.net>11 */12'use strict';13var test = require('../../src');14function once(fn) {15 var returnValue;16 var called = false;17 return function() {18 if (!called) {19 called = true;20 returnValue = fn.apply(this, arguments);21 }22 return returnValue;23 };24}25var jQuery = {26 ajax: function() {}27};28function getTodos(listId, callback) {29 jQuery.ajax({30 url: '/todo/' + listId + '/items',31 success: function(data) {32 // Node-style CPS: callback(err, data)33 callback(null, data);34 }35 });36}37describe('Unit.js provides sinon.js', function() {38 describe('Spies', function() {39 it('calls the original function', function() {40 var callback = test.spy();41 var proxy = once(callback);42 proxy();43 test.assert(callback.called);44 });45 it('calls the original function only once', function() {46 var callback = test.spy();47 var proxy = once(callback);48 proxy();49 proxy();50 test.assert(callback.calledOnce);51 // ...or:52 test.assert.strictEqual(callback.callCount, 1);53 });54 it('calls original function with right this and args', function() {55 var callback = test.spy();56 var proxy = once(callback);57 var obj = {};58 proxy.call(obj, 1, 2, 3);59 test.assert(callback.calledOn(obj));60 test.assert(callback.calledWith(1, 2, 3));61 });62 });63 describe('Stubs', function() {64 it('returns the return value from the original function', function() {65 var callback = test.stub().returns(42);66 var proxy = once(callback);67 test.assert.strictEqual(proxy(), 42);68 });69 });70 describe('Testing Ajax', function() {71 after(function() {72 // When the test either fails or passes, restore the original73 // jQuery ajax function (Sinon.JS also provides tools to help74 // test frameworks automate clean-up like this)75 jQuery.ajax.restore();76 });77 it('makes a GET request for todo items', function() {78 test.stub(jQuery, 'ajax');79 getTodos(42, test.spy());80 test.assert(jQuery.ajax.calledWithMatch({81 url: '/todo/42/items'82 }));83 });84 });85 describe('Mocks', function() {86 it('returns the return value from the original function', function() {87 var myAPI = {88 method: function() {}89 };90 var mock = test.mock(myAPI);91 mock.expects('method').once().returns(42);92 var proxy = once(myAPI.method);93 test.assert.equal(proxy(), 42);94 mock.verify();95 });96 it('test should call a method with exceptions', function() {97 var myAPI = {98 method: function() {}99 };100 var mock = test.mock(myAPI);101 mock.expects('method').once().throws();102 test.exception(function() {103 myAPI.method();104 })105 .isInstanceOf(Error);106 mock.verify();107 });108 });109 describe('Matchers', function() {110 it('test should assert fuzzy', function() {111 var book = {112 pages: 42,113 author: 'cjno'114 };115 var spy = test.spy();116 spy(book);117 test.sinon.assert.calledWith(spy, test.sinon.match({118 author: 'cjno'119 }));120 test.sinon.assert.calledWith(spy, test.sinon.match.has('pages', 42));121 });122 it('test should stub method differently based on argument types',123 function() {124 var callback = test.stub();125 callback.withArgs(test.sinon.match.string).returns(true);126 callback.withArgs(test.sinon.match.number).throws('TypeError');127 test.bool(callback('abc')).isTrue(); // Returns true128 test.exception(function() {129 callback(123); // Throws TypeError130 })131 .isValid(function(err) {132 if (err.name === 'TypeError') {133 return true;134 }135 });136 });137 it('Combining matchers', function() {138 var stringOrNumber = test.sinon.match.string139 .or(test.sinon.match.number);140 var bookWithPages = test.sinon.match.object141 .and(test.sinon.match.has('pages'));142 var book = {143 pages: 42,144 author: 'cjno'145 };146 var spy = test.spy();147 var otherSpy = test.spy();148 spy(book);149 otherSpy(10);150 test.sinon.assert.calledWith(spy, bookWithPages);151 test.sinon.assert.calledWith(otherSpy, stringOrNumber);152 });153 it('Custom matchers', function() {154 var equal10 = test.sinon.match(function(value) {155 return value === 10;156 }, 'value is not equal to 10');157 var spy = test.spy();158 var otherSpy = test.spy();159 spy(10);160 otherSpy(42);161 // ok because the argument value 10 is identical to 10 expected162 test.sinon.assert.calledWith(spy, equal10);163 test.exception(function() {164 // throws an exception because the argument value 42165 // is not identical to 10 expected166 test.sinon.assert.calledWith(otherSpy, equal10);167 })168 .hasMessage(/value is not equal to 10/);169 });170 });171 describe('Sandbox', function() {172 it('test using test.sinon.test sandbox', test.sinon.test(function() {173 var myAPI = {174 method: function() {}175 };176 var mockMyApi = this.mock(myAPI).expects('method').once().returns(177 42);178 var proxy = once(myAPI.method);179 test.number(proxy()).isIdenticalTo(42);180 mockMyApi.verify();181 }));182 });...
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3var object = {4 method: function(arg1, arg2) {5 return arg1 + arg2;6 }7};8var spy = sinon.spy(object, "method");9object.method("foo", "bar");10object.method("baz", "quux");11assert(spy.calledWith(sinon.match.string, sinon.match.string.or(sinon.match.number)));12assert(spy.calledWith(sinon.match.string, sinon.match.string.or(sinon.match.number)));13assert(spy.calledWith(sinon.match.string, sinon.match.string.or(sinon.match.number)));14assert(spy.calledWith(sinon.match.string, sinon.match.string.or(sinon.match.number)));15assert(spy.calledWith(sinon.match.string, sinon.match.string.or(sinon.match.number)));16assert(spy.calledWith(sinon.match.string, sinon.match.string.or(sinon.match.number)));17assert(spy.calledWith(sinon.match.string, sinon.match.string.or(sinon.match.number)));18assert(spy.calledWith(sinon.match.string, sinon.match.string.or(sinon.match.number)));19assert(spy.calledWith(sinon.match.string, sinon.match.string.or(sinon.match.number)));20assert(spy.calledWith(sinon.match.string, sinon.match.string.or(sinon.match.number)));21var sinon = require('sinon');22var assert = require('assert');23var object = {24 method: function(arg1, arg2) {25 return arg1 + arg2;26 }27};28var spy = sinon.spy(object, "method");29object.method("
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3var spy = sinon.spy();4spy('hello', 'world');5assert(spy.calledWith(sinon.match.string.or(sinon.match.string)));6assert(spy.calledWith(sinon.match.string.or(sinon.match.number)));7var sinon = require('sinon');8var assert = require('assert');9var spy = sinon.spy();10spy('hello', 'world');11assert(spy.calledWith(sinon.match.string.and(sinon.match.string)));12assert(!spy.calledWith(sinon.match.string.and(sinon.match.number)));13var sinon = require('sinon');14var assert = require('assert');15var spy = sinon.spy();16spy('hello', 'world');17assert(spy.calledWith(sinon.match.string.defined));18assert(!spy.calledWith(sinon.match.string.undefined));19var sinon = require('sinon');20var assert = require('assert');21var spy = sinon.spy();22spy('hello', 'world');23assert(spy.calledWith(sinon.match.string.startsWith('hello')));24assert(!spy.calledWith(sinon.match.string.startsWith('world')));25var sinon = require('sinon');26var assert = require('assert');27var spy = sinon.spy();28spy('hello', 'world');29assert(spy.calledWith(sinon.match.string.endsWith('world')));30assert(!spy.calledWith(sinon.match.string.endsWith('hello')));31var sinon = require('sinon');32var assert = require('assert');33var spy = sinon.spy();34spy('hello', 'world');35assert(spy.calledWith(sinon.match.string.contains('hello')));36assert(spy.calledWith(sinon.match.string.contains('world')));37assert(!spy.calledWith(sinon.match.string.contains('foo')));
Using AI Code Generation
1var sinon = require('sinon');2var sinonTest = require('sinon-test');3sinon.test = sinonTest.configureTest(sinon);4var assert = require('assert');5var myModule = require('./myModule');6describe('myModule', function() {7 it('should return true if the argument is a string', sinon.test(function() {8 var stub = this.stub(console, 'log');9 myModule('hello');10 assert(stub.calledWith(sinon.match.string));11 }));12 it('should return true if the argument is a string or a number', sinon.test(function() {13 var stub = this.stub(console, 'log');14 myModule(123);15 assert(stub.calledWith(sinon.match.string.or(sinon.match.number)));16 }));17});18module.exports = function(str) {19 console.log(str);20}21assert(stub.calledWith(sinon.match.string.or(sinon.match.number)));22assert(stub.calledWith(sinon.match.string.or(sinon.match.number).or(sinon.match.number)));23assert(stub.calledWith(sinon.match.string.or(sinon.match.number).or(sinon.match.string)));24assert(stub.calledWith(sinon.match.string.or(sinon.match.number).or(sinon.match.number).or(sinon.match.string)));25assert(stub.calledWith(sinon.match.string.or(sinon.match.number).or(sinon.match.number).or(sinon.match.string).or(sinon.match.number)));26assert(stub.calledWith(sinon.match.string.or(sinon.match.number).or(sinon.match.number).or(sinon.match.string).or(sinon.match.number).or(sinon.match.string)));27assert(stub.calledWith(sinon.match.string.or(sinon.match.number).or(sinon.match.number).or(sinon.match.string).or(sinon.match.number).or(sinon.match.string).or(sinon.match.number)));28assert(stub.calledWith(sinon.match.string.or(sinon.match.number).or(sinon.match.number).or(sinon.match.string).or(sinon.match.number
Using AI Code Generation
1var sinon = require('sinon');2var myFunc = function (arg) {3 if (arg === 'foo') {4 console.log('foo');5 } else if (arg === 'bar') {6 console.log('bar');7 }8};9var spy = sinon.spy(myFunc);10spy('foo');11spy('bar');12spy('baz');13console.log(spy.withArgs(sinon.match.string.or('foo', 'bar')).calledTwice);14console.log(spy.withArgs(sinon.match.string.or('foo', 'bar')).calledThrice);15console.log(spy.withArgs(sinon.match.string.or('foo', 'bar')).calledOnce);
Using AI Code Generation
1var assert = require('assert');2var sinon = require('sinon');3var myModule = require('./myModule');4describe('myModule', function () {5 it('should call callback with "foo" or "bar"', function () {6 var callback = sinon.spy();7 myModule(callback);8 assert(callback.calledWith(sinon.match.string.or("foo", "bar")));9 });10});11var myModule = function (callback) {12 callback('foo');13};14module.exports = myModule;
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3describe('sinon', function() {4 it('should match the string', function() {5 var spy = sinon.spy();6 spy('hello');7 assert(spy.calledWith(sinon.match.string.or(sinon.match.number)));8 });9});10var sinon = require('sinon');11var assert = require('assert');12describe('sinon', function() {13 it('should match the string', function() {14 var spy = sinon.spy();15 spy('hello');16 assert(spy.calledWith(sinon.match.string.and(sinon.match.number)));17 });18});19var sinon = require('sinon');20var assert = require('assert');21describe('sinon', function() {22 it('should match the string', function() {23 var spy = sinon.spy();24 spy('hello');25 assert(spy.calledWith(sinon.match.string.startsWith('he')));26 });27});28var sinon = require('sinon');29var assert = require('assert');30describe('sinon', function() {31 it('should match the string', function() {32 var spy = sinon.spy();33 spy('
Using AI Code Generation
1var sinon = require('sinon');2var obj = {3 method: function(value) {4 console.log('Invoked');5 }6};7var spy = sinon.spy(obj, 'method');8obj.method('test');9console.log(spy.calledWith(sinon.match.string.or(sinon.match.number)));
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3var obj = {};4var spy = sinon.spy();5obj.method = spy;6obj.method('foo', 42);7assert(spy.calledWithMatch(stringOrNumber, stringOrNumber));8assert(spy.calledWithMatch(string, number));9assert(spy.calledWithMatch('foo', 42));10assert(spy.calledWithMatch('foo', 'bar') === false);11assert(spy.calledWithMatch(42, 'bar') === false);12var sinon = require('sinon');13var assert = require('assert');14var obj = {};15var spy = sinon.spy();16obj.method = spy;17obj.method('foo', 42);18assert(spy.calledWithMatch(stringAndNumber));19assert(spy.calledWithMatch(string, number));20assert(spy.calledWithMatch('foo', 42));21assert(spy.calledWithMatch('foo', 'bar') === false);22assert(spy.calledWithMatch(42, 'bar') === false);23var sinon = require('sinon');24var assert = require('assert');25var obj = {};26var spy = sinon.spy();27obj.method = spy;28obj.method('foo', 42);29assert(spy.calledWithMatch(stringXorNumber, stringXorNumber));30assert(spy.calledWithMatch(string, number));31assert(spy.calledWithMatch('foo', 42) === false);32assert(spy.calledWithMatch('foo', 'bar'));33assert(spy.calledWithMatch(42, 'bar'));34var sinon = require('sinon');35var assert = require('assert');36var obj = {};37var spy = sinon.spy();
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3var object = {4 method: function (arg1, arg2) {5 return arg1 + arg2;6 }7};8var spy = sinon.spy(object, "method");9var stub = sinon.stub().returns(42);10object.method(1, stub);11assert.ok(spy.calledWith(sinon.match.string.or(sinon.match.number), sinon.match.string));12assert.ok(spy.neverCalledWith(sinon.match.string, sinon.match.number));13assert.ok(spy.neverCalledWith(sinon.match.string, sinon.match.string));14 at Object.<anonymous> (C:\Users\Jagadeesh\Desktop\sinon\test.js:17:14)15 at Module._compile (module.js:652:30)16 at Object.Module._extensions..js (module.js:663:10)17 at Module.load (module.js:565:32)18 at tryModuleLoad (module.js:505:12)19 at Function.Module._load (module.js:497:3)20 at Function.Module.runMain (module.js:693:10)21 at startup (bootstrap_node.js:188:16)
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!!