Best JavaScript code snippet using mountebank
utils-spec.js
Source:utils-spec.js
...90 const testPredicate = (newParams, expected) => {91 expect(predicate(explorePageLocationChanged(generateRouteState(newParams)))).to.be.equal(expected);92 };93 it('returns false if location is not changed', () => {94 testPredicate(originalParams, false);95 });96 it('returns false if any action, except EXPLORE_PAGE_LOCATION_CHANGED action is received', () => {97 expect(predicate({ ...explorePageLocationChanged(originalParams, differentParams), type: 'foo' })).to.be.false;98 expect(predicate({ type: 'foo' })).to.be.false;99 testPredicate(differentParams, true);100 });101 it('returns true if location.path is changed', () => {102 testPredicate({103 ...originalParams,104 path: differentParams.path105 }, true);106 // treat navigation to details page as location change107 testPredicate({108 ...originalParams,109 pageType: PageTypes.details110 }, true);111 // should return false if we are navigating to graph/wiki tab112 testPredicate({113 ...originalParams,114 pageType: PageTypes.graph115 }, false);116 testPredicate({117 ...originalParams,118 pageType: PageTypes.wiki119 }, false);120 });121 it('returns true if location.query is changed', () => {122 testPredicate({123 ...originalParams,124 query: differentParams.query125 }, true);126 testPredicate({127 ...originalParams,128 query: null129 }, true);130 testPredicate({131 ...originalParams,132 query: {}133 }, true);134 });135 it('returns false if location.state is changed', () => {136 testPredicate({137 ...originalParams,138 state: differentParams.state139 }, false);140 testPredicate({141 ...originalParams,142 state: null143 }, false);144 testPredicate({145 ...originalParams,146 state: {}147 }, false);148 });149 it('returns false if location is changed due to the fact, that a dataset was saved as other dataset', () => {150 // In that case we set state.afterDatasetSave = true151 testPredicate({152 ...differentParams,153 state: {154 ...differentParams.state,155 afterDatasetSave: true156 }157 }, false);158 });159 });160 describe('getActionPredicate', () => {161 describe('without entity', () => {162 it('should return true only if action.type is in actionTypes', () => {163 expect(getActionPredicate('SUCCESS')({type: 'FOO'})).to.be.false;164 expect(getActionPredicate(['SUCCESS'])({type: 'FOO'})).to.be.false;165 expect(getActionPredicate('SUCCESS')({type: 'SUCCESS'})).to.be.true;...
predicate-date.test.ts
Source:predicate-date.test.ts
1import { testPredicate } from "./__testutils__/testPredicate";2import * as prismic from "../src";3testPredicate(4 '[date.after(document.first_publication_date, "2017-05-18T17:00:00-0500")]',5 prismic.predicate.dateAfter(6 "document.first_publication_date",7 "2017-05-18T17:00:00-0500",8 ),9);10testPredicate(11 "[date.after(document.last_publication_date, 1495080000000)]",12 prismic.predicate.dateAfter("document.last_publication_date", 1495080000000),13);14testPredicate(15 '[date.after(my.article.release-date, "2017-01-22")]',16 prismic.predicate.dateAfter("my.article.release-date", "2017-01-22"),17);18testPredicate(19 "[date.after(my.article.release-date, 1485043200000)]",20 prismic.predicate.dateAfter(21 "my.article.release-date",22 Date.parse("2017-01-22"),23 ),24);25testPredicate(26 '[date.before(document.first_publication_date, "2016-09-19T14:00:00-0400")]',27 prismic.predicate.dateBefore(28 "document.first_publication_date",29 "2016-09-19T14:00:00-0400",30 ),31);32testPredicate(33 "[date.before(document.last_publication_date, 1476504000000)]",34 prismic.predicate.dateBefore("document.last_publication_date", 1476504000000),35);36testPredicate(37 '[date.before(my.post.date, "2017-02-24")]',38 prismic.predicate.dateBefore("my.post.date", "2017-02-24"),39);40testPredicate(41 "[date.before(my.post.date, 1487894400000)]",42 prismic.predicate.dateBefore("my.post.date", Date.parse("2017-02-24")),43);44testPredicate(45 '[date.between(document.first_publication_date, "2017-01-16", "2017-01-20")]',46 prismic.predicate.dateBetween(47 "document.first_publication_date",48 "2017-01-16",49 "2017-01-20",50 ),51);52testPredicate(53 '[date.between(document.last_publication_date, "2016-09-15T05:30:00+0100", "2017-10-15T11:45:00+0100")]',54 prismic.predicate.dateBetween(55 "document.last_publication_date",56 "2016-09-15T05:30:00+0100",57 "2017-10-15T11:45:00+0100",58 ),59);60testPredicate(61 "[date.between(my.query-fields.date, 1483074000000, 1483333200000)]",62 prismic.predicate.dateBetween(63 "my.query-fields.date",64 1483074000000,65 1483333200000,66 ),67);68testPredicate(69 "[date.between(my.query-fields.date, 1583074000000, 1583333200000)]",70 prismic.predicate.dateBetween(71 "my.query-fields.date",72 new Date(1583074000000),73 new Date(1583333200000),74 ),75);76testPredicate(77 "[date.day-of-month(document.first_publication_date, 22)]",78 prismic.predicate.dateDayOfMonth("document.first_publication_date", 22),79);80testPredicate(81 "[date.day-of-month(document.last_publication_date, 30)]",82 prismic.predicate.dateDayOfMonth("document.last_publication_date", 30),83);84testPredicate(85 "[date.day-of-month(my.post.date, 14)]",86 prismic.predicate.dateDayOfMonth("my.post.date", 14),87);88testPredicate(89 "[date.day-of-month-after(document.first_publication_date, 22)]",90 prismic.predicate.dateDayOfMonthAfter("document.first_publication_date", 22),91);92testPredicate(93 "[date.day-of-month-after(document.last_publication_date, 10)]",94 prismic.predicate.dateDayOfMonthAfter("document.last_publication_date", 10),95);96testPredicate(97 "[date.day-of-month-after(my.event.date-and-time, 15)]",98 prismic.predicate.dateDayOfMonthAfter("my.event.date-and-time", 15),99);100testPredicate(101 "[date.day-of-month-before(document.first_publication_date, 20)]",102 prismic.predicate.dateDayOfMonthBefore("document.first_publication_date", 20),103);104testPredicate(105 "[date.day-of-month-before(document.last_publication_date, 10)]",106 prismic.predicate.dateDayOfMonthBefore("document.last_publication_date", 10),107);108testPredicate(109 "[date.day-of-month-before(my.blog-post.release-date, 23)]",110 prismic.predicate.dateDayOfMonthBefore("my.blog-post.release-date", 23),111);112testPredicate(113 '[date.day-of-week(document.first_publication_date, "monday")]',114 prismic.predicate.dateDayOfWeek("document.first_publication_date", "monday"),115);116testPredicate(117 '[date.day-of-week(document.last_publication_date, "sun")]',118 prismic.predicate.dateDayOfWeek("document.last_publication_date", "sun"),119);120testPredicate(121 '[date.day-of-week(my.concert.show-date, "Friday")]',122 prismic.predicate.dateDayOfWeek("my.concert.show-date", "Friday"),123);124testPredicate(125 "[date.day-of-week(my.concert.show-date, 5)]",126 prismic.predicate.dateDayOfWeek("my.concert.show-date", 5),127);128testPredicate(129 '[date.day-of-week-after(document.first_publication_date, "fri")]',130 prismic.predicate.dateDayOfWeekAfter(131 "document.first_publication_date",132 "fri",133 ),134);135testPredicate(136 '[date.day-of-week-after(document.last_publication_date, "Thu")]',137 prismic.predicate.dateDayOfWeekAfter("document.last_publication_date", "Thu"),138);139testPredicate(140 '[date.day-of-week-after(my.blog-post.date, "tuesday")]',141 prismic.predicate.dateDayOfWeekAfter("my.blog-post.date", "tuesday"),142);143testPredicate(144 "[date.day-of-week-after(my.blog-post.date, 2)]",145 prismic.predicate.dateDayOfWeekAfter("my.blog-post.date", 2),146);147testPredicate(148 '[date.day-of-week-before(document.first_publication_date, "Wed")]',149 prismic.predicate.dateDayOfWeekBefore(150 "document.first_publication_date",151 "Wed",152 ),153);154testPredicate(155 '[date.day-of-week-before(document.last_publication_date, "saturday")]',156 prismic.predicate.dateDayOfWeekBefore(157 "document.last_publication_date",158 "saturday",159 ),160);161testPredicate(162 '[date.day-of-week-before(my.page.release-date, "Saturday")]',163 prismic.predicate.dateDayOfWeekBefore("my.page.release-date", "Saturday"),164);165testPredicate(166 "[date.day-of-week-before(my.page.release-date, 6)]",167 prismic.predicate.dateDayOfWeekBefore("my.page.release-date", 6),168);169testPredicate(170 '[date.month(document.first_publication_date, "august")]',171 prismic.predicate.dateMonth("document.first_publication_date", "august"),172);173testPredicate(174 '[date.month(document.last_publication_date, "Sep")]',175 prismic.predicate.dateMonth("document.last_publication_date", "Sep"),176);177testPredicate(178 "[date.month(my.blog-post.date, 1)]",179 prismic.predicate.dateMonth("my.blog-post.date", 1),180);181testPredicate(182 '[date.month-after(document.first_publication_date, "February")]',183 prismic.predicate.dateMonthAfter(184 "document.first_publication_date",185 "February",186 ),187);188testPredicate(189 "[date.month-after(document.last_publication_date, 6)]",190 prismic.predicate.dateMonthAfter("document.last_publication_date", 6),191);192testPredicate(193 '[date.month-after(my.article.date, "oct")]',194 prismic.predicate.dateMonthAfter("my.article.date", "oct"),195);196testPredicate(197 "[date.month-before(document.first_publication_date, 8)]",198 prismic.predicate.dateMonthBefore("document.first_publication_date", 8),199);200testPredicate(201 '[date.month-before(document.last_publication_date, "june")]',202 prismic.predicate.dateMonthBefore("document.last_publication_date", "june"),203);204testPredicate(205 '[date.month-before(my.blog-post.release-date, "Sep")]',206 prismic.predicate.dateMonthBefore("my.blog-post.release-date", "Sep"),207);208testPredicate(209 "[date.year(document.first_publication_date, 2016)]",210 prismic.predicate.dateYear("document.first_publication_date", 2016),211);212testPredicate(213 "[date.year(document.last_publication_date, 2017)]",214 prismic.predicate.dateYear("document.last_publication_date", 2017),215);216testPredicate(217 "[date.year(my.employee.birthday, 1986)]",218 prismic.predicate.dateYear("my.employee.birthday", 1986),219);220testPredicate(221 "[date.hour(document.first_publication_date, 12)]",222 prismic.predicate.dateHour("document.first_publication_date", 12),223);224testPredicate(225 "[date.hour(document.last_publication_date, 8)]",226 prismic.predicate.dateHour("document.last_publication_date", 8),227);228testPredicate(229 "[date.hour(my.event.date-and-time, 19)]",230 prismic.predicate.dateHour("my.event.date-and-time", 19),231);232testPredicate(233 "[date.hour-after(document.first_publication_date, 21)]",234 prismic.predicate.dateHourAfter("document.first_publication_date", 21),235);236testPredicate(237 "[date.hour-after(document.last_publication_date, 8)]",238 prismic.predicate.dateHourAfter("document.last_publication_date", 8),239);240testPredicate(241 "[date.hour-after(my.blog-post.releaseDate, 16)]",242 prismic.predicate.dateHourAfter("my.blog-post.releaseDate", 16),243);244testPredicate(245 "[date.hour-before(document.first_publication_date, 10)]",246 prismic.predicate.dateHourBefore("document.first_publication_date", 10),247);248testPredicate(249 "[date.hour-before(document.last_publication_date, 12)]",250 prismic.predicate.dateHourBefore("document.last_publication_date", 12),251);252testPredicate(253 "[date.hour-before(my.event.dateAndTime, 12)]",254 prismic.predicate.dateHourBefore("my.event.dateAndTime", 12),...
test-precondition.js
Source:test-precondition.js
...3module.exports = (predicate, target) => {4 if (typeof predicate === 'string') {5 predicate = JSON.parse(predicate);6 }7 return testPredicate(predicate, target);8}9const operators = {10 equals: ([key, val, defaultVal], target) => {11 return lodash.get(target, key, defaultVal) === val;12 },13 every: (args, target) => {14 return args15 .map(arg => testPredicate(arg, target))16 .every(arg => !!arg);17 },18 regexp: ([key, regexp, defaultVal], target) => {19 return new RegExp(regexp).test(lodash.get(target, key, defaultVal));20 },21 some: (args, target) => {22 return args23 .map(arg => testPredicate(arg, target))24 .some(arg => !!arg);25 }26};27function testPredicate(ast, target) {28 const [[ op, arg ]] = Object.entries(ast);29 return !!operators[op](arg, target);...
Using AI Code Generation
1var request = require('request');2var options = {3 headers: {4 },5 json: {6 {7 {8 "is": {9 "headers": {10 },11 }12 }13 {14 {15 "equals": {16 }17 },18 {19 "equals": {20 }21 }22 }23 }24 }25};26request(options, function (error, response, body) {27 if (!error && response.statusCode == 201) {28 console.log(body);29 }30});31var request = require('request');32var options = {33};34request(options, function (error, response, body) {35 if (!error && response.statusCode == 200) {36 console.log(body);37 }38});39var request = require('request');40var options = {
Using AI Code Generation
1var testPredicate = require('mountebank').testPredicate;2var predicate = {3 equals: {4 }5};6var request = {7};8var result = testPredicate(predicate, request);9var request = {10};11var result = testPredicate(predicate, request);12var predicate = {13 startsWith: {14 }15};16var request = {17};18var result = testPredicate(predicate, request);19var request = {20};21var result = testPredicate(predicate, request);22var request = {23};24var result = testPredicate(predicate, request);25var predicate = {26 startsWith: {27 },28 and: {29 equals: {30 }31 }32};33var request = {34};35var result = testPredicate(predicate, request);36var request = {37};38var result = testPredicate(predicate, request);39var request = {40};41var result = testPredicate(predicate, request);42var request = {43};44var result = testPredicate(predicate, request);45var request = {46};47var result = testPredicate(predicate, request);48var predicate = {49 startsWith: {50 },51 or: {52 equals: {53 }54 }55};56var request = {
Using AI Code Generation
1var testPredicate = require('mountebank').testPredicate;2var predicate = {3 equals: {4 }5};6var request = {7};8console.log(testPredicate(predicate, request));9var testPredicate = require('mountebank').testPredicate;10var predicate = {11 equals: {12 }13};14var request = {15};16console.log(testPredicate(predicate, request));17var testPredicate = require('mountebank').testPredicate;18var predicate = {19 equals: {20 }21};22var request = {23};24console.log(testPredicate(predicate, request));25var testPredicate = require('mountebank').testPredicate;26var predicate = {27 equals: {28 }29};30var request = {31 query: {32 }33};34console.log(testPredicate(predicate, request));35var testPredicate = require('mountebank').testPredicate;36var predicate = {37 equals: {38 query: {39 }40 }41};42var request = {43 query: {44 }45};46console.log(testPredicate(predicate, request));47var testPredicate = require('mountebank').testPredicate;48var predicate = {49 equals: {
Using AI Code Generation
1var request = require('request');2var assert = require('assert');3var options = {4 'headers': {5 },6 body: JSON.stringify({7 {8 "equals": {9 "headers": {10 },11 }12 }13 })14};15request(options, function (error, response) {16 if (error) throw new Error(error);17 console.log(response.body);18});19const express = require('express');20const app = express();21const port = 3000;22app.get('/', (req, res) => res.send('Hello World!'));23app.listen(port, () => console.log(`Example app listening on port ${port}!`));24test('Port 3000', () => {25 expect(port).toBe(3000);26});27pm.test("Status code is 200", function () {28 pm.response.to.have.status(200);29});30pm.test("Response time is less than 200ms", function () {31 pm.expect(pm.response.responseTime).to.be.below(200);32});33pm.test("Content-Type is application/json", function () {34 var header = pm.response.headers.get("Content-Type");35 pm.expect(header).to.include("application/json");36});37pm.test("Response has body", function ()
Using AI Code Generation
1var testPredicate = require('mountebank').testPredicate;2var assert = require('assert');3var predicate = {4 equals: {5 }6};7var request = {8};9assert(testPredicate(predicate, request));10var testPredicate = require('mountebank').testPredicate;11var assert = require('assert');12var predicate = {13 equals: {14 }15};16var request = {17};18assert(testPredicate(predicate, request));
Using AI Code Generation
1var assert = require('assert');2var mb = require('mountebank');3var imposter = {4 {5 predicates: [ { equals: { path: '/test' } } ],6 responses: [ { is: { body: 'OK' } } ]7 }8};9mb.create(imposter, function (error, instance) {10 assert.ifError(error);11 console.log('Imposter created at: ' + instance.url);12 console.log('Testing predicate');13 mb.testPredicate(instance.url, { path: '/test' }, function (error, response) {14 assert.ifError(error);15 console.log('Predicate response: ' + response.body);16 instance.stop();17 });18});
Using AI Code Generation
1var mb = require('mountebank');2mb.start({3}, function () {4 console.log('Mountebank started');5 mb.post('/imposters', {6 {7 {8 equals: {9 }10 }11 {12 is: {13 }14 }15 }16 }, function (error, response) {17 console.log('Imposter created');18 });19});20var mb = require('mountebank');21mb.start({22}, function () {23 console.log('Mountebank started');24 mb.post('/imposters', {25 {26 {27 equals: {28 }29 }30 {31 is: {32 }33 }34 }35 }, function (error, response) {36 console.log('Imposter created');37 });38});39var request = require('request-promise');40var mb = require('mountebank');41mb.start({42}, function () {43 console.log('Mountebank started');44 mb.post('/imposters', {45 {46 {47 equals: {48 }49 }50 {51 is: {52 }53 }54 }55 }, function (error, response) {56 console.log('Imposter created');57 console.log(response);58 mb.stop();59 });
Using AI Code Generation
1const request = require('request-promise');2const logger = require('winston');3const testPredicate = (request, response) => {4 logger.info('request received');5 response.statusCode = 200;6 response.headers['Content-Type'] = 'application/json';7 response.body = JSON.stringify({ message: 'request received' });8};9request({10 body: { equals: { method: 'GET', path: '/test' } },11}).then((response) => {12 logger.info('successfully added predicate');13 logger.info('response body', response.body);14 return request({15 body: {16 stubs: [{17 predicates: [{18 }],19 responses: [{20 is: {21 headers: {22 },23 body: JSON.stringify({ message: 'hello world' }),24 },25 }],26 }],27 },28 });29}).then((response) => {30 logger.info('successfully added stub');31 logger.info('response body', response.body);32 return request({33 });34}).then((response) => {35 logger.info('successfully received response');36 logger.info('response body', response.body);37}).catch((err) => {38 logger.error('error adding stub', err.message);39});
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!!