Best JavaScript code snippet using mountebank
Systemd.js
Source:Systemd.js  
1"use strict";2Object.defineProperty(exports, "__esModule", { value: true });3var os_1 = require("os");4var linux_shell_command_1 = require("linux-shell-command");5var linux_package_manager_1 = require("linux-package-manager");6var Systemd = /** @class */ (function () {7    function Systemd() {8        this.installed = { systemd: null, sudo: null };9        this.serviceNames = null;10        if (os_1.platform() !== 'linux') {11            throw Error("This module only runs on linux");12        }13    }14    Systemd.prototype.setup = function () {15        var _this = this;16        return new Promise(function (resolve, reject) {17            linux_package_manager_1.isPackageInstalled('systemd').then(function (installed) {18                _this.installed.systemd = installed;19                linux_package_manager_1.isPackageInstalled('sudo').then(function (installed) {20                    _this.installed.sudo = installed;21                    resolve();22                }).catch(function (e) {23                    reject(e);24                });25            }).catch(function (e) {26                reject(e);27            });28        });29    };30    Systemd.prototype.isInstalled = function () {31        var _this = this;32        return new Promise(function (resolve, reject) {33            if (_this.installed.systemd === null || _this.installed.sudo === null) {34                _this.setup().then(function () {35                    _this.isInstalled().then(function () { return resolve(); }).catch(function (e) { return reject(e); });36                }).catch(function (e) {37                    reject(e);38                });39            }40            else if (_this.installed.systemd === true && _this.installed.sudo === true) {41                resolve();42            }43            else if (_this.installed.systemd === true) {44                reject(Error('sudo isn\'t installed'));45            }46            else if (_this.installed.sudo === true) {47                reject(Error('systemd isn\'t installed'));48            }49            else {50                reject(Error('sudo and systemd aren\'t installed'));51            }52        });53    };54    Systemd.prototype.listNames = function (forceUpdate, callback) {55        var _this = this;56        if (forceUpdate === void 0) { forceUpdate = false; }57        var result;58        if (this.serviceNames === null || forceUpdate === true) {59            result = new Promise(function (resolve, reject) {60                _this.isInstalled().then(function () {61                    linux_shell_command_1.execute('systemctl list-unit-files -alt service --no-legend --no-pager | cut -d \' \' -f1').then(function (_a) {62                        var shellCommand = _a.shellCommand, success = _a.success;63                        if (success === true) {64                            _this.serviceNames = shellCommand.stdout.trim().split('\n').filter(function (name) { return !name.includes('@'); });65                            resolve(_this.serviceNames);66                        }67                        else {68                            reject(shellCommand.error);69                        }70                    }).catch(function (e) {71                        reject(e);72                    });73                }).catch(function (e) {74                    reject(e);75                });76            });77        }78        else {79            result = Promise.resolve(this.serviceNames);80        }81        if (typeof callback === 'undefined') {82            return result;83        }84        else {85            result.then(function (services) {86                callback(null, services);87            }).catch(function (e) {88                //@ts-ignore;89                callback(e);90            });91        }92    };93    Systemd.prototype.list = function (forceUpdate, callback) {94        var _this = this;95        if (forceUpdate === void 0) { forceUpdate = false; }96        var result = new Promise(function (resolve, reject) {97            _this.listNames(forceUpdate).then(function (names) {98                var command = 'systemctl show \'!?!\' --no-pager -p Names,Description,LoadState,ActiveState,SubState,UnitFileState';99                command = command.replace(/'\!\?\!'/, new Array(names.length).fill("'!?!'").join(' '));100                linux_shell_command_1.execute(command, names).then(function (_a) {101                    var shellCommand = _a.shellCommand, success = _a.success;102                    if (success === true) {103                        var services = {};104                        var informations = shellCommand.stdout.trim().split('\n\n');105                        for (var i = 0; i < informations.length; i++) {106                            var service = informations[i].trim().split('\n');107                            var name_1 = service[0].split('=')[1];108                            services[name_1] = {109                                name: name_1,110                                description: service[1].split('=')[1],111                                loadState: service[2].split('=')[1],112                                activeState: service[3].split('=')[1],113                                subState: service[4].split('=')[1],114                                unitFileState: service[5].split('=')[1],115                            };116                        }117                        resolve(services);118                    }119                    else {120                        reject(shellCommand.error);121                    }122                }).catch(function (e) {123                    reject(e);124                });125            }).catch(function (e) {126                reject(e);127            });128        });129        if (typeof callback === 'undefined') {130            return result;131        }132        else {133            result.then(function (services) {134                callback(null, services);135            }).catch(function (e) {136                //@ts-ignore137                callback(e);138            });139        }140    };141    Systemd.prototype.exists = function (service, forceUpdate, callback) {142        var _this = this;143        if (forceUpdate === void 0) { forceUpdate = false; }144        var result = new Promise(function (resolve, reject) {145            service = service.trim();146            if (!service.includes(' ')) {147                if (!service.includes('.service')) {148                    service = service + ".service";149                }150                _this.listNames(forceUpdate).then(function (names) {151                    resolve(names.includes(service));152                }).catch(function (e) {153                    reject(e);154                });155            }156            else {157                reject(Error('A service name cannot contain space'));158            }159        });160        if (typeof callback === 'undefined') {161            return result;162        }163        else {164            result.then(function (exists) {165                callback(null, exists);166            }).catch(function (e) {167                setTimeout(function () {168                    //@ts-ignore169                    callback(e);170                });171            });172        }173    };174    Systemd.prototype.basicInformations = function (service, callback) {175        var _this = this;176        var result = new Promise(function (resolve, reject) {177            _this.exists(service).then(function (exists) {178                if (exists === true) {179                    linux_shell_command_1.execute('systemctl show \'!?!\' --no-pager -p Names,Description,LoadState,ActiveState,SubState,UnitFileState', [service]).then(function (_a) {180                        var shellCommand = _a.shellCommand, success = _a.success;181                        if (success === true) {182                            var service_1 = shellCommand.stdout.trim().split('\n');183                            resolve({184                                name: service_1[0].split('=')[1],185                                description: service_1[1].split('=')[1],186                                loadState: service_1[2].split('=')[1],187                                activeState: service_1[3].split('=')[1],188                                subState: service_1[4].split('=')[1],189                                unitFileState: service_1[5].split('=')[1]190                            });191                        }192                        else {193                            reject(shellCommand.error);194                        }195                    }).catch(function (e) {196                        reject(e);197                    });198                }199                else {200                    reject(Error("The service \"" + service + "\" doesn't exists"));201                }202            }).catch(function (e) {203                reject(e);204            });205        });206        if (typeof callback === 'undefined') {207            return result;208        }209        else {210            result.then(function (informations) {211                callback(null, informations);212            }).catch(function (e) {213                //@ts-ignore214                callback(e);215            });216        }217    };218    Systemd.prototype.detailedInformations = function (service, callback) {219        var _this = this;220        var result = new Promise(function (resolve, reject) {221            _this.exists(service).then(function (exists) {222                if (exists === true) {223                    linux_shell_command_1.execute('systemctl show \'!?!\' --no-pager', [service]).then(function (_a) {224                        var shellCommand = _a.shellCommand, success = _a.success;225                        if (success === true) {226                            var buf = shellCommand.stdout.trim().split('\n');227                            var informations = {};228                            for (var i = 0; i < buf.length; i++) {229                                var information = buf[i].split('=');230                                informations[information[0]] = information[1];231                            }232                            resolve(informations);233                        }234                        else {235                            reject(shellCommand.error);236                        }237                    }).catch(function (e) {238                        reject(e);239                    });240                }241                else {242                    reject(Error("The service \"" + service + "\" doesn't exists"));243                }244            }).catch(function (e) {245                reject(e);246            });247        });248        if (typeof callback === 'undefined') {249            return result;250        }251        else {252            result.then(function (informations) {253                callback(null, informations);254            }).catch(function (e) {255                //@ts-ignore256                callback(e);257            });258        }259    };260    Systemd.prototype.loadState = function (service, callback) {261        var _this = this;262        var result = new Promise(function (resolve, reject) {263            _this.exists(service).then(function (exists) {264                if (exists === true) {265                    linux_shell_command_1.execute('systemctl show \'!?!\' --no-pager -p LoadState --value', [service]).then(function (_a) {266                        var shellCommand = _a.shellCommand, success = _a.success;267                        if (success === true) {268                            resolve(shellCommand.stdout.trim());269                        }270                        else {271                            reject(shellCommand.error);272                        }273                    }).catch(function (e) {274                        reject(e);275                    });276                }277                else {278                    reject(Error("The service \"" + service + "\" doesn't exists"));279                }280            }).catch(function (e) {281                reject(e);282            });283        });284        if (typeof callback === 'undefined') {285            return result;286        }287        else {288            result.then(function (loadState) {289                callback(null, loadState);290            }).catch(function (e) {291                //@ts-ignore292                callback(e);293            });294        }295    };296    Systemd.prototype.activeState = function (service, callback) {297        var _this = this;298        var result = new Promise(function (resolve, reject) {299            _this.exists(service).then(function (exists) {300                if (exists === true) {301                    linux_shell_command_1.execute('systemctl show \'!?!\' --no-pager -p ActiveState --value', [service]).then(function (_a) {302                        var shellCommand = _a.shellCommand, success = _a.success;303                        if (success === true) {304                            resolve(shellCommand.stdout.trim());305                        }306                        else {307                            reject(shellCommand.error);308                        }309                    }).catch(function (e) {310                        reject(e);311                    });312                }313                else {314                    reject(Error("The service \"" + service + "\" doesn't exists"));315                }316            }).catch(function (e) {317                reject(e);318            });319        });320        if (typeof callback === 'undefined') {321            return result;322        }323        else {324            result.then(function (activeState) {325                callback(null, activeState);326            }).catch(function (e) {327                //@ts-ignore328                callback(e);329            });330        }331    };332    Systemd.prototype.unitFileState = function (service, callback) {333        var _this = this;334        var result = new Promise(function (resolve, reject) {335            _this.exists(service).then(function (exists) {336                if (exists === true) {337                    linux_shell_command_1.execute('systemctl show \'!?!\' --no-pager -p UnitFileState --value', [service]).then(function (_a) {338                        var shellCommand = _a.shellCommand, success = _a.success;339                        if (success === true) {340                            resolve(shellCommand.stdout.trim());341                        }342                        else {343                            reject(shellCommand.error);344                        }345                    }).catch(function (e) {346                        reject(e);347                    });348                }349                else {350                    reject(Error("The service \"" + service + "\" doesn't exists"));351                }352            }).catch(function (e) {353                reject(e);354            });355        });356        if (typeof callback === 'undefined') {357            return result;358        }359        else {360            result.then(function (unitFileState) {361                callback(null, unitFileState);362            }).catch(function (e) {363                //@ts-ignore364                callback(e);365            });366        }367    };368    Systemd.prototype.start = function (service, callback) {369        var _this = this;370        var result = new Promise(function (resolve, reject) {371            _this.activeState(service).then(function (activeState) {372                if (activeState === 'inactive') {373                    linux_shell_command_1.execute('sudo systemctl start \'!?!\'', [service]).then(function (_a) {374                        var shellCommand = _a.shellCommand, success = _a.success;375                        if (success === true) {376                            resolve();377                        }378                        else {379                            reject(shellCommand.error);380                        }381                    }).catch(function (e) {382                        reject(e);383                    });384                }385                else {386                    reject(Error("To be started the service should be inactive but his current active state is " + activeState));387                }388            }).catch(function (e) {389                reject(e);390            });391        });392        if (typeof callback === 'undefined') {393            return result;394        }395        else {396            result.then(function () {397                callback(null);398            }).catch(function (e) {399                callback(e);400            });401        }402    };403    Systemd.prototype.stop = function (service, callback) {404        var _this = this;405        var result = new Promise(function (resolve, reject) {406            _this.activeState(service).then(function (activeState) {407                if (activeState === 'active') {408                    linux_shell_command_1.execute('sudo systemctl stop \'!?!\'', [service]).then(function (_a) {409                        var shellCommand = _a.shellCommand, success = _a.success;410                        if (success === true) {411                            resolve();412                        }413                        else {414                            reject(shellCommand.error);415                        }416                    }).catch(function (e) {417                        reject(e);418                    });419                }420                else {421                    reject(Error("To be stopped the service should be active but his current active state is " + activeState));422                }423            }).catch(function (e) {424                reject(e);425            });426        });427        if (typeof callback === 'undefined') {428            return result;429        }430        else {431            result.then(function () {432                callback(null);433            }).catch(function (e) {434                callback(e);435            });436        }437    };438    Systemd.prototype.restart = function (service, callback) {439        var _this = this;440        var result = new Promise(function (resolve, reject) {441            _this.activeState(service).then(function (activeState) {442                if (activeState === 'active') {443                    linux_shell_command_1.execute('sudo systemctl restart \'!?!\'', [service]).then(function (_a) {444                        var shellCommand = _a.shellCommand, success = _a.success;445                        if (success === true) {446                            resolve();447                        }448                        else {449                            reject(shellCommand.error);450                        }451                    }).catch(function (e) {452                        reject(e);453                    });454                }455                else {456                    reject(Error("To be restarted the service should be active but his current active state is " + activeState));457                }458            }).catch(function (e) {459                reject(e);460            });461        });462        if (typeof callback === 'undefined') {463            return result;464        }465        else {466            result.then(function () {467                callback(null);468            }).catch(function (e) {469                callback(e);470            });471        }472    };473    Systemd.prototype.enable = function (service, callback) {474        var _this = this;475        var result = new Promise(function (resolve, reject) {476            _this.unitFileState(service).then(function (unitFileState) {477                if (unitFileState === 'masked') {478                    reject(Error('The service is currently masked and cannot be enabled'));479                }480                else if (unitFileState !== 'enabled') {481                    linux_shell_command_1.execute('sudo systemctl enable \'!?!\'', [service]).then(function (_a) {482                        var shellCommand = _a.shellCommand, success = _a.success;483                        if (success === true) {484                            resolve();485                        }486                        else {487                            reject(shellCommand.error);488                        }489                    }).catch(function (e) {490                        reject(e);491                    });492                }493                else {494                    // reject(Error('The service is already enabled'));495                    resolve();496                }497            }).catch(function (e) {498                reject(e);499            });500        });501        if (typeof callback === 'undefined') {502            return result;503        }504        else {505            result.then(function () {506                callback(null);507            }).catch(function (e) {508                callback(e);509            });510        }511    };512    Systemd.prototype.disable = function (service, callback) {513        var _this = this;514        var result = new Promise(function (resolve, reject) {515            _this.unitFileState(service).then(function (unitFileState) {516                if (unitFileState === 'masked') {517                    reject(Error('The service is currently masked and cannot be disabled'));518                }519                else if (unitFileState !== 'disabled') {520                    linux_shell_command_1.execute('sudo systemctl disable \'!?!\'', [service]).then(function (_a) {521                        var shellCommand = _a.shellCommand, success = _a.success;522                        if (success === true) {523                            resolve();524                        }525                        else {526                            reject(shellCommand.error);527                        }528                    }).catch(function (e) {529                        reject(e);530                    });531                }532                else {533                    // reject(Error('The service is already disabled'));534                    resolve();535                }536            }).catch(function (e) {537                reject(e);538            });539        });540        if (typeof callback === 'undefined') {541            return result;542        }543        else {544            result.then(function () {545                callback(null);546            }).catch(function (e) {547                callback(e);548            });549        }550    };551    Systemd.prototype.mask = function (service, callback) {552        var _this = this;553        var result = new Promise(function (resolve, reject) {554            _this.unitFileState(service).then(function (unitFileState) {555                if (unitFileState !== 'masked') {556                    linux_shell_command_1.execute('sudo systemctl mask \'!?!\'', [service]).then(function (_a) {557                        var shellCommand = _a.shellCommand, success = _a.success;558                        if (success === true) {559                            resolve();560                        }561                        else {562                            reject(shellCommand.error);563                        }564                    }).catch(function (e) {565                        reject(e);566                    });567                }568                else {569                    // reject(Error('The service is already masked'));570                    resolve();571                }572            }).catch(function (e) {573                reject(e);574            });575        });576        if (typeof callback === 'undefined') {577            return result;578        }579        else {580            result.then(function () {581                callback(null);582            }).catch(function (e) {583                callback(e);584            });585        }586    };587    Systemd.prototype.unmask = function (service, callback) {588        var _this = this;589        var result = new Promise(function (resolve, reject) {590            _this.unitFileState(service).then(function (unitFileState) {591                if (unitFileState === 'masked') {592                    linux_shell_command_1.execute('sudo systemctl unmask \'!?!\'', [service]).then(function (_a) {593                        var shellCommand = _a.shellCommand, success = _a.success;594                        if (success === true) {595                            resolve();596                        }597                        else {598                            reject(shellCommand.error);599                        }600                    }).catch(function (e) {601                        reject(e);602                    });603                }604                else {605                    // reject(Error('The service isn\'t masked'));606                    resolve();607                }608            }).catch(function (e) {609                reject(e);610            });611        });612        if (typeof callback === 'undefined') {613            return result;614        }615        else {616            result.then(function () {617                callback(null);618            }).catch(function (e) {619                callback(e);620            });621        }622    };623    return Systemd;624}());...docker-service.js
Source:docker-service.js  
1const expect = require('chai').expect;2import {Service, RestartPolicy} from "../../../src/domain";3import {ShellDockerServiceExporter} from "../../../src/exporter/shell/docker-service";4import {EnvironmentVariable} from "../../../src/domain/environmentVariable";5describe('ShellDockerServiceExporter', function () {6    it('should convert image.', () => {7        const service = new Service("database");8        service.setBaseImage("mysql:5.6");9        const shellCommand = ShellDockerServiceExporter.getShellCommand(service);10        expect(shellCommand).to.equal('docker service create --name database mysql:5.6');11    });12    it('should convert env vars.', () => {13        const service = new Service("database");14        service.setBaseImage("mysql:5.6");15        service.addEnvironmentVariable("NODE_ENV", "development");16        const shellCommand = ShellDockerServiceExporter.getShellCommand(service);17        expect(shellCommand).to.equal('docker service create --name database --env NODE_ENV=development mysql:5.6');18    });19    it('should resolve env values.', () => {20        const service = new Service("database");21        service.setBaseImage("mysql:5.6");22        service.addEnvironmentVariable("SPRING_RABBITMQ_HOST", "$IP");23        const globalEnvVars = [24            EnvironmentVariable.create("IP", "127.0.0.1")25        ];26        const shellCommand = ShellDockerServiceExporter.getShellCommand(service, globalEnvVars);27        expect(shellCommand).to.equal('docker service create --name database --env SPRING_RABBITMQ_HOST=127.0.0.1 mysql:5.6');28    });29    it('should convert ports.', () => {30        const service = new Service("database");31        service.setBaseImage("mysql:5.6");32        service.addPortMapping(1337, 3360);33        service.addPortMapping(8080);34        const shellCommand = ShellDockerServiceExporter.getShellCommand(service);35        expect(shellCommand).to.equal('docker service create --name database --publish 1337:3360 --publish 8080 mysql:5.6');36    });37    it('should convert restart-policy.', () => {38        const service = new Service("database");39        service.setBaseImage("mysql:5.6");40        service.setRestartPolicy(RestartPolicy.ON_FAILURE);41        let shellCommand = ShellDockerServiceExporter.getShellCommand(service);42        expect(shellCommand).to.equal('docker service create --name database --restart-condition on-failure mysql:5.6');43        service.setRestartPolicy(RestartPolicy.NO);44        shellCommand = ShellDockerServiceExporter.getShellCommand(service);45        expect(shellCommand).to.equal('docker service create --name database mysql:5.6');46        service.setRestartPolicy(RestartPolicy.ALWAYS);47        shellCommand = ShellDockerServiceExporter.getShellCommand(service);48        expect(shellCommand).to.equal('docker service create --name database --restart-condition any mysql:5.6');49    });50    it('should pretty print.', () => {51        const service = new Service("database");52        service.setBaseImage("mysql:5.6");53        service.addEnvironmentVariable("NODE_ENV", "development");54        const shellCommand = ShellDockerServiceExporter.getShellCommand(service, [], true);55        const actual = 'docker service create \\\n    --name database \\\n    --env NODE_ENV=development \\\n    mysql:5.6';56        expect(shellCommand).to.equal(actual);57    });...ShellCommand.spec.ts
Source:ShellCommand.spec.ts  
1import { assert } from 'chai'2import ShellCommand from './ShellCommand'3import { testShellFactory } from '../util/test-helpers'4describe('ShellCommand', function() {5  const testShell = testShellFactory()6  it('Successfully creates an instance from only one argument', function() {7    const cmd = new ShellCommand('onearg', testShell)8    assert.equal(cmd.args[0], 'onearg')9    assert.equal(cmd.args.length, 1)10  })11  it('Successfully creates an instance from multiple arguments', function() {12    const cmd = new ShellCommand('two args', testShell)13    assert.equal(cmd.args[0], 'two')14    assert.equal(cmd.args.length, 2)15    assert.equal(cmd.args[1], 'args')16  })17  it('Successfully creates an instance from an empty string', function() {18    const cmd = new ShellCommand('', testShell)19    assert.equal(cmd.args[0], '')20    assert.equal(cmd.args.length, 1)21  })22  it('Successfully handles excessive whitespace', function() {23    const cmd = new ShellCommand('   two     args    ', testShell)24    assert.equal(cmd.args[0], 'two')25    assert.equal(cmd.args.length, 2)26    assert.equal(cmd.args[1], 'args')27  })28  it('Successfully creates an instance from a string with only whitespace', function() {29    const cmd = new ShellCommand('     ', testShell)30    assert.equal(cmd.args[0], '')31    assert.equal(cmd.args.length, 1)32  })...Using AI Code Generation
1var shell = require('shelljs');2var port = 2525;3var host = 'localhost';4var command = 'mb --configfile imposter.ejs --port ' + port + ' --host ' + host + ' --allowInjection';5var mb = require('mountebank');6var fs = require('fs');7var port = 2525;8var host = 'localhost';9var config = fs.readFileSync('imposter.ejs', 'utf8');10var options = {11};12mb.start(options, config).then(function (instance) {13    console.log('Mountebank server started at port: ' + port);14}).catch(function (error) {15    console.error(error);16});17{18    "stubs": [{19        "responses": [{20            "is": {21                "headers": {22                },23                "body": {24                    "data": {Using AI Code Generation
1var shellCommand = require('shelljs').exec;2var command = 'mb';3var args = ['--configfile', 'imposters.ejs', '--loglevel', 'debug', '--allowInjection'];4var options = {async: true, silent: true};5var mb = shellCommand(command + ' ' + args.join(' '), options);6mb.stdout.on('data', function (data) {7    console.log(data);8});9mb.stderr.on('data', function (data) {10    console.log(data);11});12mb.on('exit', function (code) {13    console.log('child process exited with code ' + code);14});Using AI Code Generation
1var shellCommand = require('shelljs');2var command = 'mb --configfile config.json --allowInjection';3shellCommand.exec(command, {async:true});4{5    {6        {7            {8              "is": {9                "headers": {10                },11                "body": {12                  "data": {13                  }14                }15              }16            }17        }18    }19}20var request = require('request');21var options = {22  headers: {23  },24  json: {25  }26};27request(options, function(error, response, body) {28  console.log(body);29});30{ status: 'success',31  data: { name: 'John', age: 30 } }32var mb = require('mountebank');33var server = mb.create({34    {35        {36            {37              is: {38                headers: {39                },40                body: {41                  data: {42                  }43                }44              }45            }46        }47    }48});49server.start().then(function () {50  console.log('Mountebank started');51});52### create(optionsUsing AI Code Generation
1var shellCommand = require('mountebank').shellCommand;2var shell = shellCommand('mb');3var port = 2525;4var protocol = 'http';5var stub = {6  predicates: [{equals: {method: 'GET', path: '/test'}}],7  responses: [{is: {statusCode: 200, body: 'test'}}]8};9shell.create({port: port, protocol: protocol, stubs: stub}, function () {10  console.log('Mountebank running on port ' + port);11});12var shellCommand = require('mountebank').shellCommand;13var shell = shellCommand('mb');14shell.create({port: 2525, protocol: 'http', stubs: stub}, function () {15  console.log('Mountebank running on port 2525');16});17* `port` - the port on which the server will listen (default: 2525)18* `protocol` - the protocol on which the server will listen (default: http)19* `stubs` - an array of stubs to be used by the server (default: [])20* `configFile` - a path to a JSON file containing the server configuration (default: none)21shell.delete(function () {22  console.log('Mountebank stopped');23});24var stub = {25  predicates: [{equals: {method: 'GET', path: '/test'}}],26  responses: [{is: {statusCode: 200, body: 'test'}}]27};28shell.addStub(stub, function () {Using AI Code Generation
1const mb = require('mountebank');2const shell = require('shelljs');3mb.create({ port: 2525, name: 'test', allowInjection: true }, function (error, imposter) {4    imposter.addStub({5            {6                is: {7                    headers: {8                    },9                }10            }11    }, function (error, stub) {12    });13});14shell.exec("mb start --configfile config.json --allowInjection");15shell.exec("mb stop");16{17        {18                {19                    "is": {20                        "headers": {21                        },22                    }23                }24        }25}26const mb = require('mountebank');27const shell = require('shelljs');28describe('test', function () {29    before(function (done) {30        shell.exec("mb start --configfile config.json --allowInjection");31        setTimeout(function () {32            done();33        }, 5000);34    });35    after(function (done) {36        shell.exec("mb stop");37        setTimeout(function () {38            done();39        }, 5000);40    });41    it('test', function (done) {Using AI Code Generation
1const shellCommand = require('shell-command');2const mb = require('mountebank');3mb.create({ port: 2525, allowInjection: true }, () => {4    mb.post('/imposters', {5        stubs: [{6            responses: [{7                is: {8                }9            }]10        }]11    }, () => {12            console.log(stdout);13        });14    });15});16mb.stop(() => {17    console.log('Server stopped');18});19mb.stop(() => {20    console.log('Server stopped');21});22mb.stop(() => {23    console.log('Server stopped');24});25mb.stop(() => {26    console.log('Server stopped');27});28mb.stop(() => {29    console.log('Server stopped');30});31mb.stop(() => {32    console.log('Server stopped');33});34mb.stop(() => {35    console.log('Server stopped');36});37mb.stop(() => {38    console.log('Server stopped');39});40mb.stop(() => {41    console.log('Server stopped');42});43mb.stop(() => {44    console.log('Server stopped');45});Using AI Code Generation
1var shellCommand = require('shelljs');2shellCommand.exec('mb --configfile imposter.ejs --allowInjection --loglevel debug --logfile ./logs/mb.log --pidfile ./mb.pid');3shellCommand.exec('mb restart --configfile imposter.ejs --allowInjection --loglevel debug --logfile ./logs/mb.log --pidfile ./mb.pid');4var shellCommand = require('shelljs');5shellCommand.exec('mb restart --configfile imposter.ejs --allowInjection --loglevel debug --logfile ./logs/mb.log --pidfile ./mb.pid');6var shellCommand = require('shelljs');7shellCommand.exec('mb restart --configfile imposter.ejs --allowInjection --loglevel debug --logfile ./logs/mb.log --pidfile ./mb.pid');8var shellCommand = require('shelljs');9shellCommand.exec('mb restart --configfile imposter.ejs --allowInjection --loglevel debug --logfile ./logs/mb.log --pidfile ./mb.pid');10var shellCommand = require('shelljs');11shellCommand.exec('mb restart --configfile imposter.ejs --allowInjection --loglevel debug --logfile ./logs/mb.log --pidfile ./mb.pid');12var shellCommand = require('shelljs');13shellCommand.exec('mb restart --configfile imposter.ejs --allowInjection --loglevel debug --logfile ./logs/mb.log --pidfile ./mb.pid');14var shellCommand = require('shelljs');15shellCommand.exec('mb restart --configfile imposter.ejs --allowInjection --loglevel debug --logfile ./logs/mb.log --pidfile ./mb.pid');16var shellCommand = require('shelljs');17shellCommand.exec('mb restart --configfile imposter.ejs --allowInjection --loglevel debug --logfile ./logs/mb.log --pidfile ./mb.pid');Using AI Code Generation
1var shellCommand = require('mountebank').shellCommand;2shellCommand('mb start --configfile config.json --allowInjection --localOnly', { cwd: 'C:\\Users\\user\\Desktop\\mb' }, function (error, stdout, stderr) {3    console.log('stdout: ' + stdout);4    console.log('stderr: ' + stderr);5    if (error !== null) {6        console.log('exec error: ' + error);7    }8});9var mb = require('mountebank').create({ cwd: 'C:\\Users\\user\\Desktop\\mb', allowInjection: true, localOnly: true });10mb.start().then(function () {11    console.log('started');12});13mb.stop().then(function () {14    console.log('stopped');15});16var mb = require('mountebank').create({ configfile: 'config.json', cwd: 'C:\\Users\\user\\Desktop\\mb', allowInjection: true, localOnly: true });17mb.start().then(function () {18    console.log('started');19});20mb.stop().then(function () {21    console.log('stopped');22});23mb.createImposter({ port: 4545, protocol: 'http' }).then(function (imposter) {24    console.log(imposter.port);25    console.log(imposter.protocol);26});27mb.createImposter({ port: 4545, protocol: 'http', stubs: [{ responses: [{ is: { body: 'Hello World' } }] }] }).then(function (imposter) {28    console.log(imposter.port);29    console.log(imposter.protocol);30    console.log(imposter.stubs);31});32mb.createImposter({ port: 4545, protocol: 'http' }).then(function (imposter) {33    imposter.addStub({ responses: [{ is: { body: 'Hello World' } }] }).then(function (stub) {34        console.log(stub);35    });36});37mb.createImposter({ port: 4545, protocol: 'http' }).then(function (imposter) {38    imposter.addStub({ responses: [{ is: { body: 'Hello World' } }] }).then(function (stub) {39        stub.update({ responses: [{ is: { body: 'Hello World' } }] }).then(function (stub) {Using AI Code Generation
1var shellCommand = require('mountebank').shellCommand;2var mb = require('mountebank');3var imposter = require('./imposter');4var request = require('request');5var fs = require('fs');6var mbPort = 2525;7var mbProcess = mb.create({port: mbPort, pidfile: 'mb.pid', logfile: 'mb.log', ipWhitelist: '*', protofile: 'protofile.json'});8mbProcess.then(function (result) {9    console.log('mbProcess then');10    console.log(result);11    console.log('mbProcess then end');12}).then(function (result) {13    console.log('shellCommand then');14    console.log(result);15    console.log('shellCommand then end');16    return imposter.create(mbUrl);17}).then(function (result) {18    console.log('imposter create then');19    console.log(result);20    console.log('imposter create then end');21    return imposter.get(mbUrl, result.id);22}).then(function (result) {23    console.log('imposter get then');24    console.log(result);25    console.log('imposter get then end');26    return imposter.remove(mbUrl, result.id);27}).then(function (result) {28    console.log('imposter remove then');29    console.log(result);30    console.log('imposter remove then end');31    return shellCommand('mb stop --pidfile mb.pid');32}).then(function (result) {33    console.log('mb stop then');34    console.log(result);35    console.log('mb stop then end');36}).catch(function (error) {37    console.log('error');38    console.log(error);39    console.log('error end');40});41var request = require('request');42var fs = require('fs');43var imposter = {44    create: function (mbUrl, options) {45        var promise = new Promise(function (resolve, reject) {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!!
