Best JavaScript code snippet using sinon
Graph-test.js
Source:Graph-test.js
...104 ['Arbitrary', '/absolute/path/to/Arbitrary.js'],105 ['../entry.js', '/whereever/is/entry.js'],106 ]);107 for (const [id, file] of modules) {108 resolve.stub.withArgs(id).yields(null, file);109 }110 const [file1, file2] = modules.values();111 graph(modules.keys(), anyPlatform, noOpts, () => {112 expect(load).toBeCalledWith(file1, any(Object), any(Function));113 expect(load).toBeCalledWith(file2, any(Object), any(Function));114 done();115 });116 });117 it('passes the `optimize` flag on to `load`', done => {118 graph(anyEntry, anyPlatform, {optimize: true}, () => {119 expect(load).toBeCalledWith(120 any(String), objectContaining({optimize: true}), any(Function));121 done();122 });123 });124 it('uses `false` as the default for the `optimize` flag', done => {125 graph(anyEntry, anyPlatform, noOpts, () => {126 expect(load).toBeCalledWith(127 any(String), objectContaining({optimize: false}), any(Function));128 done();129 });130 });131 it('forwards a passed-in `log` to `load`', done => {132 const log = new Console();133 graph(anyEntry, anyPlatform, {log}, () => {134 expect(load)135 .toBeCalledWith(any(String), objectContaining({log}), any(Function));136 done();137 });138 });139 it('calls back with every error produced by `load`', done => {140 const error = Error();141 load.stub.yields(error);142 graph(anyEntry, anyPlatform, noOpts, e => {143 expect(e).toBe(error);144 done();145 });146 });147 it('resolves any dependencies provided by `load`', done => {148 const entryPath = '/path/to/entry.js';149 const id1 = 'required/id';150 const id2 = './relative/import';151 resolve.stub.withArgs('entry').yields(null, entryPath);152 load.stub.withArgs(entryPath)153 .yields(null, {path: entryPath}, [id1, id2]);154 graph(['entry'], anyPlatform, noOpts, () => {155 expect(resolve).toBeCalledWith(156 id1, entryPath, any(String), any(Object), any(Function));157 expect(resolve).toBeCalledWith(158 id2, entryPath, any(String), any(Object), any(Function));159 done();160 });161 });162 it('loads transitive dependencies', done => {163 const entryPath = '/path/to/entry.js';164 const id1 = 'required/id';165 const id2 = './relative/import';166 const path1 = '/path/to/dep/1';167 const path2 = '/path/to/dep/2';168 resolve.stub169 .withArgs(id1).yields(null, path1)170 .withArgs(id2).yields(null, path2)171 .withArgs('entry').yields(null, entryPath);172 load.stub173 .withArgs(entryPath).yields(null, {path: entryPath}, [id1])174 .withArgs(path1).yields(null, {path: path1}, [id2]);175 graph(['entry'], anyPlatform, noOpts, () => {176 expect(resolve).toBeCalledWith(id2, path1, any(String), any(Object), any(Function));177 expect(load).toBeCalledWith(path1, any(Object), any(Function));178 expect(load).toBeCalledWith(path2, any(Object), any(Function));179 done();180 });181 });182 it('resolves modules in depth-first traversal order, regardless of the order of resolution',183 done => {184 load.stub.reset();185 resolve.stub.reset();186 const ids = [187 'a',188 'b',189 'c', 'd',190 'e',191 'f', 'g',192 'h',193 ];194 ids.forEach(id => {195 const path = idToPath(id);196 resolve.stub.withArgs(id).yields(null, path);197 load.stub.withArgs(path).yields(null, createFile(id), []);198 });199 load.stub.withArgs(idToPath('a')).yields(null, createFile('a'), ['b', 'e', 'h']);200 load.stub.withArgs(idToPath('b')).yields(null, createFile('b'), ['c', 'd']);201 load.stub.withArgs(idToPath('e')).yields(null, createFile('e'), ['f', 'g']);202 // load certain ids later203 ['b', 'e', 'h'].forEach(id => resolve.stub.withArgs(id).resetBehavior());204 resolve.stub.withArgs('h').func = (a, b, c, d, callback) => {205 callback(null, idToPath('h'));206 ['e', 'b'].forEach(207 id => resolve.stub.withArgs(id).yield(null, idToPath(id)));208 };209 graph(['a'], anyPlatform, noOpts, (error, result) => {210 expect(error).toEqual(null);211 expect(result.modules).toEqual([212 createModule('a', ['b', 'e', 'h']),213 createModule('b', ['c', 'd']),214 createModule('c'),215 createModule('d'),216 createModule('e', ['f', 'g']),217 createModule('f'),218 createModule('g'),219 createModule('h'),220 ]);221 done();222 });223 },224 );225 it('calls back with the resolved modules of the entry points', done => {226 load.stub.reset();227 resolve.stub.reset();228 load.stub.withArgs(idToPath('a')).yields(null, createFile('a'), ['b']);229 load.stub.withArgs(idToPath('b')).yields(null, createFile('b'), []);230 load.stub.withArgs(idToPath('c')).yields(null, createFile('c'), ['d']);231 load.stub.withArgs(idToPath('d')).yields(null, createFile('d'), []);232 'abcd'.split('')233 .forEach(id => resolve.stub.withArgs(id).yields(null, idToPath(id)));234 graph(['a', 'c'], anyPlatform, noOpts, (error, result) => {235 expect(result.entryModules).toEqual([236 createModule('a', ['b']),237 createModule('c', ['d']),238 ]);239 done();240 });241 });242 it('resolves modules for all entry points correctly if one is a dependency of another', done => {243 load.stub.reset();244 resolve.stub.reset();245 load.stub.withArgs(idToPath('a')).yields(null, createFile('a'), ['b']);246 load.stub.withArgs(idToPath('b')).yields(null, createFile('b'), []);247 'ab'.split('')248 .forEach(id => resolve.stub.withArgs(id).yields(null, idToPath(id)));249 graph(['a', 'b'], anyPlatform, noOpts, (error, result) => {250 expect(result.entryModules).toEqual([251 createModule('a', ['b']),252 createModule('b', []),253 ]);254 done();255 });256 });257 it('does not include dependencies more than once', done => {258 const ids = ['a', 'b', 'c', 'd'];259 ids.forEach(id => {260 const path = idToPath(id);261 resolve.stub.withArgs(id).yields(null, path);262 load.stub.withArgs(path).yields(null, createFile(id), []);263 });264 ['a', 'd'].forEach(id =>265 load.stub266 .withArgs(idToPath(id)).yields(null, createFile(id), ['b', 'c']));267 graph(['a', 'd', 'b'], anyPlatform, noOpts, (error, result) => {268 expect(error).toEqual(null);269 expect(result.modules).toEqual([270 createModule('a', ['b', 'c']),271 createModule('b'),272 createModule('c'),273 createModule('d', ['b', 'c']),274 ]);275 done();276 });277 });278 it('handles dependency cycles', done => {279 resolve.stub280 .withArgs('a').yields(null, idToPath('a'))281 .withArgs('b').yields(null, idToPath('b'))282 .withArgs('c').yields(null, idToPath('c'));283 load.stub284 .withArgs(idToPath('a')).yields(null, createFile('a'), ['b'])285 .withArgs(idToPath('b')).yields(null, createFile('b'), ['c'])286 .withArgs(idToPath('c')).yields(null, createFile('c'), ['a']);287 graph(['a'], anyPlatform, noOpts, (error, result) => {288 expect(result.modules).toEqual([289 createModule('a', ['b']),290 createModule('b', ['c']),291 createModule('c', ['a']),292 ]);293 done();294 });295 });296 it('can skip files', done => {297 ['a', 'b', 'c', 'd', 'e'].forEach(298 id => resolve.stub.withArgs(id).yields(null, idToPath(id)));299 load.stub300 .withArgs(idToPath('a')).yields(null, createFile('a'), ['b', 'c', 'd'])301 .withArgs(idToPath('b')).yields(null, createFile('b'), ['e']);302 ['c', 'd', 'e'].forEach(id =>303 load.stub.withArgs(idToPath(id)).yields(null, createFile(id), []));304 const skip = new Set([idToPath('b'), idToPath('c')]);305 graph(['a'], anyPlatform, {skip}, (error, result) => {306 expect(result.modules).toEqual([307 createModule('a', ['b', 'c', 'd']),308 createModule('d', []),309 ]);310 done();311 });312 });313});314function createDependency(id) {315 return {id, path: idToPath(id)};316}317function createFile(id) {...
cors_spec.js
Source:cors_spec.js
1var sinon = require('sinon'),2 should = require('should'),3 rewire = require('rewire'),4 configUtils = require('../../utils/configUtils'),5 cors = rewire('../../../server/middleware/cors');6describe('cors', function () {7 var res, req, next, sandbox;8 beforeEach(function () {9 sandbox = sinon.sandbox.create();10 req = {11 headers: {12 origin: null13 },14 client: {15 trustedDomains: []16 }17 };18 res = {19 headers: {},20 getHeader: function () {},21 setHeader: function (h, v) {22 this.headers[h] = v;23 }24 };25 next = sandbox.spy();26 });27 afterEach(function () {28 sandbox.restore();29 configUtils.restore();30 cors = rewire('../../../server/middleware/cors');31 });32 it('should not be enabled without a request origin header', function (done) {33 req.get = sinon.stub().withArgs('origin').returns(null);34 cors(req, res, next);35 next.called.should.be.true();36 should.not.exist(res.headers['Access-Control-Allow-Origin']);37 done();38 });39 it('should be enabled when origin is 127.0.0.1', function (done) {40 var origin = 'http://127.0.0.1:2368';41 req.get = sinon.stub().withArgs('origin').returns(origin);42 res.get = sinon.stub().withArgs('origin').returns(origin);43 req.headers.origin = origin;44 cors(req, res, next);45 next.called.should.be.true();46 res.headers['Access-Control-Allow-Origin'].should.equal(origin);47 done();48 });49 it('should be enabled when origin is localhost', function (done) {50 var origin = 'http://localhost:2368';51 req.get = sinon.stub().withArgs('origin').returns(origin);52 res.get = sinon.stub().withArgs('origin').returns(origin);53 req.headers.origin = origin;54 cors(req, res, next);55 next.called.should.be.true();56 res.headers['Access-Control-Allow-Origin'].should.equal(origin);57 done();58 });59 it('should be enabled when origin is a client_trusted_domain', function (done) {60 var origin = 'http://my-trusted-domain.com';61 req.client.trustedDomains.push({trusted_domain: origin});62 req.get = sinon.stub().withArgs('origin').returns(origin);63 res.get = sinon.stub().withArgs('origin').returns(origin);64 req.headers.origin = origin;65 cors(req, res, next);66 next.called.should.be.true();67 res.headers['Access-Control-Allow-Origin'].should.equal(origin);68 done();69 });70 it('should be enabled when there are multiple trusted domains', function (done) {71 var origin = 'http://my-other-trusted-domain.com';72 req.client.trustedDomains.push({trusted_domain: origin});73 req.client.trustedDomains.push({trusted_domain: 'http://my-trusted-domain.com'});74 req.get = sinon.stub().withArgs('origin').returns(origin);75 res.get = sinon.stub().withArgs('origin').returns(origin);76 req.headers.origin = origin;77 cors(req, res, next);78 next.called.should.be.true();79 res.headers['Access-Control-Allow-Origin'].should.equal(origin);80 done();81 });82 it('should not be enabled the origin is not trusted or whitelisted', function (done) {83 var origin = 'http://not-trusted.com';84 req.client.trustedDomains.push({trusted_domain: 'http://example.com'});85 req.get = sinon.stub().withArgs('origin').returns(origin);86 res.get = sinon.stub().withArgs('origin').returns(origin);87 req.headers.origin = origin;88 cors(req, res, next);89 next.called.should.be.true();90 should.not.exist(res.headers['Access-Control-Allow-Origin']);91 done();92 });93 it('should not be enabled the origin client_trusted_domains is empty', function (done) {94 var origin = 'http://example.com';95 req.get = sinon.stub().withArgs('origin').returns(origin);96 res.get = sinon.stub().withArgs('origin').returns(origin);97 req.headers.origin = origin;98 cors(req, res, next);99 next.called.should.be.true();100 should.not.exist(res.headers['Access-Control-Allow-Origin']);101 done();102 });103 it('should be enabled if the origin matches config.url', function (done) {104 var origin = 'http://my.blog';105 configUtils.set({106 url: origin107 });108 req.get = sinon.stub().withArgs('origin').returns(origin);109 res.get = sinon.stub().withArgs('origin').returns(origin);110 req.headers.origin = origin;111 cors(req, res, next);112 next.called.should.be.true();113 res.headers['Access-Control-Allow-Origin'].should.equal(origin);114 done();115 });116 it('should be enabled if the origin matches config.urlSSL', function (done) {117 var origin = 'https://secure.blog';118 configUtils.set({119 url: 'http://my.blog',120 urlSSL: origin121 });122 req.get = sinon.stub().withArgs('origin').returns(origin);123 res.get = sinon.stub().withArgs('origin').returns(origin);124 req.headers.origin = origin;125 cors(req, res, next);126 next.called.should.be.true();127 res.headers['Access-Control-Allow-Origin'].should.equal(origin);128 done();129 });...
game.spec.js
Source:game.spec.js
...36 it.skip('throws an error when no cards are passed', () => {37 expect(new Game).to.throw(Error)38 });39 it('should return a question which matches the answer and fact', () => {40 stub.withArgs(0,1,1).returns([0]);41 stub.withArgs(0,7,1).returns([2]);42 expect(new Game(cards).question()).to.equal(`Who's key2 is 2?`);43 stub.withArgs(0,1,1).returns([1]);44 stub.withArgs(0,7,1).returns([2]);45 expect(new Game(cards).question()).to.equal(`Who's key2 is 21?`);46 stub.withArgs(0,1,1).returns([1]);47 stub.withArgs(0,7,1).returns([3]);48 expect(new Game(cards).question()).to.equal(`Who's key3 is 31?`);49 });50 it('should return taller/smaller when the answer key is height', () => {51 stub.withArgs(0,1,1).returns([0]);52 stub.withArgs(0,7,1).returns([6]);53 expect(new Game(cards).question()).to.equal(`Who's height is smaller, card1 or card2?`);54 stub.withArgs(0,1,1).returns([1]);55 stub.withArgs(0,7,1).returns([6]);56 expect(new Game(cards).question()).to.equal(`Who's height is taller, card1 or card2?`);57 });58 it('should return answer `both` when both cards matches the answer and fact', () => {59 stub.withArgs(0,1,1).returns([0]);60 stub.withArgs(0,7,1).returns([4]);61 expect(new Game(cards).answer()).to.equal(`both`);62 stub.withArgs(0,1,1).returns([1]);63 stub.withArgs(0,7,1).returns([4]);64 expect(new Game(cards).answer()).to.equal(`both`);65 stub.withArgs(0,1,1).returns([1]);66 stub.withArgs(0,7,1).returns([5]);67 expect(new Game(cards).answer()).to.equal(`both`);68 });69 it('should return answer key `name`', () => {70 stub.withArgs(0,1,1).returns([0]);71 stub.withArgs(0,7,1).returns([1]);72 expect(new Game(cards).answer()).to.equal(`card1`)73 stub.withArgs(0,1,1).returns([1]);74 stub.withArgs(0,7,1).returns([2]);75 expect(new Game(cards).answer()).to.equal(`card2`)76 });77 it('should return answer unknown when wrong answer value is unknown', () => {78 stub.withArgs(0,1,1).returns([0]);79 stub.withArgs(0,7,1).returns([1]);80 expect(new Game(cards).answer()).to.equal(`card1`)81 stub.withArgs(0,1,1).returns([1]);82 stub.withArgs(0,7,1).returns([1]);83 expect(new Game(cards).answer()).to.equal(`unknown`)84 stub.withArgs(0,1,1).returns([1]);85 stub.withArgs(0,7,1).returns([7]);86 expect(new Game(cards).answer()).to.equal(`both`)87 });88 it('should return the correct answer card', () => {89 stub.withArgs(0,1,1).returns([0]);90 stub.withArgs(0,7,1).returns([1]);91 expect(new Game(cards).answerId()).to.equal(fakeCard1.url)92 stub.withArgs(0,1,1).returns([1]);93 stub.withArgs(0,7,1).returns([1]);94 expect(new Game(cards).answerId()).to.equal(fakeCard2.url)95 });...
Using AI Code Generation
1var stub = sinon.stub();2stub.withArgs(42).returns(1);3stub.withArgs(43).throws('TypeError');4var stub = sinon.stub();5stub.onCall(0).returns(1);6stub.onCall(1).throws('TypeError');7stub.onCall(2).returns(42);8var stub = sinon.stub();9stub.onFirstCall().returns(1);10stub.onSecondCall().throws('TypeError');11stub.onThirdCall().returns(42);12var stub = sinon.stub();13stub.withArgs(42).onCall(0).returns(1);14stub.withArgs(42).onCall(1).throws('TypeError');15stub.withArgs(42).onCall(2).returns(42);16var stub = sinon.stub();17stub.withArgs(42).onFirstCall().returns(1);18stub.withArgs(42).onSecondCall().throws('TypeError');19stub.withArgs(42).onThirdCall().returns(42);20var stub = sinon.stub();21stub.returnsArg(0);22var stub = sinon.stub();23stub.returnsThis();
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3var stub = sinon.stub();4stub.withArgs(1).returns('one');5stub.withArgs(2).returns('two');6assert.equal(stub(1), 'one');7assert.equal(stub(2), 'two');8assert.equal(stub(3), undefined);9assert.equal(stub(4), undefined);10assert.equal(stub(5), undefined);11var sinon = require('sinon');12var assert = require('assert');13var stub = sinon.stub();14stub.withArgs(1).returns('one');15stub.withArgs(2).returns('two');16assert.equal(stub(1), 'one');17assert.equal(stub(2), 'two');18assert.equal(stub(3), undefined);19assert.equal(stub(4), undefined);20assert.equal(stub(5), undefined);21var sinon = require('sinon');22var assert = require('assert');23var stub = sinon.stub();24stub.withArgs(1).returns('one');25stub.withArgs(2).returns('two');26assert.equal(stub(1), 'one');27assert.equal(stub(2), 'two');28assert.equal(stub(3), undefined);29assert.equal(stub(4), undefined);30assert.equal(stub(5), undefined);31var sinon = require('sinon');32var assert = require('assert');33var stub = sinon.stub();34stub.withArgs(1).returns('one');35stub.withArgs(2).returns('two');36assert.equal(stub(1), 'one');37assert.equal(stub(2), 'two');38assert.equal(stub(3), undefined);39assert.equal(stub(4), undefined);40assert.equal(stub(5), undefined);41var sinon = require('sinon');42var assert = require('assert');43var stub = sinon.stub();44stub.withArgs(1).returns('one');45stub.withArgs(2).returns('two');46assert.equal(stub(1), 'one');47assert.equal(stub(2), 'two');48assert.equal(stub(3), undefined);49assert.equal(stub(4), undefined);50assert.equal(stub(5), undefined);
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3var stub = sinon.stub();4stub.withArgs('foo').returns(42);5stub.withArgs('bar').throws(new Error('bar'));6assert.equal(stub('foo'), 42);7assert.throws(stub.bind(null, 'bar'), Error);8var sinon = require('sinon');9var assert = require('assert');10var stub = sinon.stub();11stub.withArgs('foo').returns(42);12stub.withArgs('bar').throws(new Error('bar'));13assert.equal(stub('foo'), 42);14assert.throws(stub.bind(null, 'bar'), Error);15var sinon = require('sinon');16var assert = require('assert');17var stub = sinon.stub();18stub.withArgs('foo').returns(42);19stub.withArgs('bar').throws(new Error('bar'));20assert.equal(stub('foo'), 42);21assert.throws(stub.bind(null, 'bar'), Error);22var sinon = require('sinon');23var assert = require('assert');24var stub = sinon.stub();25stub.withArgs('foo').returns(42);26stub.withArgs('bar').throws(new Error('bar'));27assert.equal(stub('foo'), 42);28assert.throws(stub.bind(null, 'bar'), Error);29var sinon = require('sinon');30var assert = require('assert');31var stub = sinon.stub();32stub.withArgs('foo').returns(42);33stub.withArgs('bar').throws(new Error('bar'));34assert.equal(stub('foo'), 42);35assert.throws(stub.bind(null, 'bar'), Error);36var sinon = require('sinon');37var assert = require('assert');38var stub = sinon.stub();39stub.withArgs('foo').returns(42);40stub.withArgs('bar').throws(new Error('bar'));41assert.equal(stub('foo'), 42);42assert.throws(stub.bind(null, 'bar'), Error);43var sinon = require('sinon');44var assert = require('assert');45var stub = sinon.stub();46stub.withArgs('foo').returns(42
Using AI Code Generation
1var assert = require('chai').assert;2var sinon = require('sinon');3var myModule = require('../myModule');4describe('myModule', function() {5 it('should call the callback with the correct arguments', function() {6 var stub = sinon.stub(myModule, 'myMethod');7 stub.withArgs('foo', 'bar').callsArgWith(2, 'baz');8 var callback = sinon.spy();9 myModule.myMethod('foo', 'bar', callback);10 assert(callback.calledWith('baz'));11 });12});
Using AI Code Generation
1var sinon = require('sinon');2var chai = require('chai');3var assert = chai.assert;4var foo = {5 bar: function (param) {6 if (param) {7 return 'bar';8 } else {9 return 'baz';10 }11 }12};13describe('foo', function () {14 describe('#bar', function () {15 it('should return "bar" when called with true', function () {16 var stub = sinon.stub(foo, 'bar');17 stub.withArgs(true).returns('bar');18 assert.equal(foo.bar(true), 'bar');19 });20 });21});22var sinon = require('sinon');23var chai = require('chai');24var assert = chai.assert;25var foo = {26 bar: function (param) {27 if (param) {28 return 'bar';29 } else {30 return 'baz';31 }32 }33};34describe('foo', function () {35 describe('#bar', function () {36 it('should return "bar" when called with true', function () {37 var stub = sinon.stub(foo, 'bar');38 stub.withArgs(true).returns('bar');39 assert.equal(foo.bar(true), 'bar');40 });41 });42});43var sinon = require('sinon');44var chai = require('chai');45var assert = chai.assert;46var foo = {47 bar: function (param) {48 if (param) {49 return 'bar';50 } else {51 return 'baz';52 }53 }54};55describe('foo', function () {56 describe('#bar', function () {57 it('should return "bar" when called with true', function () {58 var stub = sinon.stub(foo, 'bar');59 stub.withArgs(true).returns('bar');60 assert.equal(foo.bar(true), 'bar');61 });62 });63});64var sinon = require('sinon');65var chai = require('chai');66var assert = chai.assert;67var foo = {68 bar: function (param) {69 if (param) {70 return 'bar';71 } else {72 return 'baz';73 }74 }75};76describe('foo', function () {77 describe('#bar', function () {
Using AI Code Generation
1var assert = require('assert');2var sinon = require('sinon');3var MyClass = require('./myclass');4describe('MyClass', function() {5 describe('#myMethod', function() {6 it('should stub myMethod with arguments', function() {7 var myClass = new MyClass();8 var stub = sinon.stub(myClass, 'myMethod').withArgs('foo', 'bar').returns('baz');9 assert.equal(myClass.myMethod('foo', 'bar'), 'baz');10 assert.equal(myClass.myMethod('foo', 'baz'), undefined);11 stub.restore();12 });13 });14});15var MyClass = function() {16};17MyClass.prototype.myMethod = function(a, b) {18 return a + b;19};20module.exports = MyClass;21 1 passing (8ms)22var assert = require('assert');23var sinon = require('sinon');24var MyClass = require('./myclass');25describe('MyClass', function() {26 describe('#myMethod', function() {27 it('should stub myMethod with arguments that is called multiple times', function() {28 var myClass = new MyClass();29 var stub = sinon.stub(myClass, 'myMethod').withArgs('foo', 'bar').returns('baz');30 assert.equal(myClass.myMethod('foo', 'bar'), 'baz');31 assert.equal(myClass.myMethod('foo', 'bar'), 'baz');32 assert.equal(myClass.myMethod('foo', 'baz'), undefined);33 stub.restore();34 });35 });36});37var MyClass = function() {38};39MyClass.prototype.myMethod = function(a, b) {40 return a + b;41};42module.exports = MyClass;
Using AI Code Generation
1var myModule = require('./module.js');2describe('myModule', function() {3 it('should return 3 when passed 1 and 2', function() {4 var stub = sinon.stub(myModule, 'add');5 stub.withArgs(1, 2).returns(3);6 var result = myModule.add(1, 2);7 expect(result).to.equal(3);8 });9 it('should return 4 when passed 2 and 2', function() {10 var result = myModule.add(2, 2);11 expect(result).to.equal(4);12 });13});14module.exports = {15 add: function(a, b) {16 return a + b;17 }18};19var myModule = require('./module.js');20describe('myModule', function() {21 it('should return 3 when passed 1 and 2', function() {22 var stub = sinon.stub(myModule, 'add');23 stub.withArgs(1, 2).returns(3);24 var result = myModule.add(1, 2);25 expect(result).to.equal(3);26 });27 it('should return 4 when passed 2 and 2', function() {28 var result = myModule.add(2, 2);29 expect(result).to.equal(4);30 });31});32module.exports = {33 add: function(a, b) {34 return a + b;35 }36};37var myModule = require('./module.js');38describe('myModule', function() {39 it('should return 3 when passed 1 and 2', function() {40 var stub = sinon.stub(myModule, 'add');41 stub.withArgs(1, 2).returns(3);42 var result = myModule.add(1, 2);43 expect(result).to.equal(3);44 });45 it('should return 4 when passed 2 and 2', function() {46 var result = myModule.add(2, 2);47 expect(result).to.equal(4);48 });49});50module.exports = {
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!!