Best JavaScript code snippet using mountebank
jum.js
Source:jum.js  
1// ***** BEGIN LICENSE BLOCK *****2// Version: MPL 1.1/GPL 2.0/LGPL 2.13//4// The contents of this file are subject to the Mozilla Public License Version5// 1.1 (the "License"); you may not use this file except in compliance with6// the License. You may obtain a copy of the License at7// http://www.mozilla.org/MPL/8//9// Software distributed under the License is distributed on an "AS IS" basis,10// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License11// for the specific language governing rights and limitations under the12// License.13//14// The Original Code is Mozilla Corporation Code.15//16// The Initial Developer of the Original Code is17// Adam Christian.18// Portions created by the Initial Developer are Copyright (C) 200819// the Initial Developer. All Rights Reserved.20//21// Contributor(s):22//  Mikeal Rogers <mikeal.rogers@gmail.com>23//24// Alternatively, the contents of this file may be used under the terms of25// either the GNU General Public License Version 2 or later (the "GPL"), or26// the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),27// in which case the provisions of the GPL or the LGPL are applicable instead28// of those above. If you wish to allow use of your version of this file only29// under the terms of either the GPL or the LGPL, and not to allow others to30// use your version of this file under the terms of the MPL, indicate your31// decision by deleting the provisions above and replace them with the notice32// and other provisions required by the GPL or the LGPL. If you do not delete33// the provisions above, a recipient may use your version of this file under34// the terms of any one of the MPL, the GPL or the LGPL.35//36// ***** END LICENSE BLOCK *****37var EXPORTED_SYMBOLS = ["assert", "assertTrue", "assertFalse", "assertEquals", "assertNotEquals",38                        "assertNull", "assertNotNull", "assertUndefined", "assertNotUndefined",39                        "assertNaN", "assertNotNaN", "fail", "pass"];40var frame = {}; Components.utils.import("resource://mozmill/modules/frame.js", frame);41var ifJSONable = function (v) {42  if (typeof(v) == 'function') {43    return undefined;44  } else {45    return v;46  }47}48var assert = function (booleanValue, comment) {49  if (booleanValue) {50    frame.events.pass({'function':'jum.assert', 'value':ifJSONable(booleanValue), 'comment':comment});51    return true;52  }53  reportFail("jum.assert(" + ifJSONable(booleanValue) + ") - " + comment);54  return false;55}56var assertTrue = function (booleanValue, comment) {57  if (typeof(booleanValue) != 'boolean') {58    reportFail("jum.assertTrue(" + ifJSONable(booleanValue) + ") " +59      "Bad argument, value type " + typeof(booleanValue) + " isn't boolean - " + comment);60      return false;61  }62  if (booleanValue) {63    frame.events.pass({'function':'jum.assertTrue', 'value':ifJSONable(booleanValue),64                       'comment':comment});65    return true;66  }67  reportFail("jum.assertTrue(" + ifJSONable(booleanValue) + ") - " + comment);68  return false;69}70var assertFalse = function (booleanValue, comment) {71  if (typeof(booleanValue) != 'boolean') {72    reportFail("jum.assertFalse(" + ifJSONable(booleanValue) + ") " +73      "Bad argument, value type " + typeof(booleanValue) + " isn't boolean - " + comment);74    return false;75  }76  if (!booleanValue) {77    frame.events.pass({'function':'jum.assertFalse', 'value':ifJSONable(booleanValue),78                       'comment':comment});79    return true;80  }81  reportFail("jum.assertFalse(" + ifJSONable(booleanValue) + ") - " + comment);82  return false;83}84var assertEquals = function (value1, value2, comment) {85  if (value1 == value2) {86    frame.events.pass({'function':'jum.assertEquals', 'comment':comment,87                       'value1':ifJSONable(value1), 'value2':ifJSONable(value2)});88    return true;89  }90  reportFail("jum.assertEquals(" + ifJSONable(value1) + ", " +91    ifJSONable(value2) + ") - " + comment);92  return false;93}94var assertNotEquals = function (value1, value2, comment) {95  if (value1 != value2) {96    frame.events.pass({'function':'jum.assertNotEquals', 'comment':comment,97                       'value1':ifJSONable(value1), 'value2':ifJSONable(value2)});98    return true;99  }100  reportFail("jum.assertNotEquals(" + ifJSONable(value1) + ", " +101      ifJSONable(value2) + ") - " + comment);102  return false;103}104var assertNull = function (value, comment) {105  if (value == null) {106    frame.events.pass({'function':'jum.assertNull', 'comment':comment,107                       'value':ifJSONable(value)});108    return true;109  }110  reportFail("jum.assertNull(" + ifJSONable(booleanValue) + ") - " + comment);111  return false;112}113var assertNotNull = function (value, comment) {114  if (value != null) {115    frame.events.pass({'function':'jum.assertNotNull', 'comment':comment,116                       'value':ifJSONable(value)});117    return true;118  }119  reportFail("jum.assertNotNull(" + ifJSONable(booleanValue) + ") - " + comment);120  return false;121}122var assertUndefined = function (value, comment) {123  if (value == undefined) {124    frame.events.pass({'function':'jum.assertUndefined', 'comment':comment,125                       'value':ifJSONable(value)});126    return true;127  }128  reportFail("jum.assertUndefined(" + ifJSONable(booleanValue) + ") - " + comment);129  return false;130}131var assertNotUndefined = function (value, comment) {132  if (value != undefined) {133    frame.events.pass({'function':'jum.assertNotUndefined', 'comment':comment,134                       'value':ifJSONable(value)});135    return true;136  }137  reportFail("jum.assertNotUndefined(" + ifJSONable(booleanValue) + ") - " + comment);138  return false;139}140var assertNaN = function (value, comment) {141  if (isNaN(value)) {142    frame.events.pass({'function':'jum.assertNaN', 'comment':comment,143                       'value':ifJSONable(value)});144    return true;145  }146  reportFail("jum.assertNaN(" + ifJSONable(booleanValue) + ") - " + comment);147  return false;148}149var assertNotNaN = function (value, comment) {150  if (!isNaN(value)) {151    frame.events.pass({'function':'jum.assertNotNaN', 'comment':comment,152                       'value':ifJSONable(value)});153    return true;154  }155  reportFail("jum.assertNotNaN(" + ifJSONable(booleanValue) + ") - " + comment);156  return false;157}158var reportFail = function(comment) {159  try {160    throw new Error(comment || "");161  } catch(e) {162    frame.events.fail({'assertion': e});163  }164}165var fail = function (comment) {166  frame.events.fail({'function':'jum.fail', 'comment':comment});167  return false;168}169var pass = function (comment) {170  frame.events.pass({'function':'jum.pass', 'comment':comment});171  return true;...delegates.js
Source:delegates.js  
...49  create: function () { return new ExportOptionsSaveForWeb(); },50  schema: [51    ["blur",           c.finitePositiveNumber(0)],52    ["includeProfile", c.interpretEmbedColorProfile],53    ["interlaced",     c.booleanValue(false)],54    ["matteColor",     c.rgbColor]55  ]56};57var PALETTED_COMMON = {58  schema: [59    ["colors",       c.intInRange(2, 256, 256)],60    ["dither",       c.enumValue(Dither, Dither.DIFFUSION)],61    ["ditherAmount", c.intInRange(0, 100, 100)],62    ["transparency", c.booleanValue(true)]63  ]64};65var EXPORT_PALETTED_COMMON = mergeDelegates(EXPORT_COMMON, PALETTED_COMMON, {66  schema: [67    ["colorReduction",     c.enumValue(ColorReductionType, ColorReductionType.SELECTIVE)],68    ["lossy",              c.intInRange(0, 100, 0)],69    ["transparencyDither", c.enumValue(Dither, Dither.NONE)],70    ["transparencyAmount", c.intInRange(0, 100, 100)],71    ["webSnap",            c.intInRange(0, 100, 0)]72  ]73});74var EXPORT_GIF = mergeDelegates(EXPORT_PALETTED_COMMON, {75  schema: [76    ["format", c.constValue(SaveDocumentType.COMPUSERVEGIF)]77  ]78});79var EXPORT_JPEG = mergeDelegates(EXPORT_COMMON, {80  schema: [81    ["format",       c.constValue(SaveDocumentType.JPEG)],82    ["quality",      c.intInRange(0, 100, 1000 / 12)],83    ["optimized",    c.booleanValue(true)],84    ["transparency", c.constValue(false)]85  ]86});87var EXPORT_PNG24 = mergeDelegates(EXPORT_COMMON, {88  schema: [89    ["transparency", c.constValue(true)],90    ["format",       c.constValue(SaveDocumentType.PNG)],91    ["PNG8",         c.constValue(false)]92  ]93});94var EXPORT_PNG8 = mergeDelegates(EXPORT_PALETTED_COMMON, {95  schema: [96    ["format", c.constValue(SaveDocumentType.PNG)],97    ["PNG8",   c.constValue(true)]98  ]99});100var SAVE_BMP = {101  create: function () { return new BMPSaveOptions(); },102  schema: [103    ["alphaChannels",  c.constValue(true)],104    ["depth",          c.constValue(BMPDepthType.BMP_A8R8G8B8)],105    ["flipRowOrder",   c.booleanValue(false)],106    // Other choice is .OS2, which isn't gonna happen107    ["osType",         c.constValue(OperatingSystem.WINDOWS)],108    ["rleCompression", c.booleanValue(true)]109  ]110};111var SAVE_GIF = mergeDelegates(PALETTED_COMMON, {112  create: function () { return new GIFSaveOptions(); },113  schema: [114    ["forcedColors",        c.enumValue(ForcedColors, ForcedColors.NONE)],115    ["matte",               c.enumValue(MatteType,    MatteType.NONE)],116    ["palette",             c.enumValue(Palette,      Palette.LOCALSELECTIVE)],117    ["interlaced",          c.booleanValue(false)],118    ["preserveExactColors", c.booleanValue(false)]119  ]120});121var SAVE_JPEG = {122  create: function () { return new JPEGSaveOptions(); },123  schema: [124    ["embedColorProfile", c.interpretEmbedColorProfile],125    ["formatOptions",     c.enumValue(FormatOptions, FormatOptions.OPTIMIZEDBASELINE)],126    ["matte",             c.enumValue(MatteType,     MatteType.BLACK)],127    ["quality",           c.scaleToIntRange(0, 100, 0, 12, 10)],128    ["scans",             c.intInRange(3, 5, 3)]129  ]130};131var SAVE_PNG = {132  create: function () { return new PNGSaveOptions(); },133  schema: [134    ["compression", c.intInRange(0, 9, 4)],135    ["interlaced",  c.booleanValue(false)]136  ]137};138var SAVE_TARGA = {139  create: function () { return new TargaSaveOptions(); },140  schema: [141    ["alphaChannels",  c.constValue(true)],142    ["resolution",     c.constValue(TargaBitsPerPixels.THIRTYTWO)],143    ["rleCompression", c.booleanValue(true)]144  ]145};146var SAVE_TIFF = {147  create: function () { return new TiffSaveOptions(); },148  schema: [149    ["alphaChannels",      c.booleanValue(false)],150    ["annotations",        c.booleanValue(false)],151    ["byteOrder",          c.enumValue(ByteOrder, ByteOrder.IBM)],152    ["embedColorProfile",  c.interpretEmbedColorProfile],153    ["imageCompression",   c.enumValue(TIFFEncoding, TIFFEncoding.NONE)],154    ["interleaveChannels", c.booleanValue(true)],155    ["jpegQuality",        c.scaleToIntRange(0, 100, 0, 12, 10)],156    ["layers",             c.booleanValue(false)],157    ["saveImagePyramid",   c.booleanValue(false)],158    ["spotColors",         c.booleanValue(false)],159    ["transparency",       c.booleanValue(true)]160  ]161};162var EXPORTERS = {163  gif: EXPORT_GIF,164  jpg: EXPORT_JPEG,165  png: EXPORT_PNG24,166  png8: EXPORT_PNG8167};168var SAVERS = {169  bmp: SAVE_BMP,170  gif: SAVE_GIF,171  jpg: SAVE_JPEG,172  png: SAVE_PNG,173  tga: SAVE_TARGA,...Checkbox.js
Source:Checkbox.js  
1import React, { Fragment } from 'react'2import { storiesOf } from '@storybook/react'3import { BooleanValue } from 'react-values'4import { Checkbox } from 'Checkbox'5import { Heading } from 'Heading'6import { Pane } from 'Pane'7storiesOf('Checkbox', module)8  .add('Variants', () => (9    <Fragment>10      <Pane>11        <BooleanValue>12          {({ value, toggle }) => (13            <Checkbox onClick={toggle} checked={value}>14              Checkbox label15            </Checkbox>16          )}17        </BooleanValue>18      </Pane>19      <Pane>20        <BooleanValue>21          {({ value, toggle }) => (22            <Checkbox onClick={toggle} checked={value} variant='square'>23              Checkbox label24            </Checkbox>25          )}26        </BooleanValue>27      </Pane>28    </Fragment>29  ))30  .add('Examples', () => (31    <Fragment>32      <Pane mb={3}>33        <Heading as='h3' mb={1}>34          Uncontrolled35        </Heading>36        <Pane>37          <Checkbox>Checkbox label</Checkbox>38        </Pane>39        <Pane>40          <Checkbox checked>Checkbox label</Checkbox>41        </Pane>42        <Pane>43          <Checkbox checked color='darkest'>44            Checkbox label45          </Checkbox>46        </Pane>47        <Pane>48          <Checkbox checked color='danger'>49            Checkbox label50          </Checkbox>51        </Pane>52      </Pane>53      <Heading as='h3' mb={1}>54        Controlled55      </Heading>56      <Pane>57        <BooleanValue defaultValue>58          {({ value, toggle }) => (59            <Checkbox onClick={toggle} checked={value} size={2} color='gray'>60              Checkbox label61            </Checkbox>62          )}63        </BooleanValue>64      </Pane>65      <Pane>66        <BooleanValue>67          {({ value, toggle }) => (68            <Checkbox onClick={toggle} checked={value}>69              Checkbox label70            </Checkbox>71          )}72        </BooleanValue>73      </Pane>74      <Pane>75        <BooleanValue>76          {({ value, toggle }) => (77            <Checkbox onClick={toggle} checked={value} size={4}>78              Checkbox label79            </Checkbox>80          )}81        </BooleanValue>82      </Pane>83    </Fragment>...Using AI Code Generation
1var http = require('http');2var options = {3};4http.get(options, function(res) {5  var body = '';6  res.on('data', function(chunk) {7    body += chunk;8  });9  res.on('end', function() {10    var response = JSON.parse(body);11    console.log(response);12  });13}).on('error', function(e) {14  console.log("Got error: ", e);15});Using AI Code Generation
1var mb = require('mountebank');2var imposter = require('mountebank').create();3var predicate = require('mountebank').create().predicate;4var response = require('mountebank').create().response;5var stub = require('mountebank').create().stub;6var proxyResponse = require('mountebank').create().proxyResponse;Using AI Code Generation
1var mb = require('mountebank');2var mb = require('mountebank');3const mb = require('mountebank');4if(mb.booleanValue(true)){5    console.log("true");6}7else{8    console.log("false");9}Using AI Code Generation
1var assert = require('assert');2var mb = require('mountebank');3mb.create({ port: 2525, allowInjection: true }, function () {4    mb.post('/imposters', {5        stubs: [{6            responses: [{7                is: {8                }9            }]10        }]11    }, function (error, response) {12        assert.strictEqual(response.statusCode, 201);13            assert.strictEqual(response.body, 'Hello world!');14            mb.delete('/imposters', function () {15                mb.stop();16            });17        });18    });19});20    throw err;21at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)22at Function.Module._load (internal/modules/cjs/loader.js:507:25)23at Module.require (internal/modules/cjs/loader.js:637:17)24at require (internal/modules/cjs/helpers.js:22:18)25at Object. (C:\Users\user1\Documents\mb\test.js:3:12)26at Module._compile (internal/modules/cjs/loader.js:689:30)27at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)28at Module.load (internal/modules/cjs/loader.js:599:32)29at tryModuleLoad (internal/modules/cjs/loader.js:538:12)30at Function.Module._load (internal/modules/cjs/loader.js:530:3)31Your name to display (optional):32Your name to display (optional):33Your name to display (optional):Using AI Code Generation
1var jsdom = require('jsdom');2var fs = require('fs');3var jquery = fs.readFileSync('./node_modules/jquery/dist/jquery.js', 'utf-8');4jsdom.env({5    done: function (errors, window) {6        var $ = window.$;7        $.ajax({8            success: function (data) {9                var booleanValue = data.stubs[0].predicates[0].equals.booleanValue;10                console.log(booleanValue);11            }12        });13    }14});Using AI Code Generation
1var imposter = require('mountebank').createImposter(3000, 'http');2var stub = {3        {4            is: {5            }6        }7};8imposter.addStub(stub);9imposter.save(function (error, imposter) {10    if (error) {11        console.log('Error creating imposter: ' + error.message);12    }13    else {14        console.log('Imposter created at: ' + imposter.url);15    }16});17var imposter = require('mountebank').createImposter(3000, 'http');18var stub = {19        {20            is: {21            }22        }23};24imposter.addStub(stub);25imposter.save(function (error, imposter) {26    if (error) {27        console.log('Error creating imposter: ' + error.message);28    }29    else {30        console.log('Imposter created at: ' + imposter.url);31    }32});33var imposter = require('mountebank').createImposter(3000, 'http');34var stub = {35        {36            is: {37            }38        }39};40imposter.addStub(stub);41imposter.save(function (error, imposter) {42    if (error) {43        console.log('Error creating imposter: ' + error.message);44    }45    else {46        console.log('Imposter created at: ' + imposter.url);47    }48});49var imposter = require('mountebank').createImposter(3000, 'http');50var stub = {51        {52            is: {53            }54        }55};56imposter.addStub(stub);57imposter.save(function (error, imposter) {58    if (error) {59        console.log('Error creating imposter: ' + error.message);60    }61    else {62        console.log('Imposter created at: ' + imposter.url);63    }64});Using AI Code Generation
1var mb = require('mountebank');2var assert = require('assert');3var port = 2525;4var host = 'localhost';5var protocol = 'http';6var imposterPort = 2526;7var imposterProtocol = 'http';8var imposterHost = 'localhost';9var imposterPath = '/test';10var imposterStub = {11        {12            is: {13            }14        }15};16    {17        matches: {18        }19    }20];21var imposterStub = {22        {23            is: {24            }25        }26};27var imposter = {28    _links: {29        self: {30        }31    },32};33var imposterPath = '/imposters';34mb.create({ port: port, pidfile: 'mb.pid', logfile: 'mb.log' }, function (error, mbServer) {35    assert.ifError(error);36    mbServer.post(imposterUrl, imposter, function (error, response) {37        assert.ifError(error);38        assert.strictEqual(response.statusCode, 201);39        var imposterUrl = response.body._links.self.href;40        mbServer.get(imposterUrl, function (error, response) {41            assert.ifError(error);42            assert.strictEqual(response.statusCode, 200);43            assert.deepEqual(response.body, imposter);44            mbServer.del(imposterUrl, function (error, response) {45                assert.ifError(error);46                assert.strictEqual(response.statusCode, 200);47                mbServer.close();48            });49        });50    });51});52var mb = require('mountebank');53var assert = require('assert');54var port = 2525;55var host = 'localhost';56var protocol = 'http';57var imposterPort = 2526;58var imposterProtocol = 'http';59var imposterHost = 'localhost';Using AI Code Generation
1var mb = require('mountebank');2mb.create({ port: 2525 }, function(error, imposter) {3  console.log(imposter.port);4  imposter.addStub({5    responses: [{6      is: {7      }8    }]9  });10});11var mb = require('mountebank');12mb.create({ port: 2525 }, function(error, imposter) {13  console.log(imposter.port);14  imposter.addStub({15    responses: [{16      is: {17      }18    }]19  });20});Using AI Code Generation
1var mb = require('mountebank');2var mbHost = 'localhost';3var mbPort = 2525;4var booleanValue = new mb.BooleanValue(true);5var imposter = {6    stubs: [{7        responses: [{8            is: {9            }10        }]11    }]12};13var predicate = {14    equals: {15    }16};17var stub = {18    responses: [{19        is: {20        }21    }]22};23var request = {24};25mb.createImposter(mbPort, imposter, mbHost, function (error, imposter) {26    if (error) {27        console.log('Error creating imposter: ' + error.message);28    } else {29        console.log('Imposter created at ' + imposter.url);30    }31});32mb.addStub(imposter.port, stub, mbHost, function (error, stub) {33    if (error) {34        console.log('Error adding stub: ' + error.message);35    } else {36        console.log('Stub added');37    }38});39mb.verify(imposter.port, predicate, mbHost, function (error, result) {40    if (error) {41        console.log('Error verifying predicate: ' + error.message);42    } else {43        console.log('Predicate verified: ' + result);44    }45});46mb.sendRequest(imposter.port, request, mbHost, function (error, response) {47    if (error) {48        console.log('Error sending request: ' + error.message);49    } else {50        console.log('Request sent: ' + response.body);51    }52});53mb.deleteImposter(imposter.port, mbHost, function (error) {54    if (error) {55        console.log('Error deleting imposter: ' + error.message);56    } else {57        console.log('Imposter deleted');58    }59});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!!
