Best JavaScript code snippet using sinon
mock-expectation.js
Source:mock-expectation.js
...16 return "never called";17 }18 return "called " + timesInWords(callCount);19}20function expectedCallCountInWords(expectation) {21 var min = expectation.minCalls;22 var max = expectation.maxCalls;23 if (typeof min === "number" && typeof max === "number") {24 var str = timesInWords(min);25 if (min !== max) {26 str = "at least " + str + " and at most " + timesInWords(max);27 }28 return str;29 }30 if (typeof min === "number") {31 return "at least " + timesInWords(min);32 }33 return "at most " + timesInWords(max);34}35function receivedMinCalls(expectation) {36 var hasMinLimit = typeof expectation.minCalls === "number";37 return !hasMinLimit || expectation.callCount >= expectation.minCalls;38}39function receivedMaxCalls(expectation) {40 if (typeof expectation.maxCalls !== "number") {41 return false;42 }43 return expectation.callCount === expectation.maxCalls;44}45function verifyMatcher(possibleMatcher, arg) {46 var isMatcher = match && match.isMatcher(possibleMatcher);47 return isMatcher && possibleMatcher.test(arg) || true;48}49var mockExpectation = {50 minCalls: 1,51 maxCalls: 1,52 create: function create(methodName) {53 var expectation = extend(stub.create(), mockExpectation);54 delete expectation.create;55 expectation.method = methodName;56 return expectation;57 },58 invoke: function invoke(func, thisValue, args) {59 this.verifyCallAllowed(thisValue, args);60 return spyInvoke.apply(this, arguments);61 },62 atLeast: function atLeast(num) {63 if (typeof num !== "number") {64 throw new TypeError("'" + valueToString(num) + "' is not number");65 }66 if (!this.limitsSet) {67 this.maxCalls = null;68 this.limitsSet = true;69 }70 this.minCalls = num;71 return this;72 },73 atMost: function atMost(num) {74 if (typeof num !== "number") {75 throw new TypeError("'" + valueToString(num) + "' is not number");76 }77 if (!this.limitsSet) {78 this.minCalls = null;79 this.limitsSet = true;80 }81 this.maxCalls = num;82 return this;83 },84 never: function never() {85 return this.exactly(0);86 },87 once: function once() {88 return this.exactly(1);89 },90 twice: function twice() {91 return this.exactly(2);92 },93 thrice: function thrice() {94 return this.exactly(3);95 },96 exactly: function exactly(num) {97 if (typeof num !== "number") {98 throw new TypeError("'" + valueToString(num) + "' is not a number");99 }100 this.atLeast(num);101 return this.atMost(num);102 },103 met: function met() {104 return !this.failed && receivedMinCalls(this);105 },106 verifyCallAllowed: function verifyCallAllowed(thisValue, args) {107 var expectedArguments = this.expectedArguments;108 if (receivedMaxCalls(this)) {109 this.failed = true;110 mockExpectation.fail(this.method + " already called " + timesInWords(this.maxCalls));111 }112 if ("expectedThis" in this && this.expectedThis !== thisValue) {113 mockExpectation.fail(this.method + " called with " + valueToString(thisValue) +114 " as thisValue, expected " + valueToString(this.expectedThis));115 }116 if (!("expectedArguments" in this)) {117 return;118 }119 if (!args) {120 mockExpectation.fail(this.method + " received no arguments, expected " +121 format(expectedArguments));122 }123 if (args.length < expectedArguments.length) {124 mockExpectation.fail(this.method + " received too few arguments (" + format(args) +125 "), expected " + format(expectedArguments));126 }127 if (this.expectsExactArgCount &&128 args.length !== expectedArguments.length) {129 mockExpectation.fail(this.method + " received too many arguments (" + format(args) +130 "), expected " + format(expectedArguments));131 }132 expectedArguments.forEach(function (expectedArgument, i) {133 if (!verifyMatcher(expectedArgument, args[i])) {134 mockExpectation.fail(this.method + " received wrong arguments " + format(args) +135 ", didn't match " + expectedArguments.toString());136 }137 if (!deepEqual(expectedArgument, args[i])) {138 mockExpectation.fail(this.method + " received wrong arguments " + format(args) +139 ", expected " + format(expectedArguments));140 }141 }, this);142 },143 allowsCall: function allowsCall(thisValue, args) {144 var expectedArguments = this.expectedArguments;145 if (this.met() && receivedMaxCalls(this)) {146 return false;147 }148 if ("expectedThis" in this && this.expectedThis !== thisValue) {149 return false;150 }151 if (!("expectedArguments" in this)) {152 return true;153 }154 args = args || [];155 if (args.length < expectedArguments.length) {156 return false;157 }158 if (this.expectsExactArgCount &&159 args.length !== expectedArguments.length) {160 return false;161 }162 return expectedArguments.every(function (expectedArgument, i) {163 if (!verifyMatcher(expectedArgument, args[i])) {164 return false;165 }166 if (!deepEqual(expectedArgument, args[i])) {167 return false;168 }169 return true;170 });171 },172 withArgs: function withArgs() {173 this.expectedArguments = slice.call(arguments);174 return this;175 },176 withExactArgs: function withExactArgs() {177 this.withArgs.apply(this, arguments);178 this.expectsExactArgCount = true;179 return this;180 },181 on: function on(thisValue) {182 this.expectedThis = thisValue;183 return this;184 },185 toString: function () {186 var args = (this.expectedArguments || []).slice();187 if (!this.expectsExactArgCount) {188 push.call(args, "[...]");189 }190 var callStr = spyCallToString.call({191 proxy: this.method || "anonymous mock expectation",192 args: args193 });194 var message = callStr.replace(", [...", "[, ...") + " " +195 expectedCallCountInWords(this);196 if (this.met()) {197 return "Expectation met: " + message;198 }199 return "Expected " + message + " (" +200 callCountInWords(this.callCount) + ")";201 },202 verify: function verify() {203 if (!this.met()) {204 mockExpectation.fail(this.toString());205 } else {206 mockExpectation.pass(this.toString());207 }208 return true;209 },...
Using AI Code Generation
1var sinon = require('sinon');2var chai = require('chai');3var assert = chai.assert;4var expect = chai.expect;5var should = chai.should();6var sinonChai = require('sinon-chai');7chai.use(sinonChai);8var myFunc = function (a, b, c) {9 console.log(a + b + c);10};11describe('Testing myFunc', function () {12 it('should call the function only once', function () {13 var spy = sinon.spy(myFunc);14 spy(1, 2, 3);15 spy.should.have.been.calledOnce;16 spy.should.have.been.calledWith(1, 2, 3);17 });18});
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3var myObj = {4 myMethod: function() {}5};6var spy = sinon.spy(myObj, 'myMethod');7myObj.myMethod();8myObj.myMethod();9myObj.myMethod();10var expectedCallCountInWords = spy.expectedCallCountInWords();11assert.equal(expectedCallCountInWords, 'exactly 3 times');12var sinon = require('sinon');13var assert = require('assert');14var myObj = {15 myMethod: function() {}16};17var spy = sinon.spy(myObj, 'myMethod');18myObj.myMethod();19myObj.myMethod();20myObj.myMethod();21assert.equal(spy.callCount, 3);22assert.callCount(spy, expectedCallCount, [message])23assert.called(spy, [message])24assert.calledOnce(spy, [message])25assert.calledTwice(spy, [message])26assert.calledThrice(spy, [message])27assert.calledBefore(spy, otherSpy, [message])
Using AI Code Generation
1var sinon = require('sinon');2var assert = sinon.assert;3var spy = sinon.spy();4spy("Hello World");5assert.calledOnce(spy);6assert.calledWith(spy, "Hello World");7assert.calledOnceWith(spy, "Hello World");8assert.callCount(spy, 1);9assert.callCountInWords(spy, "once");10assert.callCountInWords(spy, "once", "Hello World");11assert.callCountInWords(spy, "once", "Hello World", "Hello World");12assert.callCountInWords(spy, "once", "Hello World", "Hello World", "Hello World");13assert.callCountInWords(spy, "once", "Hello World", "Hello World", "Hello World", "Hello World");14assert.callCountInWords(spy, "once", "Hello World", "Hello World", "Hello World", "Hello World", "Hello World");15assert.callCountInWords(spy, "once", "Hello World", "Hello World", "Hello World", "Hello World", "Hello World", "Hello World");16assert.callCountInWords(spy, "once", "Hello World", "Hello World", "Hello World", "Hello World", "Hello World", "Hello World", "Hello World");17assert.callCountInWords(spy, "once", "Hello World", "Hello World", "Hello World", "Hello World", "Hello World", "Hello World", "Hello World", "Hello World");
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3var sinonTest = require('sinon-test')(sinon, {useFakeTimers: false});4var myModule = require('./myModule');5describe('sinon-test', function() {6 var clock;7 beforeEach(function() {8 clock = sinon.useFakeTimers();9 });10 afterEach(function() {11 clock.restore();12 });13 it('should call myModule.doSomething once', sinonTest(function() {14 var spy = this.spy(myModule, 'doSomething');15 myModule.doSomething();16 clock.tick(100);17 assert(spy.calledOnce);18 }));19 it('should call myModule.doSomething twice', sinonTest(function() {20 var spy = this.spy(myModule, 'doSomething');21 myModule.doSomething();22 myModule.doSomething();23 clock.tick(100);24 assert(spy.calledTwice);25 }));26 it('should call myModule.doSomething thrice', sinonTest(function() {27 var spy = this.spy(myModule, 'doSomething');28 myModule.doSomething();29 myModule.doSomething();30 myModule.doSomething();31 clock.tick(100);32 assert(spy.calledThrice);33 }));34});35exports.doSomething = function() {36 setTimeout(function() {37 console.log('done');38 }, 100);39};
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3var obj = { method: function() { } };4var spy = sinon.spy(obj, "method");5obj.method("foo", "bar");6obj.method("foo", "bar");7obj.method("foo", "bar");8assert.equal(spy.callCount, 3);9assert.equal(spy.getCall(0).args[0], "foo");10assert.equal(spy.getCall(1).args[0], "foo");11assert.equal(spy.getCall(2).args[0], "foo");12assert.equal(spy.getCall(0).args[1], "bar");13assert.equal(spy.getCall(1).args[1], "bar");14assert.equal(spy.getCall(2).args[1], "bar");15assert.equal(spy.calledWith("foo", "bar"), true);16assert.equal(spy.calledWith("foo", "baz"), false);17assert.equal(spy.calledWith("foo"), true);18assert.equal(spy.calledWith("foo", "bar", "baz"), false);19assert.equal(spy.calledWith("foo", "bar", "baz", "qux"), false);20assert.equal(spy.calledWith("foo"), true);21assert.equal(spy.calledWith("foo", "bar"), true);22assert.equal(spy.calledWith("foo", "bar", "baz"), false);23assert.equal(spy.calledWith("foo", "bar", "baz", "qux"), false);24assert.equal(spy.calledWith("foo"), true);25assert.equal(spy.calledWith("foo", "bar"), true);26assert.equal(spy.calledWith("foo", "bar", "baz"), false);27assert.equal(spy.calledWith("foo", "bar", "baz", "qux"), false);28assert.equal(spy.calledWith("foo"), true);29assert.equal(spy.calledWith("foo", "bar"), true);30assert.equal(spy.calledWith("foo", "bar", "baz"), false);31assert.equal(spy.calledWith("foo", "bar", "baz", "qux"), false);32assert.equal(spy.calledWith("foo"), true);33assert.equal(spy.calledWith("foo", "bar"), true);34assert.equal(spy.calledWith("foo", "bar", "baz"), false);35assert.equal(spy.calledWith("foo", "bar", "baz", "qux"), false);36assert.equal(spy
Using AI Code Generation
1sinon.assert.expectedCallCountInWords = function (expected, actual, message) {2 var expectedWord = "once";3 var actualWord = "once";4 if (expected > 1) {5 expectedWord = "twice";6 }7 if (actual > 1) {8 actualWord = "twice";9 }10 if (expected !== actual) {11 this.fail((message ? message + ": " : "") + "Expected " + expectedWord + " but was " + actualWord);12 }13};14sinon.assert.expectedCallCountInWords = function (expected, actual, message) {15 var expectedWord = "once";16 var actualWord = "once";17 if (expected > 1) {18 expectedWord = "twice";19 }20 if (actual > 1) {21 actualWord = "twice";22 }23 if (expected !== actual) {24 this.fail((message ? message + ": " : "") + "Expected " + expectedWord + " but was " + actualWord);25 }26};27sinon.assert.expectedCallCountInWords = function (expected, actual, message) {28 var expectedWord = "once";29 var actualWord = "once";30 if (expected > 1) {31 expectedWord = "twice";32 }33 if (actual > 1) {34 actualWord = "twice";35 }36 if (expected !== actual) {37 this.fail((message ? message + ": " : "") + "Expected " + expectedWord + " but was " + actualWord);38 }39};40sinon.assert.expectedCallCountInWords = function (expected, actual, message) {41 var expectedWord = "once";42 var actualWord = "once";43 if (expected > 1) {44 expectedWord = "twice";
Using AI Code Generation
1var sinon = require("sinon");2var assert = require("assert");3var myModule = require("./myModule");4var myModule = myModule();5describe("myModule", function() {6 it("should call myMethod 3 times", function() {7 var spy = sinon.spy(myModule, "myMethod");8 myModule.myMethod();9 myModule.myMethod();10 myModule.myMethod();11 assert.equal(spy.expectedCallCountInWords(), "exactly 3 times");12 });13});14module.exports = function() {15 var myModule = {};16 myModule.myMethod = function() {17 console.log("myMethod called");18 };19 myModule.myMethod();20 myModule.myMethod();21 myModule.myMethod();22 return myModule;23}
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!!