Best JavaScript code snippet using sinon
saveConfiguration.test.js
Source:saveConfiguration.test.js
...13 });14 it('should saveConfiguration: save all', async () => {15 const gladys = {16 variable: {17 setValue: fake.resolves('value'),18 getValue: fake.resolves(true),19 },20 };21 const config = {22 mqttUrl: 'mqttUrl',23 mqttUsername: 'mqttUsername',24 mqttPassword: 'mqttPassword',25 useEmbeddedBroker: false,26 };27 const mqttHandler = new MqttHandler(gladys, MockedMqttClient, serviceId);28 await mqttHandler.saveConfiguration(config);29 assert.callCount(gladys.variable.setValue, 4);30 assert.calledWith(gladys.variable.setValue, CONFIGURATION.MQTT_URL_KEY, config.mqttUrl, serviceId);31 assert.calledWith(gladys.variable.setValue, CONFIGURATION.MQTT_USERNAME_KEY, config.mqttUsername, serviceId);32 assert.calledWith(gladys.variable.setValue, CONFIGURATION.MQTT_PASSWORD_KEY, config.mqttPassword, serviceId);33 assert.calledWith(34 gladys.variable.setValue,35 CONFIGURATION.MQTT_EMBEDDED_BROKER_KEY,36 config.useEmbeddedBroker,37 serviceId,38 );39 });40 it('should saveConfiguration: destroy all', async () => {41 const gladys = {42 variable: {43 destroy: fake.resolves('value'),44 setValue: fake.resolves('value'),45 getValue: fake.resolves(true),46 },47 };48 const config = {49 mqttUrl: 'mqttUrl',50 };51 const mqttHandler = new MqttHandler(gladys, MockedMqttClient, serviceId);52 await mqttHandler.saveConfiguration(config);53 assert.callCount(gladys.variable.destroy, 3);54 assert.callCount(gladys.variable.setValue, 1);55 assert.calledWith(gladys.variable.destroy, CONFIGURATION.MQTT_USERNAME_KEY, serviceId);56 assert.calledWith(gladys.variable.destroy, CONFIGURATION.MQTT_PASSWORD_KEY);57 assert.calledWith(gladys.variable.destroy, CONFIGURATION.MQTT_EMBEDDED_BROKER_KEY, serviceId);58 });59 it('should saveConfiguration: init docker but no container', async () => {60 const gladys = {61 variable: {62 destroy: fake.resolves('value'),63 setValue: fake.resolves('value'),64 getValue: fake.resolves(true),65 },66 system: {67 getContainers: fake.resolves([]),68 exec: fake.resolves(true),69 restartContainer: fake.resolves(true),70 },71 };72 const config = {73 mqttUrl: 'mqttUrl',74 useEmbeddedBroker: true,75 };76 const mqttHandler = new MqttHandler(gladys, MockedMqttClient, serviceId);77 try {78 await mqttHandler.saveConfiguration(config);79 assert.fail('Should have fail');80 } catch (e) {81 expect(e).to.be.instanceOf(NotFoundError);82 assert.callCount(gladys.variable.destroy, 2);83 assert.callCount(gladys.variable.setValue, 2);84 assert.calledWith(85 gladys.variable.setValue,86 CONFIGURATION.MQTT_EMBEDDED_BROKER_KEY,87 config.useEmbeddedBroker,88 serviceId,89 );90 assert.calledOnce(gladys.system.getContainers);91 assert.notCalled(gladys.system.exec);92 assert.notCalled(gladys.system.restartContainer);93 }94 });95 it('should saveConfiguration: init docker container no present (no user)', async () => {96 const gladys = {97 variable: {98 destroy: fake.resolves('value'),99 setValue: fake.resolves('value'),100 getValue: fake.resolves(true),101 },102 system: {103 getContainers: fake.resolves([104 {105 image: 'another-image',106 },107 ]),108 exec: fake.resolves(true),109 restartContainer: fake.resolves(true),110 },111 };112 const config = {113 mqttUrl: 'mqttUrl',114 useEmbeddedBroker: true,115 };116 const mqttHandler = new MqttHandler(gladys, MockedMqttClient, serviceId);117 await mqttHandler.saveConfiguration(config);118 assert.callCount(gladys.variable.destroy, 2);119 assert.callCount(gladys.variable.setValue, 3);120 assert.calledWith(gladys.variable.setValue, CONFIGURATION.MQTT_URL_KEY, config.mqttUrl, serviceId);121 assert.calledWith(122 gladys.variable.setValue,123 CONFIGURATION.MQTT_EMBEDDED_BROKER_KEY,124 config.useEmbeddedBroker,125 serviceId,126 );127 assert.calledWith(128 gladys.variable.setValue,129 CONFIGURATION.MQTT_MOSQUITTO_VERSION,130 DEFAULT.MOSQUITTO_VERSION,131 serviceId,132 );133 assert.calledOnce(gladys.system.getContainers);134 assert.calledOnce(gladys.system.exec);135 assert.calledTwice(gladys.system.restartContainer);136 });137 it('should saveConfiguration: init docker container not running (no user)', async () => {138 const gladys = {139 variable: {140 destroy: fake.resolves('value'),141 setValue: fake.resolves('value'),142 getValue: fake.resolves(true),143 },144 system: {145 getContainers: fake.resolves([146 {147 image: DEFAULT.MQTT_IMAGE,148 },149 ]),150 exec: fake.resolves(true),151 restartContainer: fake.resolves(true),152 },153 };154 const config = {155 mqttUrl: 'mqttUrl',156 useEmbeddedBroker: true,157 };158 const mqttHandler = new MqttHandler(gladys, MockedMqttClient, serviceId);159 await mqttHandler.saveConfiguration(config);160 assert.callCount(gladys.variable.destroy, 2);161 assert.callCount(gladys.variable.setValue, 3);162 assert.calledWith(gladys.variable.setValue, CONFIGURATION.MQTT_URL_KEY, config.mqttUrl, serviceId);163 assert.calledWith(164 gladys.variable.setValue,165 CONFIGURATION.MQTT_EMBEDDED_BROKER_KEY,166 config.useEmbeddedBroker,167 serviceId,168 );169 assert.calledWith(170 gladys.variable.setValue,171 CONFIGURATION.MQTT_MOSQUITTO_VERSION,172 DEFAULT.MOSQUITTO_VERSION,173 serviceId,174 );175 assert.calledOnce(gladys.system.getContainers);176 assert.calledOnce(gladys.system.exec);177 assert.calledTwice(gladys.system.restartContainer);178 });179 it('should saveConfiguration: init docker container is running (no user)', async () => {180 const gladys = {181 variable: {182 destroy: fake.resolves('value'),183 setValue: fake.resolves('value'),184 getValue: fake.resolves(true),185 },186 system: {187 getContainers: fake.resolves([188 {189 image: DEFAULT.MQTT_IMAGE,190 state: 'running',191 },192 ]),193 exec: fake.resolves(true),194 restartContainer: fake.resolves(true),195 },196 };197 const config = {198 mqttUrl: 'mqttUrl',199 useEmbeddedBroker: true,200 };201 const mqttHandler = new MqttHandler(gladys, MockedMqttClient, serviceId);202 await mqttHandler.saveConfiguration(config);203 assert.callCount(gladys.variable.destroy, 2);204 assert.callCount(gladys.variable.setValue, 3);205 assert.calledWith(gladys.variable.setValue, CONFIGURATION.MQTT_URL_KEY, config.mqttUrl, serviceId);206 assert.calledWith(207 gladys.variable.setValue,208 CONFIGURATION.MQTT_EMBEDDED_BROKER_KEY,209 config.useEmbeddedBroker,210 serviceId,211 );212 assert.calledWith(213 gladys.variable.setValue,214 CONFIGURATION.MQTT_MOSQUITTO_VERSION,215 DEFAULT.MOSQUITTO_VERSION,216 serviceId,217 );218 assert.calledOnce(gladys.system.getContainers);219 assert.calledOnce(gladys.system.exec);220 assert.calledOnce(gladys.system.restartContainer);221 });222 it('should saveConfiguration: init docker container is running (with user no old)', async () => {223 const gladys = {224 variable: {225 destroy: fake.resolves('value'),226 setValue: fake.resolves('value'),227 getValue: fake.resolves(null),228 },229 system: {230 getContainers: fake.resolves([231 {232 image: DEFAULT.MQTT_IMAGE,233 state: 'running',234 },235 ]),236 exec: fake.resolves(true),237 restartContainer: fake.resolves(true),238 },239 };240 const config = {241 mqttUrl: 'mqttUrl',242 useEmbeddedBroker: true,243 mqttUsername: 'user',244 };245 const mqttHandler = new MqttHandler(gladys, MockedMqttClient, serviceId);246 await mqttHandler.saveConfiguration(config);247 assert.callCount(gladys.variable.destroy, 1);248 assert.callCount(gladys.variable.setValue, 4);249 assert.calledWith(gladys.variable.setValue, CONFIGURATION.MQTT_URL_KEY, config.mqttUrl, serviceId);250 assert.calledWith(gladys.variable.setValue, CONFIGURATION.MQTT_USERNAME_KEY, config.mqttUsername, serviceId);251 assert.calledWith(...
getConfiguration.test.js
Source:getConfiguration.test.js
...7describe('mqttHandler.getConfiguration', () => {8 it('should getConfiguration: not Docker', async () => {9 const gladys = {10 variable: {11 getValue: fake.resolves('value'),12 },13 system: {14 isDocker: fake.resolves(false),15 getContainers: fake.resolves([]),16 getNetworkMode: fake.resolves('host'),17 },18 };19 const mqttHandler = new MqttHandler(gladys, MockedMqttClient, serviceId);20 const config = await mqttHandler.getConfiguration();21 const expectedConfig = {22 mqttUrl: 'value',23 mqttUsername: 'value',24 mqttPassword: 'value',25 useEmbeddedBroker: false,26 dockerBased: false,27 brokerContainerAvailable: false,28 networkModeValid: false,29 mosquittoVersion: null,30 };31 expect(config).to.deep.eq(expectedConfig);32 assert.callCount(gladys.variable.getValue, 3);33 assert.calledOnce(gladys.system.isDocker);34 assert.notCalled(gladys.system.getContainers);35 assert.notCalled(gladys.system.getNetworkMode);36 });37 it('should getConfiguration: Docker no container', async () => {38 const gladys = {39 variable: {40 getValue: sinon41 .stub()42 .onCall(3)43 .resolves(null)44 .onCall(4)45 .resolves(null)46 .resolves('value'),47 },48 system: {49 isDocker: fake.resolves(true),50 getContainers: fake.resolves([]),51 getNetworkMode: fake.resolves('bridge'),52 },53 };54 const mqttHandler = new MqttHandler(gladys, MockedMqttClient, serviceId);55 const config = await mqttHandler.getConfiguration();56 const expectedConfig = {57 mqttUrl: 'value',58 mqttUsername: 'value',59 mqttPassword: 'value',60 useEmbeddedBroker: false,61 dockerBased: true,62 brokerContainerAvailable: false,63 networkModeValid: false,64 mosquittoVersion: null,65 };66 expect(config).to.deep.eq(expectedConfig);67 assert.callCount(gladys.variable.getValue, 5);68 assert.calledOnce(gladys.system.isDocker);69 assert.calledOnce(gladys.system.getContainers);70 assert.calledOnce(gladys.system.getNetworkMode);71 });72 it('should getConfiguration: Docker existing container', async () => {73 const gladys = {74 variable: {75 getValue: sinon76 .stub()77 .onCall(3)78 .resolves(null)79 .onCall(4)80 .resolves('2')81 .resolves('value'),82 },83 system: {84 isDocker: fake.resolves(true),85 getContainers: fake.resolves([86 {87 image: 'eclipse-mosquitto:any-tag',88 },89 ]),90 getNetworkMode: fake.resolves('bridge'),91 },92 };93 const mqttHandler = new MqttHandler(gladys, MockedMqttClient, serviceId);94 const config = await mqttHandler.getConfiguration();95 const expectedConfig = {96 mqttUrl: 'value',97 mqttUsername: 'value',98 mqttPassword: 'value',99 useEmbeddedBroker: false,100 dockerBased: true,101 brokerContainerAvailable: true,102 networkModeValid: false,103 mosquittoVersion: '2',104 };105 expect(config).to.deep.eq(expectedConfig);106 assert.callCount(gladys.variable.getValue, 5);107 assert.calledOnce(gladys.system.isDocker);108 assert.calledOnce(gladys.system.getContainers);109 assert.calledOnce(gladys.system.getNetworkMode);110 });111 it('should getConfiguration: Docker existing container (not use embedded)', async () => {112 const gladys = {113 variable: {114 getValue: sinon115 .stub()116 .onCall(3)117 .resolves('0')118 .onCall(4)119 .resolves(null)120 .resolves('value'),121 },122 system: {123 isDocker: fake.resolves(true),124 getContainers: fake.resolves([125 {126 image: 'eclipse-mosquitto:any-tag',127 },128 ]),129 getNetworkMode: fake.resolves('host'),130 },131 };132 const mqttHandler = new MqttHandler(gladys, MockedMqttClient, serviceId);133 const config = await mqttHandler.getConfiguration();134 const expectedConfig = {135 mqttUrl: 'value',136 mqttUsername: 'value',137 mqttPassword: 'value',138 useEmbeddedBroker: false,139 dockerBased: true,140 brokerContainerAvailable: true,141 networkModeValid: true,142 mosquittoVersion: null,143 };144 expect(config).to.deep.eq(expectedConfig);145 assert.callCount(gladys.variable.getValue, 5);146 assert.calledOnce(gladys.system.isDocker);147 assert.calledOnce(gladys.system.getContainers);148 assert.calledOnce(gladys.system.getNetworkMode);149 });150 it('no config on Docker', async () => {151 const gladys = {152 variable: {153 getValue: fake.resolves(null),154 },155 system: {156 isDocker: fake.resolves(true),157 getContainers: fake.resolves([]),158 getNetworkMode: fake.resolves('brigde'),159 },160 };161 const mqttHandler = new MqttHandler(gladys, MockedMqttClient, serviceId);162 const config = await mqttHandler.getConfiguration();163 const expectedConfig = {164 mqttUrl: null,165 mqttUsername: null,166 mqttPassword: null,167 useEmbeddedBroker: false,168 dockerBased: true,169 brokerContainerAvailable: false,170 networkModeValid: false,171 mosquittoVersion: null,172 };173 expect(config).to.deep.eq(expectedConfig);174 assert.callCount(gladys.variable.getValue, 5);175 assert.calledOnce(gladys.system.isDocker);176 assert.calledOnce(gladys.system.getContainers);177 assert.calledOnce(gladys.system.getNetworkMode);178 });179 it('no config not on Docker', async () => {180 const gladys = {181 variable: {182 getValue: fake.resolves(null),183 },184 system: {185 isDocker: fake.resolves(false),186 getContainers: fake.resolves([]),187 getNetworkMode: fake.resolves('host'),188 },189 };190 const mqttHandler = new MqttHandler(gladys, MockedMqttClient, serviceId);191 const config = await mqttHandler.getConfiguration();192 const expectedConfig = {193 mqttUrl: null,194 mqttUsername: null,195 mqttPassword: null,196 useEmbeddedBroker: false,197 dockerBased: false,198 brokerContainerAvailable: false,199 networkModeValid: false,200 mosquittoVersion: null,201 };202 expect(config).to.deep.eq(expectedConfig);203 assert.callCount(gladys.variable.getValue, 3);204 assert.calledOnce(gladys.system.isDocker);205 assert.notCalled(gladys.system.getContainers);206 assert.notCalled(gladys.system.getNetworkMode);207 });208 it('gladys on right network', async () => {209 const gladys = {210 variable: {211 getValue: fake.resolves(null),212 },213 system: {214 isDocker: fake.resolves(true),215 getContainers: fake.resolves([]),216 getNetworkMode: fake.resolves('host'),217 },218 };219 const mqttHandler = new MqttHandler(gladys, MockedMqttClient, serviceId);220 const config = await mqttHandler.getConfiguration();221 const expectedConfig = {222 mqttUrl: null,223 mqttUsername: null,224 mqttPassword: null,225 useEmbeddedBroker: true,226 dockerBased: true,227 brokerContainerAvailable: false,228 networkModeValid: true,229 mosquittoVersion: null,230 };...
DockerodeMock.test.js
Source:DockerodeMock.test.js
...9 demuxStream: (oneStream) => oneStream.emit('end'),10 };11 }12}13Docker.prototype.listContainers = fake.resolves(containers);14Docker.prototype.listImages = fake.resolves(images);15Docker.prototype.createContainer = fake.resolves({ id: containers[0].Id });16Docker.prototype.getContainer = fake.returns({17 inspect: fake.resolves({ HostConfig: { NetworkMode: 'host' } }),18 restart: fake.resolves(true),19 remove: fake.resolves(true),20 stop: fake.resolves(true),21 exec: ({ Cmd }) => {22 const mockedStream = new stream.Readable();23 return fake.resolves({24 start: sinon.stub().yields(Cmd[0] === 'fail' ? 'error' : null, mockedStream),25 })();26 },27});28Docker.prototype.getNetwork = (networkName) => {29 const network = networks.find((n) => n.Name === networkName);30 if (network) {31 return Promise.resolve(network);32 }33 return Promise.reject(new Error('Network not found'));34};35Docker.prototype.createNetwork = fake.resolves(true);36Docker.prototype.pull = (repoTag) => {37 if (repoTag.endsWith('latest')) {38 return fake.resolves(true)();39 }40 return fake.rejects('ERROR')();41};42Docker.prototype.followProgress = (onStream, onFinished, onProgress) => {43 onProgress({});44 onFinished(null, {});45};...
Using AI Code Generation
1const sinon = require('sinon');2const chai = require('chai');3const expect = chai.expect;4const test = require('mocha').it;5const describe = require('mocha').describe;6const before = require('mocha').before;7const after = require('mocha').after;8const fs = require('fs');9const path = require('path');10const proxyquire = require('proxyquire');11describe('test', () => {12 let readFileStub;13 let file;14 const fileContent = 'file content';15 before(() => {16 readFileStub = sinon.stub(fs, 'readFile');17 file = proxyquire('./file', {18 fs: {19 }20 });21 });22 after(() => {23 readFileStub.restore();24 });25 test('should read file', () => {26 readFileStub.resolves(fileContent);27 return file.read(path.join(__dirname, 'test.js')).then((data) => {28 expect(data).to.equal(fileContent);29 });30 });31});
Using AI Code Generation
1const sinon = require('sinon');2const expect = require('chai').expect;3const fs = require('fs');4const readFile = require('../readFile');5describe('readFile', () => {6 it('should read a file', (done) => {7 const fake = sinon.fake.resolves('Hello World');8 sinon.replace(fs, 'readFile', fake);9 readFile('test.txt', (err, data) => {10 expect(err).to.be.null;11 expect(data).to.equal('Hello World');12 sinon.restore();13 done();14 });15 });16});
Using AI Code Generation
1var sinon = require('sinon');2var chai = require('chai');3var expect = chai.expect;4var fake = sinon.fake.resolves('it worked');5var promise = fake();6promise.then(function(value){7 expect(value).to.equal('it worked');8});9var sinon = require('sinon');10var chai = require('chai');11var expect = chai.expect;12var fake = sinon.fake.resolves('it worked');13var promise = fake();14promise.then(function(value){15 expect(value).to.equal('it worked');16});17var sinon = require('sinon');18var chai = require('chai');19var expect = chai.expect;20var fake = sinon.fake.rejects('it worked');21var promise = fake();22promise.then(function(value){23 expect(value).to.equal('it worked');24});25var sinon = require('sinon');26var chai = require('chai');27var expect = chai.expect;28var fake = sinon.fake.throws('it worked');29var promise = fake();30promise.then(function(value){31 expect(value).to.equal('it worked');32});33var sinon = require('sinon');34var chai = require('chai');35var expect = chai.expect;36var fake = sinon.fake.returns('it worked');37var promise = fake.withArgs('hello');38promise.then(function(value){39 expect(value).to.equal('it worked');40});41var sinon = require('sinon');42var chai = require('chai');43var expect = chai.expect;44var mock = sinon.mock();45var promise = mock();46promise.then(function(value){47 expect(value).to.equal('it worked');48});
Using AI Code Generation
1var sinon = require('sinon');2var chai = require('chai');3var expect = chai.expect;4var assert = require('assert');5describe('myModule', function () {6 beforeEach(function () {7 this.sinon = sinon.sandbox.create();8 });9 afterEach(function () {10 this.sinon.restore();11 });12 it('should call the callback with the result', function () {13 var callback = this.sinon.spy();14 var stub = this.sinon.stub(myModule, 'myMethod').resolves('result');15 myModule.myMethod('arg1', 'arg2', callback);16 expect(callback.calledOnce).to.be.true;17 expect(callback.calledWith('result')).to.be.true;18 });19});20var sinon = require('sinon');21var chai = require('chai');22var expect = chai.expect;23var assert = require('assert');24describe('myModule', function () {25 beforeEach(function () {26 this.sinon = sinon.sandbox.create();27 });28 afterEach(function () {29 this.sinon.restore();30 });31 it('should call the callback with the error', function () {32 var callback = this.sinon.spy();33 var stub = this.sinon.stub(myModule, 'myMethod').rejects('error');34 myModule.myMethod('arg1', 'arg2', callback);35 expect(callback.calledOnce).to.be.true;36 expect(callback.calledWith('error')).to.be.true;37 });38});39var sinon = require('sinon');40var chai = require('chai');41var expect = chai.expect;42var assert = require('assert');43describe('myModule', function () {44 beforeEach(function () {45 this.sinon = sinon.sandbox.create();46 });47 afterEach(function () {48 this.sinon.restore();49 });50 it('should
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!!