Best JavaScript code snippet using fast-check-monorepo
process-template.js
Source:process-template.js
1'use strict';2const assert = require('assert');3// const http = require('http');4const nock = require('nock');5const sinon = require('sinon');6const stream = require('stream');7const processTemplate = require('../lib/process-template');8const requestFragment = require('../lib/request-fragment')(9 require('../lib/filter-headers'),10 require('../lib/process-fragment-response')11);12const AsyncStream = require('../lib/streams/async-stream');13const parseTemplate = require('../lib/parse-template');14describe('processTemplate', () => {15 let resultStream;16 let options;17 let handleNestedTag;18 beforeEach(() => {19 const mockedRequest = {20 headers: {},21 url: '/'22 };23 nock('http://fragment')24 .get('/f1')25 .reply(200, '<NormalFragment 1/>', {26 Link:27 '<http://link1>; rel="fragment-script", <http://link2>; rel="fragment-script", <http://link3>; rel="fragment-script",' +28 '<http://link4>; rel="fragment-script", <http://link5>; rel="fragment-script", <http://link6>; rel="fragment-script"'29 })30 .get('/f2')31 .reply(200, '<NormalFragment 2/>')32 .get('/f3')33 .reply(200, '<NormalFragment 3/>')34 .get('/r1')35 .reply(200, '<RemoteFragment 1/>')36 .get('/r2')37 .reply(200, '<RemoteFragment 2/>')38 .get('/primary')39 .reply(200, '<Primary/>')40 .get('/bad')41 .reply(500, 'Internal Error');42 handleNestedTag = (request, tag, options, context) => {43 if (tag && tag.name === 'nested-fragments') {44 // Simulate fetching of some remote resource here45 // TODO: who does the parsing???46 const stream = processTemplate(request, options, context);47 setTimeout(() => {48 const remoteEndpointResponse = `49 <fragment id="r1" src="http://fragment/r1" />50 <fragment id="r2" src="http://fragment/r2" />51 `;52 options53 .parseTemplate(remoteEndpointResponse, null, false)54 .then(template => {55 template.forEach(parsedTag => {56 stream.write(parsedTag);57 });58 stream.end();59 });60 }, 10);61 return stream;62 }63 return Buffer.from('');64 };65 let index = 0;66 options = {67 maxAssetLinks: 3,68 nextIndex: () => ++index,69 fragmentTag: 'fragment',70 parseTemplate: parseTemplate(71 ['fragment', 'nested-fragments'],72 ['script']73 ),74 asyncStream: new AsyncStream(),75 handleTag: handleNestedTag,76 requestFragment: requestFragment77 };78 resultStream = processTemplate(mockedRequest, options, {});79 });80 it('returns a stream', () => {81 assert(resultStream instanceof stream);82 });83 describe('result stream', () => {84 it('produces buffers as result', done => {85 resultStream.on('data', chunk => {86 assert(chunk instanceof Buffer);87 });88 resultStream.on('end', () => {89 done();90 });91 resultStream.on('fragment:found', ({ attributes: { primary } }) => {92 primary && options.asyncStream.end();93 });94 resultStream.write({ placeholder: 'pipe' });95 resultStream.write({96 name: 'fragment',97 attributes: { id: 'f3', async: true, src: 'http://fragment/f3' }98 });99 resultStream.write({100 name: 'fragment',101 attributes: { id: 'f1', src: 'http://fragment/f1' }102 });103 resultStream.write({104 name: 'fragment',105 attributes: {106 id: 'f2',107 primary: true,108 src: 'http://fragment/primary'109 }110 });111 resultStream.write({ name: 'nested-fragments' });112 resultStream.write({ placeholder: 'async' });113 resultStream.end();114 });115 it('notifies for every fragment found in the template', done => {116 const onFragment = sinon.spy();117 resultStream.on('fragment:found', onFragment);118 resultStream.on('end', () => {119 assert.equal(onFragment.callCount, 3);120 done();121 });122 resultStream.resume();123 resultStream.write({124 name: 'fragment',125 attributes: { id: 'f3', async: true, src: 'http://fragment/f3' }126 });127 resultStream.write({128 name: 'fragment',129 attributes: { id: 'f1', src: 'http://fragment/f1' }130 });131 resultStream.write({132 name: 'fragment',133 attributes: {134 id: 'f2',135 primary: true,136 src: 'http://fragment/f2'137 }138 });139 resultStream.end();140 });141 it('write async fragments to a separate stream', done => {142 let data = '';143 options.asyncStream.on('data', chunk => {144 data += chunk.toString();145 });146 options.asyncStream.on('end', () => {147 assert.equal(148 data,149 '<!-- Fragment #2 "f3" START --><NormalFragment 3/><!-- Fragment #2 "f3" END -->'150 );151 done();152 });153 resultStream.write({154 name: 'fragment',155 attributes: {156 id: 'f3',157 async: false,158 src: 'http://fragment/f2'159 }160 });161 resultStream.write({162 name: 'fragment',163 attributes: { id: 'f3', async: true, src: 'http://fragment/f3' }164 });165 resultStream.end();166 options.asyncStream.end();167 });168 it('handles indexes correctly', done => {169 let data = '';170 function assertIndex(index, fragmentText) {171 const r = new RegExp(172 `<!-- Fragment #${index} ".*" START -->.+${fragmentText}.+<!-- Fragment #${index} ".*" END -->`,173 'g'174 );175 const doesMatch = r.test(data);176 let message = `No match for the fragmend(index: ${index}, text: ${fragmentText})`;177 assert.equal(doesMatch, true, message);178 }179 resultStream.on('data', chunk => {180 data += chunk.toString();181 });182 resultStream.on('end', () => {183 assertIndex(1, 'NormalFragment\\s3');184 assertIndex(2, 'NormalFragment\\s1');185 assertIndex(3, 'Primary'); // nested fragments are handled with delay186 assertIndex(4, 'RemoteFragment\\s1');187 assertIndex(5, 'RemoteFragment\\s2');188 done();189 });190 resultStream.on('fragment:found', ({ attributes: { primary } }) => {191 primary && options.asyncStream.end();192 });193 resultStream.write({ placeholder: 'pipe' });194 resultStream.write({195 name: 'fragment',196 attributes: { id: 'f3', async: true, src: 'http://fragment/f3' }197 });198 resultStream.write({199 name: 'fragment',200 attributes: { id: 'f1', src: 'http://fragment/f1' }201 });202 resultStream.write({ name: 'nested-fragments' });203 resultStream.write({204 name: 'fragment',205 attributes: {206 id: 'f2',207 primary: true,208 src: 'http://fragment/primary'209 }210 });211 resultStream.write({ placeholder: 'async' });212 resultStream.end();213 });214 });...
websocket_request.js
Source:websocket_request.js
1var Readable = require('stream').Readable;2var Guid = require('guid')3var querystring = require('querystring')4var through2 = require('through2')5var EventEmitter = require('events').EventEmitter6var wsRequest = function wsRequest(client, args) {7 this.client = client8 this.args = args9 this.method = args.method10 this.path = args.path11 this.params = args.params12 this.body = args.body13 if(!this.body || typeof this.body !== 'object') {14 this.body = {}15 }16 var resultStream = this.init()17 return resultStream18}19wsRequest.prototype.init = function init() {20 var that = this21 this.id = Guid.raw()22 this.request = {23 id: this.id,24 path: this.client.appname + '/' + this.path + '?' + querystring.stringify(this.params),25 method: this.method,26 body: this.body,27 authorization: 'Basic ' + (new Buffer(this.client.credentials).toString('base64'))28 }29 this.resultStream = through2.obj()30 this.resultStream.writable = false31 this.closeHandler = function() {32 that.wsClosed.apply(that)33 }34 this.errorHandler = function(err) {35 that.processError.apply(that, [err])36 }37 this.messageHandler = function(dataObj) {38 that.processMessage.apply(that, [dataObj])39 }40 this.client.ws.on('close', this.closeHandler)41 this.client.ws.on('error', this.errorHandler)42 this.client.ws.on('message', this.messageHandler)43 this.client.ws.send(this.request)44 this.resultStream.on('end', function() {45 that.resultStream.readable = false46 that.stop.apply(that)47 })48 this.resultStream.stop = this.stop.bind(this)49 //this.resultStream.getId = this.getId.bind(this)50 this.resultStream.reconnect = this.reconnect.bind(this)51 return this.resultStream52}53wsRequest.prototype.wsClosed = function wsClosed() {54 this.resultStream.push(null)55}56wsRequest.prototype.processError = function processError(err) {57 this.resultStream.emit('error', err)58}59wsRequest.prototype.processMessage = function processMessage(dataObj) {60 if(!dataObj.id && dataObj.message) {61 this.resultStream.emit('error', dataObj)62 return63 }64 if(dataObj.id === this.id) {65 if(dataObj.message) {66 delete dataObj.id67 this.resultStream.emit('error', dataObj)68 return69 }70 if(dataObj.query_id) {71 this.query_id = dataObj.query_id72 }73 if(dataObj.channel) {74 this.channel = dataObj.channel75 }76 if(dataObj.body && dataObj.body !== "") {77 this.resultStream.push(dataObj.body)78 }79 return80 }81 if(!dataObj.id && dataObj.channel && dataObj.channel === this.channel) {82 this.resultStream.push(dataObj.event)83 return84 }85}86wsRequest.prototype.getId = function getId(callback) {87 if(this.query_id) {88 callback(this.query_id)89 } else {90 this.client.ws.on('message', function gid(data) {91 var dataObj = JSON.parse(data)92 if(dataObj.id === that.id) {93 if(dataObj.query_id) {94 this.client.ws.removeListener('message', gid)95 callback(query_id)96 }97 }98 })99 }100}101wsRequest.prototype.stop = function stop() {102 this.client.ws.removeListener('close', this.closeHandler)103 this.client.ws.removeListener('error', this.errorHandler)104 this.client.ws.removeListener('message', this.messageHandler)105 if(this.resultStream.readable) {106 this.resultStream.push(null)107 }108 var unsubRequest = {}109 for(var key in this.request) {110 unsubRequest[key] = this.request[key]111 }112 unsubRequest.unsubscribe = true113 this.client.ws.send(unsubRequest)114}115wsRequest.prototype.reconnect = function reconnect() {116 this.stop()117 return new wsRequest(this.client, this.args)118}...
Using AI Code Generation
1const fc = require('fast-check');2const resultStream = fc.check(3 fc.property(fc.integer(), fc.integer(), (a, b) => a + b === b + a),4 { verbose: true }5);6resultStream.on('data', (report) => {7 console.log(report);8});
Using AI Code Generation
1const fc = require('fast-check');2const { resultStream } = require('fast-check-monorepo');3const { pipe } = require('fp-ts/lib/pipeable');4const { map } = require('fp-ts/lib/Array');5const { fold } = require('fp-ts/lib/Either');6const { run } = require('fp-ts/lib/IO');7const { forEach } = require('fp-ts/lib/Array');8const { fromEither } = require('fp-ts/lib/TaskEither');9const { Task, task } = require('fp-ts/lib/Task');10const { fromTask } = require('fp-ts/lib/TaskEither');11const { either } = require('fp-ts/lib/Either');12const { log } = console;13const { stringify } = JSON;14const { identity } = require('fp-ts/lib/function');15const { sequenceT } = require('fp-ts/lib/Apply');16const { IO, io } = require('fp-ts/lib/IO');17const { IOEither, ioEither } = require('fp-ts/lib/IOEither');18const { chain, map: mapT, chainFirst } = require('fp-ts/lib/TaskEither');19const { chain: chainIO } = require('fp-ts/lib/IOEither');20const { chain: chainIOEither, map: mapIOEither } = require('fp-ts/lib/IOEither');21const { chain: chainIOEitherT, map: mapIOEitherT } = require('fp-ts/lib/IOEither');22const { chain: chainIOEitherIO, map: mapIOEitherIO } = require('fp-ts/lib/IOEither');23const { chain: chainIOEitherIOEither, map: mapIOEitherIOEither } = require('fp-ts/lib/IOEither');24const { chain: chainIOEitherIOEitherIO, map: mapIOEitherIOEitherIO } = require('fp-ts/lib/IOEither');25const { chain: chainIOEitherIOEitherIOEither, map: mapIOEitherIOEitherIOEither } = require('fp-ts/lib/IOEither');26const { chain: chainIOEitherIOEitherIOEitherIO, map: mapIOEitherIOEitherIOEitherIO } = require('fp-ts/lib/IOEither');27const { chain: chainIOEitherIOEitherIOEitherIOEither, map: mapIOEitherIOEitherIOEitherIOEither } = require('fp-ts/lib/IOEither');28const { chain:
Using AI Code Generation
1const fc = require('fast-check');2const resultStream = fc.check(fc.property(fc.integer(), fc.integer(), (a, b) => {3 return a + b === b + a;4}), { seed: 42, numRuns: 10, verbose: 1 });5resultStream.on('data', (data) => {6 console.log('data: ', data);7});8resultStream.on('end', () => {9 console.log('end');10});11resultStream.on('error', (err) => {12 console.log('error: ', err);13});14resultStream.on('pause', () => {15 console.log('pause');16});17resultStream.on('resume', () => {18 console.log('resume');19});20resultStream.on('close', () => {21 console.log('close');22});23resultStream.on('drain', () => {24 console.log('drain');25});26resultStream.on('warning', (warning) => {27 console.log('warning: ', warning);28});29resultStream.on('unpipe', (src) => {30 console.log('unpipe: ', src);31});32resultStream.on('uncaughtException', (err) => {33 console.log('uncaughtException: ', err);34});35resultStream.on('unhandledRejection', (reason, promise) => {36 console.log('unhandledRejection: ', reason, promise);37});38resultStream.on('multipleResolves', (type, promise, reason) => {39 console.log('multipleResolves: ', type, promise, reason);40});41resultStream.on('newListener', (event, listener) => {42 console.log('newListener: ', event, listener);43});44resultStream.on('removeListener', (event, listener) => {45 console.log('removeListener: ', event, listener);46});47resultStream.on('maxListenersExceeded', (event, listener) => {48 console.log('maxListenersExceeded: ', event, listener);49});50resultStream.on('pipe', (src) => {51 console.log('pipe: ', src);52});53resultStream.on('prependListener', (event, listener) => {54 console.log('prependListener: ', event, listener);55});56resultStream.on('prependOnceListener', (event, listener) => {57 console.log('prependOnceListener: ', event, listener);58});59resultStream.on('response', (response) => {60 console.log('response: ', response);61});
Using AI Code Generation
1const fc = require('fast-check');2const { resultStream } = require('fast-check');3const arb = fc.integer(1, 100);4const stream = resultStream(arb);5stream.on('data', (value) => {6 console.log(value);7});8stream.on('end', () => {9 console.log('done');10});11stream.on('error', (err) => {12 console.log(err);13});
Using AI Code Generation
1const { resultStream } = require('fast-check')2const { map } = require('rxjs/operators')3const stream = resultStream(4 {5 },6 () => {7 const arb = fc.integer()8 return fc.property(arb, (n) => n >= 0)9 }10stream.pipe(map((r) => console.log(r)))
Using AI Code Generation
1const { resultStream } = require('fast-check-monorepo');2const { property } = require('fast-check');3const test = property(4 (a, b) => a + b >= a && a + b >= b5);6const stream = resultStream(test);7stream.on('data', (result) => {8 if (result.failed) {9 console.log('failed with', result.counterexample);10 }11});12stream.on('end', () => {13 console.log('end');14});15const { run } = require('fast-check-monorepo');16const { property } = require('fast-check');17const test = property(18 (a, b) => a + b >= a && a + b >= b19);20run(test).then((result) => {21 console.log(result);22});23const { run } = require('fast-check-monorepo');24const { property } = require('fast-check');25const test = property(26 (a, b) => a + b >= a && a + b >= b27);28run(test).then((result) => {29 console.log(result);30});31const { run } = require('fast-check-monorepo');32const { property } = require('fast-check');33const test = property(34 (a, b) => a + b >= a && a + b >= b35);36run(test).then((result) => {37 console.log(result);38});39const { run } = require('fast-check-monorepo');40const { property } = require('fast-check');41const test = property(42 (a, b) => a + b >= a && a + b >= b43);44run(test).then((result) => {45 console.log(result);46});47const { run } = require('fast-check-monorepo');48const { property } = require('fast-check');49const test = property(50 (a, b) => a + b >= a && a + b
Using AI Code Generation
1var fastCheck = require("fast-check");2var resultStream = fastCheck.resultStream;3var fc = fastCheck.default;4var rs = resultStream(fc.integer(), 10, 10);5rs.on("data", function(data) {6 console.log("data", data);7});8rs.on("end", function() {9 console.log("end");10});
Using AI Code Generation
1const { resultStream } = require('fast-check');2const fs = require('fs');3const stream = fs.createWriteStream('results.txt');4resultStream()5 .forEach((r) => {6 stream.write(r + '7');8 });9stream.end();10const { report } = require('fast-check-report');11const results = fs.readFileSync('results.txt', 'utf8');12report(results, 'report.txt');13const { resultStream } = require('fast-check');14const fs = require('fs');15const stream = fs.createWriteStream('results.txt');16resultStream()17 .forEach((r) => {18 stream.write(r + '19');20 });21stream.end();22const { report } = require('fast-check-report');23const results = fs.readFileSync('results.txt', 'utf8');24report(results, 'report.txt');25const { resultStream } = require('fast-check');26const fs = require('fs');27const stream = fs.createWriteStream('results.txt');28resultStream()29 .forEach((r) => {30 stream.write(r + '31');32 });33stream.end();34const { report } = require('fast-check-report');35const results = fs.readFileSync('results.txt', 'utf8');36report(results, 'report.txt');37const {
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!!