Best JavaScript code snippet using mountebank
pair-matcher-spec.ts
Source:pair-matcher-spec.ts
...5 // -- Arrange6 const matcher = new PairMatcher(/abc/, /ghi/);7 const source = 'abc def ghi';8 // -- Act9 const match = matcher.findFirstMatch(source);10 // -- Assert11 expect(match).toBeDefined();12 expect(match?.matched).toEqual(source);13 expect(match?.before).toEqual('');14 expect(match?.after).toEqual('');15 expect(match?.index).toEqual(0);16 expect(match?.length).toEqual(source.length);17 expect(match?.groups).toHaveSize(1);18 expect(match?.groups).toEqual([' def ']);19 });20 it('Finds the match of a simple regex pair in a simple string (#2)', () => {21 // -- Arrange22 const matcher = new PairMatcher(/bc/, /gh/);23 const source = 'abc def ghi';24 const expectedMatched = 'bc def gh';25 // -- Act26 const match = matcher.findFirstMatch(source);27 // -- Assert28 expect(match).toBeDefined();29 expect(match?.matched).toEqual(expectedMatched);30 expect(match?.before).toEqual('a');31 expect(match?.after).toEqual('i');32 expect(match?.index).toEqual(1);33 expect(match?.length).toEqual(expectedMatched.length);34 expect(match?.groups).toHaveSize(1);35 expect(match?.groups).toEqual([' def ']);36 });37 it('Finds the match of a simple regex pair with capture groups in a simple string', () => {38 // -- Arrange39 const matcher = new PairMatcher(/(b)(c d)/, /(f g)(h)/);40 const source = 'abc def ghi';41 const expectedMatched = 'bc def gh';42 // -- Act43 const match = matcher.findFirstMatch(source);44 // -- Assert45 expect(match).toBeDefined();46 expect(match?.matched).toEqual(expectedMatched);47 expect(match?.before).toEqual('a');48 expect(match?.after).toEqual('i');49 expect(match?.index).toEqual(1);50 expect(match?.length).toEqual(expectedMatched.length);51 expect(match?.groups).toHaveSize(5);52 expect(match?.groups).toEqual(['b', 'c d', 'e', 'f g', 'h']);53 });54 it('Finds the match of a simple regex pair with capture groups in a string with multiple pairs', () => {55 // -- Arrange56 const matcher = new PairMatcher(/</, />/);57 const source = '<abc><def><ghi>';58 const expectedMatched = '<abc>';59 // -- Act60 const match = matcher.findFirstMatch(source);61 // -- Assert62 expect(match).toBeDefined();63 expect(match?.matched).toEqual(expectedMatched);64 expect(match?.before).toEqual('');65 expect(match?.after).toEqual('<def><ghi>');66 expect(match?.index).toEqual(0);67 expect(match?.length).toEqual(expectedMatched.length);68 expect(match?.groups).toHaveSize(1);69 expect(match?.groups).toEqual(['abc']);70 });71 it('Finds the match of a simple regex pair with capture groups in a string with nested pairs', () => {72 // -- Arrange73 const matcher = new PairMatcher(/</, />/);74 const source = 'a<bc<>d<ef>gh<i<j>>k>lm';75 const expectedMatched = '<bc<>d<ef>gh<i<j>>k>';76 // -- Act77 const match = matcher.findFirstMatch(source);78 // -- Assert79 expect(match).toBeDefined();80 expect(match?.matched).toEqual(expectedMatched);81 expect(match?.before).toEqual('a');82 expect(match?.after).toEqual('lm');83 expect(match?.index).toEqual(1);84 expect(match?.length).toEqual(expectedMatched.length);85 expect(match?.groups).toHaveSize(1);86 expect(match?.groups).toEqual(['bc<>d<ef>gh<i<j>>k']);87 });88 it('Finds the match of a simple regex pair with capture groups in a complex string', () => {89 // -- Arrange90 const matcher = new PairMatcher(/</, />/);91 const source = 'a<<bc><><d>e>fg<h>i';92 const expectedMatched = '<<bc><><d>e>';93 // -- Act94 const match = matcher.findFirstMatch(source);95 // -- Assert96 expect(match).toBeDefined();97 expect(match?.matched).toEqual(expectedMatched);98 expect(match?.before).toEqual('a');99 expect(match?.after).toEqual('fg<h>i');100 expect(match?.index).toEqual(1);101 expect(match?.length).toEqual(expectedMatched.length);102 expect(match?.groups).toHaveSize(1);103 expect(match?.groups).toEqual(['<bc><><d>e']);104 });105 it('Returns undefined when it does NOT find any match', () => {106 // -- Arrange107 const matcher = new PairMatcher(/abc/, /def/);108 const source = 'be';109 // -- Act110 const match = matcher.findFirstMatch(source);111 // -- Assert112 expect(match).toBeUndefined();113 });114 it('Returns undefined when it does NOT find an opening match', () => {115 // -- Arrange116 const matcher = new PairMatcher(/abc/, /def/);117 const source = 'def';118 // -- Act119 const match = matcher.findFirstMatch(source);120 // -- Assert121 expect(match).toBeUndefined();122 });123 it('Returns undefined when it does NOT find a closing match', () => {124 // -- Arrange125 const matcher = new PairMatcher(/abc/, /def/);126 const source = 'abc';127 // -- Act128 const match = matcher.findFirstMatch(source);129 // -- Assert130 expect(match).toBeUndefined();131 });132 });...
naive.ts
Source:naive.ts
...34 return null;35 }36 return parseInt(yieldStr.replace('servings', ''));37 }38 function findFirstMatch(selectors) {39 for (var i = 0; i < selectors.length; i++) {40 var el = document.querySelector(selectors[i]);41 if (el) {42 return el;43 }44 }45 return null;46 }47 function findFirstMatches(selectors) {48 for (var i = 0; i < selectors.length; i++) {49 var els = document.querySelectorAll(selectors[i]);50 if (els && els.length) {51 return els;52 }53 }54 return null;55 }56 var titleElement = findFirstMatch(['#title', '.title', '.recipe h1', 'h1']);57 var authorElement = findFirstMatch([58 '.recipe .author',59 '.recipe [class*="author"]',60 '.author',61 '[class*="author"',62 ]);63 var prepTimeElement = findFirstMatch([64 '.recipe .prep-time',65 '.recipe .prepTime',66 '.recipe [class*="prep"][class*="time"]',67 '.prep-time',68 '.prepTime',69 ]);70 var cookTimeElement = findFirstMatch([71 '.recipe .cook-time',72 '.recipe .cookTime',73 '.recipe [class*="cook"][class*="time"]',74 '.cook-time',75 '.cookTime',76 ]);77 var totalTimeElement = findFirstMatch([78 '.recipe .total-time',79 '.recipe .totalTime',80 '.recipe [class*="total"][class*="time"]',81 '.total-time',82 '.totalTime',83 ]);84 var servingsElement = findFirstMatch([85 '.recipe .servings',86 '.recipe [class*="servings"]',87 '.servings',88 '[class*="servings"]',89 ]);90 var ingredientsList = findFirstMatches([91 '.recipe .ingredients .ingredient',92 '.ingredients .ingredient',93 'li.ingredient',94 'ul.ingredients li',95 '.ingredients > *',96 ]);97 var instructionsList = findFirstMatches([98 '.recipe .instructions .instruction',99 '.recipe .steps .step',100 '.instructions .instruction',101 '.steps .step',102 'li.instruction',103 'li.step',104 'ul.instructions li',105 'ul.steps li',106 '.instructions > *',107 '.steps > *',108 ]);109 var imageElement = findFirstMatch([110 '.recipe img.image',111 '.recipe img[class*="image"]',112 '.recipe img',113 'img',114 ]);115 var ingredientsText = [];116 (ingredientsList || []).forEach(function(el) {117 ingredientsText.push(textContent(el));118 });119 var stepsText = [];120 (instructionsList || []).forEach(function(el) {121 stepsText.push(textContent(el));122 });123 var prepTime = toIsoDuration(textContent(prepTimeElement));...
findFirstMatchInSorted.js
Source:findFirstMatchInSorted.js
...7const findFirstMatch = (arr, pred, firstKey = 0, lastKey = arr.length - 1) => {8 // console.log(firstKey , lastKey)9 if (pred(arr[firstKey])) return arr[firstKey];10 if (!pred(arr[lastKey])) return;11 const res = findFirstMatch(arr, pred, firstKey, Math.floor((lastKey + firstKey) / 2));12 if (res) return res;13 return findFirstMatch(arr, pred, Math.floor((lastKey + firstKey) / 2) + 1, lastKey);14};15console.time('divide')16console.log(findFirstMatch(arr, predToFind));...
Using AI Code Generation
1const mb = require('mountebank');2const imposter = {3 stubs: [{4 predicates: [{ equals: { method: 'GET', path: '/test' } }],5 responses: [{ is: { body: 'Hello, World!' } }]6 }]7};8mb.create(imposter).then(() => {9 return mb.findFirstMatch({ port: 3000, path: '/test' })10 .then(response => {11 console.log(response.body);12 });13});14const mb = require('mountebank');15const imposter = {16 stubs: [{17 predicates: [{ equals: { method: 'GET', path: '/test' } }],18 responses: [{ is: { body: 'Hello, World!' } }]19 }]20};21mb.create(imposter).then(() => {22 return mb.findFirstMatch({ port: 3000, path: '/test' })23 .then(response => {24 console.log(response.body);25 });26});27const mb = require('mountebank');28const imposter = {29 stubs: [{30 predicates: [{ equals: { method: 'GET', path: '/test' } }],31 responses: [{ is: { body: 'Hello, World!' } }]32 }]33};34mb.create(imposter).then(() => {35 return mb.findFirstMatch({ port: 3000, path: '/test' })36 .then(response => {37 console.log(response.body);38 });39});40const mb = require('mountebank');41const imposter = {42 stubs: [{43 predicates: [{ equals: { method: 'GET', path: '/test' } }],44 responses: [{ is: { body: 'Hello, World!' } }]45 }]46};47mb.create(imposter).then(() => {48 return mb.findFirstMatch({ port: 3000, path: '/test' })49 .then(response
Using AI Code Generation
1var mb = require('mountebank');2mb.create().then(function (server) {3 var imposter = {4 {5 {6 is: {7 }8 }9 }10 };11 return server.createImposter(imposter);12}).then(function (imposter) {13 return imposter.findFirstMatch({14 });15}).then(function (response) {16 console.log(response.body);17 process.exit(0);18}).catch(function (error) {19 console.error(error);20 process.exit(1);21});22var mb = require('mountebank');23mb.create().then(function (server) {24 var imposter = {25 {26 {27 is: {28 }29 }30 }31 };32 return server.createImposter(imposter);33}).then(function (imposter) {34 return imposter.find({35 });36}).then(function (responses) {37 console.log(responses[0].body);38 process.exit(0);39}).catch(function (error) {40 console.error(error);41 process.exit(1);42});43var mb = require('mountebank');44mb.create().then(function (server) {45 var imposter = {46 {47 {48 is: {49 }50 }51 }52 };53 return server.createImposter(imposter);54}).then(function (imposter) {55 return imposter.addStub({56 {
Using AI Code Generation
1const mb = require('mountebank');2const path = require('path');3const imposters = {4 {5 {6 equals: {7 }8 }9 {10 is: {11 }12 }13 }14};15mb.create({ port: 2525, pidfile: 'mb.pid', logfile: 'mb.log' }, function (error, mbServer) {16 mbServer.post('/imposters', imposters, function (error, response) {17 console.log(`POST /imposters: ${response.statusCode}`);18 console.log(response.body);19 mbServer.get('/imposters', function (error, response) {20 console.log(`GET /imposters: ${response.statusCode}`);21 console.log(response.body);22 mbServer.findFirstMatch('/test', function (error, match) {23 console.log(`findFirstMatch /test: ${match.statusCode}`);24 console.log(match.body);25 mbServer.stop(function () {26 console.log('mbServer stopped');27 });28 });29 });30 });31});32{33 "scripts": {34 },
Using AI Code Generation
1var mb = require('mountebank');2var assert = require('assert');3var request = require('request');4var port = 2525;5mb.start(port, function (error) {6 if (error) {7 console.error(error.message);8 } else {9 console.log("mountebank started");10 }11});12var imposter = {13 {14 {15 is: {16 }17 }18 }19};20mb.create(imposter, function (error, imposter) {21 if (error) {22 console.error(error.message);23 } else {24 console.log("Imposter created");25 }26});27var options = {28};29request(options, function (error, response, body) {30 if (error) {31 console.error(error.message);32 } else {33 console.log("Request sent");34 }35});36mb.findFirstMatch(imposter, options, function (error, match) {37 if (error) {38 console.error(error.message);39 } else {40 console.log("Match found");41 }42});43mb.stop(port, function (error) {44 if (error) {45 console.error(error.message);46 } else {47 console.log("mountebank stopped");48 }49});50{51 "scripts": {52 },53 "dependencies": {54 }55}
Using AI Code Generation
1var mb = require('mountebank'),2 promise = require('bluebird'),3 assert = require('assert'),4 mbHelper = require('./mbHelper'),5 request = require('request'),6 util = require('util'),7 fs = require('fs');8mb.create({9 {10 {11 is: {12 }13 }14 }15}).then(function (server) {16 return mbHelper.waitForServer(server.port);17}).then(function (port) {18 return promise.promisify(request)({19 });20}).then(function (response) {21 assert.strictEqual(response.statusCode, 200);22 assert.strictEqual(response.body, 'Hello World!');23 return mbHelper.getRequests(2525);24}).then(function (requests) {25 assert.strictEqual(requests.length, 1);26 assert.strictEqual(requests[0].path, '/');27 return mbHelper.getFirstRequest(2525);28}).then(function (request) {29 assert.strictEqual(request.path, '/');30 return mbHelper.getLogs(2525);31}).then(function (logs) {32 assert.strictEqual(logs.length, 1);33 assert.strictEqual(logs[0].message, 'request received');34 return mbHelper.getFirstLog(2525);35}).then(function (log) {36 assert.strictEqual(log.message, 'request received');37 return mbHelper.getPredicates(2525);38}).then(function (predicates) {39 assert.strictEqual(predicates.length, 1);40 assert.strictEqual(predicates[0].equals.path, '/');41 return mbHelper.getFirstPredicate(2525);42}).then(function (predicate) {43 assert.strictEqual(predicate.equals.path, '/');44 return mbHelper.getResponses(2525);45}).then(function (responses) {46 assert.strictEqual(responses.length, 1);47 assert.strictEqual(responses[0].is.body
Using AI Code Generation
1var mb = require('mountebank'),2 assert = require('assert'),3 Q = require('q'),4 util = require('util'),5 request = require('request');6var port = 2525;7var server = mb.create({ port: port, pidfile: 'mb.pid', logfile: 'mb.log', protofile: 'mb.proto' });8server.start()9 .then(function () {10 var promise = Q.defer();11 {12 {13 {14 is: {15 }16 }17 }18 }19 ];20 server.createImposters(imposters, promise.makeNodeResolver());21 return promise.promise;22 })23 .then(function (response) {24 var promise = Q.defer();25 request.get(url, promise.makeNodeResolver());26 return promise.promise;27 })28 .then(function (response) {29 assert.strictEqual(response[0].statusCode, 200);30 assert.strictEqual(response[0].body, 'Hello, world!');31 return server.stop();32 })33 .done();34var mb = require('mountebank'),35 assert = require('assert'),36 Q = require('q'),37 util = require('util'),38 request = require('request');39var port = 2525;40var server = mb.create({ port: port, pidfile: 'mb.pid', logfile: 'mb.log', protofile: 'mb.proto' });41server.start()42 .then(function () {43 var promise = Q.defer();44 {45 {46 {47 is: {48 }49 }50 }51 }
Using AI Code Generation
1var imposter = require('mountebank').create(),2 assert = require('assert'),3 stub = {4 {5 is: {6 }7 }8 };9imposter.post('/path', stub, port, host, protocol, function (error, response) {10 assert.ifError(error);11 assert.strictEqual(response.statusCode, 201);12 imposter.findFirstMatch('/path', port, host, protocol, function (error, response) {13 assert.ifError(error);14 assert.strictEqual(response.statusCode, 200);15 assert.strictEqual(response.body, 'Hello world!');16 imposter.del(port, host, protocol, function (error, response) {17 assert.ifError(error);18 assert.strictEqual(response.statusCode, 200);19 console.log('done');20 });21 });22});23var imposter = require('mountebank').create(),24 assert = require('assert'),25 stub = {26 {27 is: {28 }29 }30 };31imposter.findFirstMatch('/path', port, host, protocol, function (error, response) {32 assert.ifError(error);33 assert.strictEqual(response.statusCode, 200);34 assert.strictEqual(response.body, 'Hello world!');35 imposter.del(port, host, protocol, function (error, response) {36 assert.ifError(error);37 assert.strictEqual(response.statusCode, 200);38 console.log('done');39 });40});41assert.ifError(error);42Your name to display (optional):43Your name to display (optional):44Your name to display (optional):
Using AI Code Generation
1var mb = require('mountebank'),2mbServer = mb.create(port);3mbServer.start()4.then(function () {5return mbServer.get('/test', { text: 'Hello from Mountebank!' });6})7.then(function () {8return mbServer.findFirstMatch('/test', 'GET');9})10.then(function (response) {11console.log(response);12})13.then(function () {14mbServer.stop();15})16.catch(function (error) {17console.error(error);18mbServer.stop();19});20var mb = require('mountebank'),21mbServer = mb.create(port);22mbServer.start()23.then(function () {24return mbServer.get('/test', { text: 'Hello from Mountebank!' });25})26.then(function () {27return mbServer.findFirstMatch('/test', 'GET');28})29.then(function (response) {30console.log(response);31})32.then(function () {33mbServer.stop();34})35.catch(function (error) {36console.error(error);37mbServer.stop();38});39{ statusCode: 200,40headers: { 'Content-Type': 'text/plain' },41body: 'Hello from Mountebank!' }42{ statusCode: 404,43headers: { 'Content-Type': 'text/plain' },44body: 'Resource not found' }
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!!