Best JavaScript code snippet using sinon
test.js
Source:test.js
1'use strict';2require('co-mocha');3var sinon = require('sinon');4var assertSpy = sinon.assert;5var assert = require('assert');6var proxyquire = require('proxyquire');7var promiseResolver = require('./');8var noPromiseResolver = proxyquire('./', {'native-or-bluebird/promise': false});9var resolve;10var reject;11var cb;12var resolver;13var PromiseStub;14function timeout(delay) {15 return new Promise(resolve => setTimeout(resolve, typeof delay === 'number' ? delay : 30));16}17beforeEach(() => {18 resolve = sinon.spy();19 reject = sinon.spy();20 cb = sinon.spy();21 PromiseStub = sinon.spy(function (fn) {22 fn(resolve, reject);23 return {24 then: sinon.spy(),25 catch: sinon.spy()26 };27 });28});29it('rejects with first argument', function () {30 resolver = promiseResolver(resolve, reject);31 var err = new Error('err');32 resolver(err, 'hello!');33 assertSpy.notCalled(resolve);34 assertSpy.calledOnce(reject);35 assertSpy.calledWith(reject, err);36});37it('resolves with second argument', function () {38 resolver = promiseResolver(resolve, reject);39 resolver(null, 'howdy!');40 assertSpy.notCalled(reject);41 assertSpy.calledOnce(resolve);42 assertSpy.calledWith(resolve, 'howdy!');43});44it('calls callback with error', function *() {45 resolver = promiseResolver(null, null, cb);46 var err = new Error('err');47 resolver(err, 'hello!');48 yield timeout();49 assertSpy.calledOnce(cb);50 assertSpy.calledWith(cb, err, 'hello!');51});52it('calls callback with result', function *() {53 resolver = promiseResolver(null, null, cb);54 resolver(null, 'hello!');55 yield timeout();56 assertSpy.calledOnce(cb);57 assertSpy.calledWith(cb, null, 'hello!');58});59it('calls reject and callback with error', function *() {60 resolver = promiseResolver(resolve, reject, cb);61 var err = new Error('err');62 resolver(err, 'hello!');63 yield timeout();64 assertSpy.notCalled(resolve);65 assertSpy.calledOnce(reject);66 assertSpy.calledWith(reject, err);67 assertSpy.calledOnce(cb);68 assertSpy.calledWith(cb, err, 'hello!');69});70it('calls resolve and callback with result', function *() {71 resolver = promiseResolver(resolve, reject, cb);72 resolver(null, 'goodbye!');73 yield timeout();74 assertSpy.notCalled(reject);75 assertSpy.calledOnce(resolve);76 assertSpy.calledWith(resolve, 'goodbye!');77 assertSpy.calledOnce(cb);78 assertSpy.calledWith(cb, null, 'goodbye!');79});80it('will only call cb once', function *() {81 resolver = promiseResolver(resolve, reject, cb);82 resolver(null, 'goodbye!');83 yield timeout();84 assertSpy.calledOnce(cb);85 resolver(null, 'hello!');86 yield timeout();87 assertSpy.calledOnce(cb);88 assertSpy.calledWith(cb, null, 'goodbye!');89});90it('defer() creates a defer object', function *() {91 var defer = promiseResolver.defer(cb, PromiseStub);92 resolver = defer.cb;93 assert.strictEqual(typeof defer.resolve, 'function');94 assert.strictEqual(typeof defer.reject, 'function');95 assert.strictEqual(typeof defer.promise.then, 'function');96 assert.strictEqual(typeof defer.promise.catch, 'function');97 resolver(null, 'goodbye!');98 yield timeout();99 assertSpy.notCalled(reject);100 assertSpy.calledOnce(resolve);101 assertSpy.calledWith(resolve, 'goodbye!');102 assertSpy.calledOnce(cb);103 assertSpy.calledWith(cb, null, 'goodbye!');104});105it('defer() suppresses unhandledRejection if cb is provided', function *() {106 var spy = sinon.spy();107 process.once('unhandledRejection', spy);108 promiseResolver.defer(cb).cb(new Error('hello'));109 yield timeout();110 assertSpy.notCalled(spy);111});112it('defer() does NOT suppress unhandledRejection if cb is NOT provided', function *() {113 var spy = sinon.spy();114 process.once('unhandledRejection', spy);115 promiseResolver.defer().cb(new Error('hello'));116 yield timeout();117 assertSpy.called(spy);118});119it('defer().reject will call callback with error as first arg', function *() {120 promiseResolver.defer(cb).reject(new Error('defer.reject'));121 yield timeout();122 assertSpy.calledWith(cb, sinon.match.has('message', 'defer.reject'));123});124it('defer().resolve will result to callback as second arg', function *() {125 promiseResolver.defer(cb).resolve('hello');126 yield timeout();127 assertSpy.calledWith(cb, null, 'hello');128});129it('defer().resolve works using a callback even if there is no installed promise implementation', function *() {130 noPromiseResolver.defer(cb).resolve('hello');131 yield timeout();132 assertSpy.calledWith(cb, null, 'hello');133});134it('defer().reject works using a callback even if there is no installed promise implementation', function *() {135 noPromiseResolver.defer(cb).reject(new Error('uh oh'));136 yield timeout();137 assertSpy.calledWith(cb, sinon.match.has('message', 'uh oh'));138});139it('defer().reject(null) rejects a promise, even though error is falsie', function *() {140 promiseResolver.defer(cb, PromiseStub).reject(null);141 yield timeout();142 assertSpy.calledWith(cb, sinon.match.has('message', 'reject called with falsie reason'));143 assertSpy.calledWith(reject, sinon.match.has('message', 'reject called with falsie reason'));144});145it('defer() will throw without an installed promise implementation or supplied callback', function () {146 assert.throws(function () {147 noPromiseResolver.defer();148 }, /No Promise Implementation/);...
helpers.js
Source:helpers.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');14describe('Passes IOC container', function() {15 beforeEach(function() {16 test.$di17 .set('spy', test.spy())18 .provider('assertSpy', 'spy', function(spy) {19 return function(num) {20 test.assert(spy.callCount === num);21 return test;22 };23 })24 .set('callSpy', function() {25 test.$di.get('spy');26 return test;27 })28 ;29 });30 afterEach(function() {31 test.$di32 .remove('spy')33 .remove('assertSpy')34 .remove('callSpy')35 ;36 });37 it('case()', function() {38 test39 .case(function() {40 test.object(this)41 .isIdenticalTo(test.$di._container);42 this.callSpy();43 })44 .$di.get('assertSpy')(1)45 .then('control flow')46 .object({})47 .case(function() {48 test.object(this)49 .isIdenticalTo(test.$di._container);50 this.callSpy();51 })52 .$di.get('assertSpy')(2)53 ;54 });55 it('given()', function() {56 test57 .given(function() {58 test.object(this)59 .isIdenticalTo(test.$di._container);60 this.callSpy();61 })62 .$di.get('assertSpy')(1)63 .then('control flow')64 .object({})65 .given(function() {66 test.object(this)67 .isIdenticalTo(test.$di._container);68 this.callSpy();69 })70 .$di.get('assertSpy')(2)71 ;72 });73 it('when()', function() {74 test75 .when(function() {76 test.object(this)77 .isIdenticalTo(test.$di._container);78 this.callSpy();79 })80 .$di.get('assertSpy')(1)81 .then('control flow')82 .object({})83 .when(function() {84 test.object(this)85 .isIdenticalTo(test.$di._container);86 this.callSpy();87 })88 .$di.get('assertSpy')(2)89 ;90 });91 it('then()', function() {92 test93 .then(function() {94 test.object(this)95 .isIdenticalTo(test.$di._container);96 this.callSpy();97 })98 .$di.get('assertSpy')(1)99 .then('control flow')100 .object({})101 .then(function() {102 test.object(this)103 .isIdenticalTo(test.$di._container);104 this.callSpy();105 })106 .$di.get('assertSpy')(2)107 ;108 });109 it('if()', function() {110 test111 .if(function() {112 test.object(this)113 .isIdenticalTo(test.$di._container);114 this.callSpy();115 })116 .$di.get('assertSpy')(1)117 .then('control flow')118 .object({})119 .if(function() {120 test.object(this)121 .isIdenticalTo(test.$di._container);122 this.callSpy();123 })124 .$di.get('assertSpy')(2)125 ;126 });127 it('and()', function() {128 test129 .and(function() {130 test.object(this)131 .isIdenticalTo(test.$di._container);132 this.callSpy();133 })134 .$di.get('assertSpy')(1)135 .then('control flow')136 .object({})137 .and(function() {138 test.object(this)139 .isIdenticalTo(test.$di._container);140 this.callSpy();141 })142 .$di.get('assertSpy')(2)143 ;144 });145 it('wait()', function(done){146 var spy = test.spy(function() {147 calledAt = new Date();148 calledAt = calledAt.getTime();149 });150 var defAt = new Date();151 var calledAt;152 test.wait(20, spy);153 setTimeout(function(){154 test.assert(spy.calledOnce);155 defAt = defAt.getTime();156 test.number(calledAt)157 .isGreaterThan(defAt + 18);158 done();159 }, 25);160 });161 it('dump()', function() {162 var log = console.log;163 console.log = test.spy();164 test165 .string('1')166 .dump('test.dump()')167 .is('1')168 .bool(console.log.called)169 .isTrue()170 ;171 console.log = log;172 });173 it('stats', function() {174 var countAssert, countAssertOk, total;175 test176 .object(test.stats)177 .object(test.stats.assertions)178 .object(test.stats.total)179 .number(test.stats.total.assertions)180 .isGreaterThan(2)181 .number(test.stats.assertions.isObject)182 .isGreaterThan(1)183 .number(test.stats.assertions.isNumber)184 .isGreaterThan(1)185 .case('assert', function() {186 total = test.stats.total.assertions;187 countAssert = test.stats.assertions.assert || 0;188 countAssertOk = test.stats.assertions['assert.ok'] || 0;189 test.assert(true);190 test.assert.ok(true);191 test192 .number(test.stats.total.assertions)193 .is(total + 2)194 .number(test.stats.assertions.assert)195 .is(countAssert + 1)196 .number(test.stats.assertions['assert.ok'])197 .is(countAssertOk + 1)198 ;199 })200 ;201 });...
Using AI Code Generation
1var assert = require("assert");2var sinon = require("sinon");3var obj = { func: function() {} };4var spy = sinon.spy(obj, "func");5obj.func(42);6assert(spy.calledWith(42));7var assert = require("assert");8var sinon = require("sinon");9var obj = { func: function() {} };10var spy = sinon.spy(obj, "func");11obj.func(42);12assert(spy.calledWith(42));13var assert = require("assert");14var sinon = require("sinon");15var obj = { func: function() {} };16var spy = sinon.spy(obj, "func");17obj.func(42);18assert(spy.calledWith(42));19var assert = require("assert");20var sinon = require("sinon");21var obj = { func: function() {} };22var spy = sinon.spy(obj, "func");23obj.func(42);24assert(spy.calledWith(42));25var assert = require("assert");26var sinon = require("sinon");27var obj = { func: function() {} };28var spy = sinon.spy(obj, "func");29obj.func(42);30assert(spy.calledWith(42));31var assert = require("assert");32var sinon = require("sinon");33var obj = { func: function() {} };34var spy = sinon.spy(obj, "func");35obj.func(42);36assert(spy.calledWith(42));37var assert = require("assert");38var sinon = require("sinon");39var obj = { func: function() {} };40var spy = sinon.spy(obj, "func");41obj.func(42);42assert(spy.calledWith(42));43var assert = require("assert");44var sinon = require("sinon");45var obj = { func: function() {} };46var spy = sinon.spy(obj, "func");47obj.func(42);48assert(spy.calledWith(42));
Using AI Code Generation
1var assertSpy = require('sinon').assertSpy;2var spy = sinon.spy();3spy(1,2,3);4assertSpy(spy);5var sinon = require('sinon');6var spy = sinon.spy();7spy(1,2,3);8sinon.assertSpy(spy);9var sinon = require('sinon');10var spy = sinon.spy();11spy(1,2,3);12sinon.assert.spy(spy);13var sinon = require('sinon');14var spy = sinon.spy();15spy(1,2,3);16sinon.assert.spy.calledOnce(spy);17var assertSpy = require('sinon').assertSpy;18var spy = sinon.spy();19spy(1,2,3);20assertSpy(spy);21var sinon = require('sinon');22var spy = sinon.spy();23spy(1,2,3);24sinon.assertSpy(spy);25var sinon = require('sinon');26var spy = sinon.spy();27spy(1,2,3);28sinon.assert.spy(spy);29var sinon = require('sinon');30var spy = sinon.spy();31spy(1,2,3);32sinon.assert.spy.calledOnce(spy);33var assertSpy = require('sinon').assertSpy;34var spy = sinon.spy();35spy(1,2,3);36assertSpy(spy);37var sinon = require('sinon');38var spy = sinon.spy();39spy(1,2,3);40sinon.assertSpy(spy);41var sinon = require('sinon');42var spy = sinon.spy();43spy(1,2,3);44sinon.assert.spy(spy);45var sinon = require('sinon');46var spy = sinon.spy();47spy(1,2,3);48sinon.assert.spy.calledOnce(spy);
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3var test = require('./test');4var spy = sinon.spy(test, 'test');5test.test(1);6assert(spy.calledOnce);7assert(spy.calledWith(1));8assert(spy.calledWith(2) === false);9assert(spy.calledWith(1, 2) === false);10spy.restore();11assert(test.test(1) === 1);12var test = {13 test: function (a) {14 return a;15 }16};17module.exports = test;
Using AI Code Generation
1var assert = require('chai').assert;2var sinon = require('sinon');3var spy = sinon.spy();4assert.spy(spy);5var assert = require('chai').assert;6var sinon = require('sinon');7var spy = sinon.spy();8assert.spy(spy);9var assert = require('chai').assert;10var sinon = require('sinon');11var spy = sinon.spy();12assert.spy(spy);13var assert = require('chai').assert;14var sinon = require('sinon');15var spy = sinon.spy();16assert.spy(spy);17var assert = require('chai').assert;18var sinon = require('sinon');19var spy = sinon.spy();20assert.spy(spy);21var assert = require('chai').assert;22var sinon = require('sinon');23var spy = sinon.spy();24assert.spy(spy);25var assert = require('chai').assert;26var sinon = require('sinon');27var spy = sinon.spy();28assert.spy(spy);29var assert = require('chai').assert;30var sinon = require('sinon');31var spy = sinon.spy();32assert.spy(spy);33var assert = require('chai').assert;34var sinon = require('sinon
Using AI Code Generation
1var assertSpy = sinon.assert.spy;2var assertSpy = sinon.assert.spy;3var assertSpy = sinon.assert.spy;4var assertSpy = sinon.assert.spy;5var assertSpy = sinon.assert.spy;6var assertSpy = sinon.assert.spy;7var assertSpy = sinon.assert.spy;8var assertSpy = sinon.assert.spy;9var assertSpy = sinon.assert.spy;10var assertSpy = sinon.assert.spy;11var assertSpy = sinon.assert.spy;
Using AI Code Generation
1const sinon = require('sinon');2const assert = require('assert');3const { test, assertSpy } = require('sinon');4const axios = require('axios');5describe('Test API', () => {6 it('should return data', async () => {7 const data = { foo: 'bar' };8 const get = sinon.stub(axios, 'get');9 get.resolves({ data });10 assert.equal(response.data, data);11 assertSpy(get).calledOnce();12 });13});
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!!