How to use spy.named method in sinon

Best JavaScript code snippet using sinon

hook-runner.js

Source:hook-runner.js Github

copy

Full Screen

1'use strict';2const Promise = require('bluebird');3const HookRunner = require('lib/worker/runner/test-runner/hook-runner');4const ExecutionThread = require('lib/worker/runner/test-runner/execution-thread');5const {Suite, Test} = require('../../../_mocha');6describe('worker/runner/test-runner/hook-runner', () => {7 const sandbox = sinon.sandbox.create();8 const mkRunner_ = (opts = {}) => {9 const test = opts.test || Test.create(Suite.create());10 return HookRunner.create(test, Object.create(ExecutionThread.prototype));11 };12 beforeEach(() => {13 sandbox.stub(ExecutionThread.prototype, 'run').callsFake(async (runnable) => {14 await runnable.fn();15 });16 });17 afterEach(() => sandbox.restore());18 describe('runBeforeEachHooks', () => {19 it('should run hook in execution thread', async () => {20 const test = Test.create();21 const suite = Suite.create()22 .beforeEach(() => {})23 .addTest(test);24 await mkRunner_({test}).runBeforeEachHooks();25 assert.calledWithMatch(ExecutionThread.prototype.run, suite.beforeEachHooks[0]);26 });27 it('should protect hook from modification during run in execution thread', async () => {28 const test = Test.create();29 const suite = Suite.create()30 .beforeEach(() => {})31 .addTest(test);32 ExecutionThread.prototype.run.callsFake((hook) => hook.foo = 'bar');33 await mkRunner_({test}).runBeforeEachHooks();34 assert.notProperty(suite.beforeEachHooks[0], 'foo');35 });36 it('should call beforeEach hooks for all parents', async () => {37 const topLevelHook1 = sinon.spy().named('topLevelHook1');38 const topLevelHook2 = sinon.spy().named('topLevelHook2');39 const hook1 = sinon.spy().named('hook1');40 const hook2 = sinon.spy().named('hook2');41 const test = Test.create();42 Suite.create()43 .beforeEach(topLevelHook1)44 .beforeEach(topLevelHook2)45 .addSuite(46 Suite.create()47 .beforeEach(hook1)48 .beforeEach(hook2)49 .addTest(test)50 );51 await mkRunner_({test}).runBeforeEachHooks();52 assert.callOrder(53 topLevelHook1,54 topLevelHook2,55 hook1,56 hook257 );58 });59 it('should wait until first hook finished before next hook started', async () => {60 const afterFirstHook = sinon.spy().named('afterFirstHook');61 const firstHook = sinon.stub().callsFake(() => Promise.delay(10).then(afterFirstHook));62 const secondHook = sinon.spy().named('secondHook');63 const test = Test.create();64 Suite.create()65 .beforeEach(firstHook)66 .beforeEach(secondHook)67 .addTest(test);68 await mkRunner_({test}).runBeforeEachHooks();69 assert.callOrder(70 afterFirstHook,71 secondHook72 );73 });74 it('should wait until parent hook finished before child hook start', async () => {75 const afterParentHook = sinon.spy().named('afterParentHook');76 const parentHook = sinon.stub().callsFake(() => Promise.delay(10).then(afterParentHook));77 const childHook = sinon.spy().named('childHook');78 const test = Test.create();79 Suite.create()80 .beforeEach(parentHook)81 .addSuite(82 Suite.create()83 .beforeEach(childHook)84 .addTest(test)85 );86 await mkRunner_({test}).runBeforeEachHooks();87 assert.callOrder(88 afterParentHook,89 childHook90 );91 });92 it('should fail on first hook fail', async () => {93 const secondParentHook = sinon.spy().named('secondParentHook');94 const childHook = sinon.spy().named('childHook');95 const test = Test.create();96 Suite.create()97 .beforeEach(() => Promise.reject(new Error('foo')))98 .beforeEach(secondParentHook)99 .addSuite(100 Suite.create()101 .beforeEach(childHook)102 .addTest(test)103 );104 const runner = mkRunner_({test});105 await assert.isRejected(runner.runBeforeEachHooks(), /foo/);106 assert.notCalled(secondParentHook);107 assert.notCalled(childHook);108 });109 });110 describe('runAfterEachHooks', () => {111 it('should run hook in execution thread', async () => {112 const test = Test.create();113 const suite = Suite.create()114 .afterEach(() => {})115 .addTest(test);116 await mkRunner_({test}).runAfterEachHooks();117 assert.calledWithMatch(ExecutionThread.prototype.run, suite.afterEachHooks[0]);118 });119 it('should protect hook from modification during run in execution thread', async () => {120 const test = Test.create();121 const suite = Suite.create()122 .afterEach(() => {})123 .addTest(test);124 ExecutionThread.prototype.run.callsFake((hook) => hook.foo = 'bar');125 await mkRunner_({test}).runAfterEachHooks();126 assert.notProperty(suite.afterEachHooks[0], 'foo');127 });128 it('should call afterEach hooks for all parents', async () => {129 const topLevelHook1 = sinon.spy().named('topLevelHook1');130 const topLevelHook2 = sinon.spy().named('topLevelHook2');131 const hook1 = sinon.spy().named('hook1');132 const hook2 = sinon.spy().named('hook2');133 const test = Test.create();134 Suite.create()135 .afterEach(topLevelHook1)136 .afterEach(topLevelHook2)137 .addSuite(138 Suite.create()139 .afterEach(hook1)140 .afterEach(hook2)141 .addTest(test)142 );143 await mkRunner_({test}).runAfterEachHooks();144 assert.callOrder(145 hook1,146 hook2,147 topLevelHook1,148 topLevelHook2149 );150 });151 it('should wait until first hook finished before next hook started', async () => {152 const afterFirstHook = sinon.spy().named('afterFirstHook');153 const firstHook = sinon.stub().callsFake(() => Promise.delay(10).then(afterFirstHook));154 const secondHook = sinon.spy().named('secondHook');155 const test = Test.create();156 Suite.create()157 .afterEach(firstHook)158 .afterEach(secondHook)159 .addTest(test);160 await mkRunner_({test}).runAfterEachHooks();161 assert.callOrder(162 afterFirstHook,163 secondHook164 );165 });166 it('should wait until child hook finished before parent hook start', async () => {167 const afterChildHook = sinon.spy().named('afterChildHook');168 const childHook = sinon.stub().callsFake(() => Promise.delay(10).then(afterChildHook));169 const parentHook = sinon.spy().named('parentHook');170 const test = Test.create();171 Suite.create()172 .afterEach(parentHook)173 .addSuite(174 Suite.create()175 .afterEach(childHook)176 .addTest(test)177 );178 await mkRunner_({test}).runAfterEachHooks();179 assert.callOrder(180 afterChildHook,181 parentHook182 );183 });184 it('should fail on first hook fail in suite', async () => {185 const secondHook = sinon.spy().named('secondHook');186 const test = Test.create();187 Suite.create()188 .afterEach(() => Promise.reject(new Error('foo')))189 .afterEach(secondHook)190 .addTest(test);191 const runner = mkRunner_({test});192 await assert.isRejected(runner.runAfterEachHooks(), /foo/);193 assert.notCalled(secondHook);194 });195 it('should run parent hook even if child hook failed', async () => {196 const parentHook = sinon.spy().named('parentHook');197 const test = Test.create();198 Suite.create()199 .afterEach(parentHook)200 .addSuite(201 Suite.create()202 .afterEach(() => Promise.reject(new Error('foo')))203 .addTest(test)204 );205 const runner = mkRunner_({test});206 await assert.isRejected(runner.runAfterEachHooks(), /foo/);207 assert.calledOnce(parentHook);208 });209 it('should reject with child hook error if both child and parent hooks failed', async () => {210 const test = Test.create();211 Suite.create()212 .afterEach(() => Promise.reject(new Error('bar')))213 .addSuite(214 Suite.create()215 .afterEach(() => Promise.reject(new Error('foo')))216 .addTest(test)217 );218 const runner = mkRunner_({test});219 await assert.isRejected(runner.runAfterEachHooks(), /foo/);220 });221 it('should run only parent afterEach hook if parent beforeEach hook failed', async () => {222 const parentHook = sinon.spy().named('parentHook');223 const childHook = sinon.spy().named('childHook');224 const test = Test.create();225 Suite.create()226 .beforeEach(() => Promise.reject(new Error()))227 .afterEach(parentHook)228 .addSuite(229 Suite.create()230 .afterEach(childHook)231 .addTest(test)232 );233 const runner = mkRunner_({test});234 await runner.runBeforeEachHooks().catch(() => {});235 await runner.runAfterEachHooks();236 assert.calledOnce(parentHook);237 assert.notCalled(childHook);238 });239 });...

Full Screen

Full Screen

plugins.spec.js

Source:plugins.spec.js Github

copy

Full Screen

1/* global describe, it, beforeEach */2var chai = require('chai');3var sinon = require('sinon');4var expect = chai.expect;5var _ = require('lodash');6var bamCmd = require('../lib/bam-cmd.js');7var Config = require('../lib/config.js');8function newPlugin(name) {9 return {10 install: sinon.spy() .named(name + ' install'),11 postInstall: sinon.spy().named(name + ' postInstall'),12 build: sinon.spy() .named(name + ' build'),13 postBuild: sinon.spy() .named(name + ' postBuild'),14 deploy: sinon.spy() .named(name + ' deploy'),15 postDeploy: sinon.spy() .named(name + ' postDeploy'),16 };17}18function expectCalledOnceInGoodOrder(methods) {19 var i;20 for (i=0; i < methods.length; i++) {21 var current = methods[i];22 expect(current.calledOnce, 'method ' + current + ' called').to.be.true;23 if (i !== 0) {24 var previous = methods[i - 1];25 expect(current.calledAfter(previous)).to.be.true;26 }27 if (i !== methods.length - 1) {28 var next = methods[i + 1];29 expect(current.calledBefore(next)).to.be.true;30 }31 }32}33function expectAllCalledWithParamters(methods, parameters) {34 _.forEach(methods, function(method) {35 expect(method.called).to.be.true;36 _.forEach(method.calls, function (call) {37 expect(call.args).to.be.equals(parameters);38 });39 });40}41describe('plugins', function() {42 var plugin1, plugin2, plugin1_1, plugin1_2;43 var bamjs, config;44 var options = {};45 beforeEach(function() {46 options = {};47 plugin1 = newPlugin('plugin 1');48 plugin2 = newPlugin('plugin 2');49 plugin1_1 = newPlugin('plugin 1.1');50 plugin1_2 = newPlugin('plugin 1.2');51 plugin1.plugins = [plugin1_1, plugin1_2];52 bamjs = {53 plugins: [plugin1, plugin2],54 install: sinon.spy() .named('bamjs install'),55 postInstall: sinon.spy().named('bamjs postInstall'),56 build: sinon.spy() .named('bamjs build'),57 postBuild: sinon.spy() .named('bamjs postBuild'),58 deploy: sinon.spy() .named('bamjs deploy'),59 postDeploy: sinon.spy() .named('bamjs postDeploy'),60 };61 config = new Config(bamjs);62 return config.load();63 });64 it('should launch the plugin install functions in the right order', function() {65 return bamCmd.runInstall(config, options)66 .then(function() {67 var callList = [68 plugin1_1.install,69 plugin1_2.install,70 plugin1.install,71 plugin2.install,72 bamjs.install,73 plugin2.postInstall,74 plugin1_2.postInstall,75 plugin1_1.postInstall,76 plugin1.postInstall,77 bamjs.postInstall,78 ];79 expectCalledOnceInGoodOrder(callList);80 expectAllCalledWithParamters(callList, [options]);81 });82 });83 it('should launch the plugin build functions in the right order', function() {84 return config.runBuild(config, options)85 .then(function() {86 var callList = [87 plugin1_1.build,88 plugin1_2.build,89 plugin1.build,90 plugin2.build,91 bamjs.build,92 plugin2.postBuild,93 plugin1_2.postBuild,94 plugin1_1.postBuild,95 plugin1.postBuild,96 bamjs.postBuild,97 ];98 expectCalledOnceInGoodOrder(callList);99 expectAllCalledWithParamters(callList, [options]);100 });101 });102 it('should launch the plugin deploy functions in the right order', function() {103 return config.runDeploy(config, options)104 .then(function() {105 var callList = [106 plugin1_1.deploy,107 plugin1_2.deploy,108 plugin1.deploy,109 plugin2.deploy,110 bamjs.deploy,111 plugin2.postDeploy,112 plugin1_2.postDeploy,113 plugin1_1.postDeploy,114 plugin1.postDeploy,115 bamjs.postDeploy,116 ];117 expectCalledOnceInGoodOrder(callList);118 expectAllCalledWithParamters(callList, [options]);119 });120 });121 it('should don\'t launch the plugin postInstall functions', function() {122 return bamCmd.runInstall(config, options, true)123 .then(function() {124 expect(bamjs.install.called).to.be.true;125 expect(plugin1.postInstall.called).to.be.false;126 expect(plugin2.postInstall.called).to.be.false;127 expect(bamjs.postInstall.called).to.be.false;128 });129 });...

Full Screen

Full Screen

utils.js

Source:utils.js Github

copy

Full Screen

1'use strict';2const Promise = require('bluebird');3const AsyncEmitter = require('lib/events/async-emitter');4const utils = require('lib/events/utils');5describe('events/utils', () => {6 describe('passthroughEvent', () => {7 it('should passthrough event synchronously', () => {8 const from = new AsyncEmitter();9 const to = new AsyncEmitter();10 const spy = sinon.spy();11 utils.passthroughEvent(from, to, 'some-event');12 to.on('some-event', spy);13 from.emit('some-event', 'val1', 'val2');14 assert.calledWith(spy, 'val1', 'val2');15 });16 it('should passthrough all passed events', () => {17 const from = new AsyncEmitter();18 const to = new AsyncEmitter();19 const someSpy = sinon.spy().named('someSpy');20 const otherSpy = sinon.spy().named('otherSpy');21 utils.passthroughEvent(from, to, ['some-event', 'other-event']);22 to.on('some-event', someSpy);23 to.on('other-event', otherSpy);24 from.emit('some-event', 'v1', 'v2');25 from.emit('other-event', 'd1', 'd2');26 assert.calledWith(someSpy, 'v1', 'v2');27 assert.calledWith(otherSpy, 'd1', 'd2');28 });29 });30 describe('passthroughEventAsync', () => {31 it('should passthrough event', () => {32 const from = new AsyncEmitter();33 const to = new AsyncEmitter();34 const spy = sinon.spy();35 utils.passthroughEventAsync(from, to, 'some-event');36 to.on('some-event', spy);37 from.emit('some-event', 'val1', 'val2');38 assert.calledOnceWith(spy, 'val1', 'val2');39 });40 it('should passthrough all passed events', () => {41 const from = new AsyncEmitter();42 const to = new AsyncEmitter();43 const someSpy = sinon.spy().named('someSpy');44 const otherSpy = sinon.spy().named('otherSpy');45 utils.passthroughEventAsync(from, to, ['some-event', 'other-event']);46 to.on('some-event', someSpy);47 to.on('other-event', otherSpy);48 from.emit('some-event', 'v1', 'v2');49 from.emit('other-event', 'd1', 'd2');50 assert.calledWith(someSpy, 'v1', 'v2');51 assert.calledWith(otherSpy, 'd1', 'd2');52 });53 it('should wait until the promise from `to` handler is resolved', () => {54 const from = new AsyncEmitter();55 const to = new AsyncEmitter();56 const afterWait = sinon.spy().named('afterWait');57 const insideHandler = sinon.spy().named('insideHandler');58 utils.passthroughEventAsync(from, to, 'some-event');59 to.on('some-event', () => Promise.delay(1).then(insideHandler));60 return from.emitAndWait('some-event')61 .then(afterWait)62 .then(() => assert.callOrder(insideHandler, afterWait));63 });64 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var spy = sinon.spy();2spy.named('foo');3spy.named('bar');4var spy = sinon.spy();5spy.withArgs('foo').returns(1);6spy.withArgs('bar').returns(2);7var spy = sinon.spy();8spy.withArgs('foo').returns(1);9spy.withArgs('bar').returns(2);10var spy = sinon.spy();11spy.withArgs('foo').returns(1);12spy.withArgs('bar').returns(2);13var spy = sinon.spy();14spy.withArgs('foo').returns(1);15spy.withArgs('bar').returns(2);16var spy = sinon.spy();17spy.withArgs('foo').returns(1);18spy.withArgs('bar').returns(2);19var spy = sinon.spy();20spy.withArgs('foo').returns(1);21spy.withArgs('bar').returns(2);22var spy = sinon.spy();23spy.withArgs('foo').returns(1);24spy.withArgs('bar').returns(2);25var spy = sinon.spy();26spy.withArgs('foo').returns(1);27spy.withArgs('bar').returns(2

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var assert = require('assert');3var spy = sinon.spy();4spy('foo', 'bar');5assert(spy.calledWith('foo', 'bar'));6assert(spy.calledWith('bar', 'foo') === false);7assert(spy.calledWith('foo'));8var spy = sinon.spy();9spy('foo', 'bar');10assert(spy.withArgs('foo', 'bar').calledOnce);11assert(spy.withArgs('bar', 'foo').notCalled);12assert(spy.withArgs('foo').calledTwice);13var sinon = require('sinon');14var assert = require('assert');15var foo = require('../app/foo.js');16describe('foo', function() {17 it('should call the callback function', function() {18 var callback = sinon.spy();19 foo(callback);20 assert(callback.called);21 });22});23function foo(callback) {24 callback();25}26var sinon = require('sinon');27var assert = require('assert');28var foo = require('../app/foo.js');29describe('foo', function() {30 it('should call the callback function', function() {31 var callback = sinon.stub();32 foo(callback);33 assert(callback.called);34 });35});36function foo(callback) {37 callback();38}39var sinon = require('sinon');40var assert = require('assert');41var foo = require('../app/foo.js');42describe('foo', function() {43 it('

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var spy = sinon.spy();3spy.named('mySpy');4var sinon = require('sinon');5var spy = sinon.spy();6sinon.spy.named(spy, 'mySpy');7var sinon = require('sinon');8var spy = sinon.spy();9sinon.spy.named(spy, 'mySpy');10var sinon = require('sinon');11var spy = sinon.spy();12sinon.spy.named(spy, 'mySpy');13var sinon = require('sinon');14var spy = sinon.spy();15sinon.spy.named(spy, 'mySpy');16var sinon = require('sinon');17var spy = sinon.spy();18sinon.spy.named(spy, 'mySpy');19var sinon = require('sinon');20var spy = sinon.spy();21sinon.spy.named(spy, 'mySpy');22var sinon = require('sinon');23var spy = sinon.spy();24sinon.spy.named(spy, 'mySpy');25var sinon = require('sinon');26var spy = sinon.spy();27sinon.spy.named(spy, 'mySpy');28var sinon = require('sinon');29var spy = sinon.spy();30sinon.spy.named(spy, 'mySpy');31var sinon = require('sinon');32var spy = sinon.spy();33sinon.spy.named(spy, 'mySpy');

Full Screen

Using AI Code Generation

copy

Full Screen

1var spy = sinon.spy();2spy.named("test");3var spy = sinon.spy();4spy.named("test");5var spy = sinon.spy();6spy.named("test");7var spy = sinon.spy();8spy.named("test");9var spy = sinon.spy();10spy.named("test");11var spy = sinon.spy();12spy.named("test");13var spy = sinon.spy();14spy.named("test");15var spy = sinon.spy();16spy.named("test");17var spy = sinon.spy();18spy.named("test");19var spy = sinon.spy();20spy.named("test");21var spy = sinon.spy();22spy.named("test");23var spy = sinon.spy();24spy.named("test");25var spy = sinon.spy();26spy.named("test");27console.log(spy.named.calledWith("

Full Screen

Using AI Code Generation

copy

Full Screen

1var spy = sinon.spy();2spy("test");3spy("test2");4spy("test3");5var spy = sinon.spy();6spy("test");7spy("test2");8spy("test3");9var spy = sinon.spy();10spy("test");11spy("test2");12spy("test3");13var spy = sinon.spy();14spy("test");15spy("test2");16spy("test3");17var spy = sinon.spy();18spy("test");19spy("test2");20spy("test3");21var spy = sinon.spy();22spy("test");23spy("test2");24spy("test3");25var spy = sinon.spy();26spy("test");27spy("test2");28spy("test3");29console.log(spy.alwaysCalledOn("test"));

Full Screen

Using AI Code Generation

copy

Full Screen

1var spy = sinon.spy();2spy.named('mySpy');3var spy = sinon.spy.named('mySpy');4var stub = sinon.stub();5stub.named('myStub');6var stub = sinon.stub.named('myStub');7var mock = sinon.mock();8mock.named('myMock');9var mock = sinon.mock.named('myMock');10var spy = sinon.spy();11spy.named('mySpy');12var spy = sinon.spy.named('mySpy');13var stub = sinon.stub();14stub.named('myStub');15var stub = sinon.stub.named('myStub');16var mock = sinon.mock();17mock.named('myMock');18var mock = sinon.mock.named('myMock');19var spy = sinon.spy();20spy.named('mySpy');21var spy = sinon.spy.named('mySpy');22var stub = sinon.stub();23stub.named('myStub');24var stub = sinon.stub.named('myStub');25var mock = sinon.mock();26mock.named('myMock');27var mock = sinon.mock.named('myMock');28var spy = sinon.spy();29spy.named('mySpy');30var spy = sinon.spy.named('mySpy');31var stub = sinon.stub();32stub.named('

Full Screen

Using AI Code Generation

copy

Full Screen

1var spy = sinon.spy();2spy.named('mySpy');3var spy = sinon.spy();4spy('mySpy');5var spy = sinon.spy();6spy('mySpy');7var spy = sinon.spy();8spy('mySpy');9var spy = sinon.spy();10spy('mySpy');11var spy = sinon.spy();12spy('mySpy');13var spy = sinon.spy();14spy('mySpy');15var spy = sinon.spy();16spy('mySpy');17var spy = sinon.spy();18spy('mySpy');19var spy = sinon.spy();20spy('mySpy');21var spy = sinon.spy();22spy('mySpy');23var spy = sinon.spy();24spy('mySpy');25var spy = sinon.spy();26spy('mySpy');

Full Screen

Using AI Code Generation

copy

Full Screen

1The above code will be used to test the following code in the file to be tested (test.js)2var myApp = {3 myFunction: function (a, b) {4 }5}6The above code will be used to test the following code in the file to be tested (test.js)7var myApp = {8 myFunction: function (a, b) {9 }10}11The above code will be used to test the following code in the file to be tested (test.js)12var myApp = {13 myFunction: function (a, b) {14 }15}16The above code will be used to test the following code in the file to be tested (test.js)17var myApp = {18 myFunction: function (a, b) {19 }20}21The above code will be used to test the following code in the file to be tested (test.js)22var myApp = {23 myFunction: function (a, b) {24 }25}26The above code will be used to test the following code in the file to be tested (test.js)27var myApp = {28 myFunction: function (a, b) {29 }30}31The above code will be used to test the following code in the file to be tested (test.js)32var myApp = {33 myFunction: function (a, b) {34 }35}36The above code will be used to test the following code in the file to be tested (test.js)37var myApp = {38 myFunction: function (a, b) {39 }40}41The above code will be used to test the following code in the file to be tested (test.js)42var myApp = {43 myFunction: function (a, b) {44 }45}46The above code will be used to test the following code in the file to be tested (test.js)47var myApp = {48 myFunction: function (a, b) {

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run sinon automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful