Best JavaScript code snippet using wpt
tee.js
Source:tee.js  
1'use strict';2if (self.importScripts) {3  self.importScripts('../resources/rs-utils.js');4  self.importScripts('/resources/testharness.js');5}6test(() => {7  const rs = new ReadableStream();8  const result = rs.tee();9  assert_true(Array.isArray(result), 'return value should be an array');10  assert_equals(result.length, 2, 'array should have length 2');11  assert_equals(result[0].constructor, ReadableStream, '0th element should be a ReadableStream');12  assert_equals(result[1].constructor, ReadableStream, '1st element should be a ReadableStream');13}, 'ReadableStream teeing: rs.tee() returns an array of two ReadableStreams');14promise_test(t => {15  const rs = new ReadableStream({16    start(c) {17      c.enqueue('a');18      c.enqueue('b');19      c.close();20    }21  });22  const branch = rs.tee();23  const branch1 = branch[0];24  const branch2 = branch[1];25  const reader1 = branch1.getReader();26  const reader2 = branch2.getReader();27  reader2.closed.then(t.unreached_func('branch2 should not be closed'));28  return Promise.all([29    reader1.closed,30    reader1.read().then(r => {31      assert_object_equals(r, { value: 'a', done: false }, 'first chunk from branch1 should be correct');32    }),33    reader1.read().then(r => {34      assert_object_equals(r, { value: 'b', done: false }, 'second chunk from branch1 should be correct');35    }),36    reader1.read().then(r => {37      assert_object_equals(r, { value: undefined, done: true }, 'third read() from branch1 should be done');38    }),39    reader2.read().then(r => {40      assert_object_equals(r, { value: 'a', done: false }, 'first chunk from branch2 should be correct');41    })42  ]);43}, 'ReadableStream teeing: should be able to read one branch to the end without affecting the other');44promise_test(() => {45  const theObject = { the: 'test object' };46  const rs = new ReadableStream({47    start(c) {48      c.enqueue(theObject);49    }50  });51  const branch = rs.tee();52  const branch1 = branch[0];53  const branch2 = branch[1];54  const reader1 = branch1.getReader();55  const reader2 = branch2.getReader();56  return Promise.all([reader1.read(), reader2.read()]).then(values => {57    assert_object_equals(values[0], values[1], 'the values should be equal');58  });59}, 'ReadableStream teeing: values should be equal across each branch');60promise_test(t => {61  const theError = { name: 'boo!' };62  const rs = new ReadableStream({63    start(c) {64      c.enqueue('a');65      c.enqueue('b');66    },67    pull() {68      throw theError;69    }70  });71  const branches = rs.tee();72  const reader1 = branches[0].getReader();73  const reader2 = branches[1].getReader();74  reader1.label = 'reader1';75  reader2.label = 'reader2';76  return Promise.all([77    promise_rejects(t, theError, reader1.closed),78    promise_rejects(t, theError, reader2.closed),79    reader1.read().then(r => {80      assert_object_equals(r, { value: 'a', done: false }, 'should be able to read the first chunk in branch1');81    }),82    reader1.read().then(r => {83      assert_object_equals(r, { value: 'b', done: false }, 'should be able to read the second chunk in branch1');84      return promise_rejects(t, theError, reader2.read());85    })86    .then(() => promise_rejects(t, theError, reader1.read()))87  ]);88}, 'ReadableStream teeing: errors in the source should propagate to both branches');89promise_test(() => {90  const rs = new ReadableStream({91    start(c) {92      c.enqueue('a');93      c.enqueue('b');94      c.close();95    }96  });97  const branches = rs.tee();98  const branch1 = branches[0];99  const branch2 = branches[1];100  branch1.cancel();101  return Promise.all([102    readableStreamToArray(branch1).then(chunks => {103      assert_array_equals(chunks, [], 'branch1 should have no chunks');104    }),105    readableStreamToArray(branch2).then(chunks => {106      assert_array_equals(chunks, ['a', 'b'], 'branch2 should have two chunks');107    })108  ]);109}, 'ReadableStream teeing: canceling branch1 should not impact branch2');110promise_test(() => {111  const rs = new ReadableStream({112    start(c) {113      c.enqueue('a');114      c.enqueue('b');115      c.close();116    }117  });118  const branches = rs.tee();119  const branch1 = branches[0];120  const branch2 = branches[1];121  branch2.cancel();122  return Promise.all([123    readableStreamToArray(branch1).then(chunks => {124      assert_array_equals(chunks, ['a', 'b'], 'branch1 should have two chunks');125    }),126    readableStreamToArray(branch2).then(chunks => {127      assert_array_equals(chunks, [], 'branch2 should have no chunks');128    })129  ]);130}, 'ReadableStream teeing: canceling branch2 should not impact branch2');131promise_test(() => {132  const reason1 = new Error('We\'re wanted men.');133  const reason2 = new Error('I have the death sentence on twelve systems.');134  let resolve;135  const promise = new Promise(r => resolve = r);136  const rs = new ReadableStream({137    cancel(reason) {138      assert_array_equals(reason, [reason1, reason2],139                          'the cancel reason should be an array containing those from the branches');140      resolve();141    }142  });143  const branch = rs.tee();144  const branch1 = branch[0];145  const branch2 = branch[1];146  branch1.cancel(reason1);147  branch2.cancel(reason2);148  return promise;149}, 'ReadableStream teeing: canceling both branches should aggregate the cancel reasons into an array');150promise_test(t => {151  const theError = { name: 'I\'ll be careful.' };152  const rs = new ReadableStream({153    cancel() {154      throw theError;155    }156  });157  const branch = rs.tee();158  const branch1 = branch[0];159  const branch2 = branch[1];160  return Promise.all([161    promise_rejects(t, theError, branch1.cancel()),162    promise_rejects(t, theError, branch2.cancel())163  ]);164}, 'ReadableStream teeing: failing to cancel the original stream should cause cancel() to reject on branches');165test(t => {166  let controller;167  const stream = new ReadableStream({ start(c) { controller = c; } });168  const [branch1, branch2] = stream.tee();169  const promise = controller.error("error");170  branch1.cancel().catch(_=>_);171  branch2.cancel().catch(_=>_);172  return promise;173}, 'ReadableStream teeing: erroring a teed stream should properly handle canceled branches');174promise_test(() => {175  let controller;176  const rs = new ReadableStream({177    start(c) {178      controller = c;179    }180  });181  const branches = rs.tee();182  const reader1 = branches[0].getReader();183  const reader2 = branches[1].getReader();184  const promise = Promise.all([reader1.closed, reader2.closed]);185  controller.close();186  return promise;187}, 'ReadableStream teeing: closing the original should immediately close the branches');188promise_test(t => {189  let controller;190  const rs = new ReadableStream({191    start(c) {192      controller = c;193    }194  });195  const branches = rs.tee();196  const reader1 = branches[0].getReader();197  const reader2 = branches[1].getReader();198  const theError = { name: 'boo!' };199  const promise = Promise.all([200    promise_rejects(t, theError, reader1.closed),201    promise_rejects(t, theError, reader2.closed)202  ]);203  controller.error(theError);204  return promise;205}, 'ReadableStream teeing: erroring the original should immediately error the branches');206test(t => {207  // Copy original global.208  const oldReadableStream = ReadableStream;209  const getReader = ReadableStream.prototype.getReader;210  const origRS = new ReadableStream();211  // Replace the global ReadableStream constructor with one that doesn't work.212  ReadableStream = function() {213    throw new Error('global ReadableStream constructor called');214  };215  t.add_cleanup(() => {216    ReadableStream = oldReadableStream;217  });218  // This will probably fail if the global ReadableStream constructor was used.219  const [rs1, rs2] = origRS.tee();220  // These will definitely fail if the global ReadableStream constructor was used.221  assert_not_equals(getReader.call(rs1), undefined, 'getReader should work on rs1');222  assert_not_equals(getReader.call(rs2), undefined, 'getReader should work on rs2');223}, 'ReadableStreamTee should not use a modified ReadableStream constructor from the global object');...tee.any.js
Source:tee.any.js  
1// META: global=worker,jsshell2// META: script=../resources/rs-utils.js3'use strict';4test(() => {5  const rs = new ReadableStream();6  const result = rs.tee();7  assert_true(Array.isArray(result), 'return value should be an array');8  assert_equals(result.length, 2, 'array should have length 2');9  assert_equals(result[0].constructor, ReadableStream, '0th element should be a ReadableStream');10  assert_equals(result[1].constructor, ReadableStream, '1st element should be a ReadableStream');11}, 'ReadableStream teeing: rs.tee() returns an array of two ReadableStreams');12promise_test(t => {13  const rs = new ReadableStream({14    start(c) {15      c.enqueue('a');16      c.enqueue('b');17      c.close();18    }19  });20  const branch = rs.tee();21  const branch1 = branch[0];22  const branch2 = branch[1];23  const reader1 = branch1.getReader();24  const reader2 = branch2.getReader();25  reader2.closed.then(t.unreached_func('branch2 should not be closed'));26  return Promise.all([27    reader1.closed,28    reader1.read().then(r => {29      assert_object_equals(r, { value: 'a', done: false }, 'first chunk from branch1 should be correct');30    }),31    reader1.read().then(r => {32      assert_object_equals(r, { value: 'b', done: false }, 'second chunk from branch1 should be correct');33    }),34    reader1.read().then(r => {35      assert_object_equals(r, { value: undefined, done: true }, 'third read() from branch1 should be done');36    }),37    reader2.read().then(r => {38      assert_object_equals(r, { value: 'a', done: false }, 'first chunk from branch2 should be correct');39    })40  ]);41}, 'ReadableStream teeing: should be able to read one branch to the end without affecting the other');42promise_test(() => {43  const theObject = { the: 'test object' };44  const rs = new ReadableStream({45    start(c) {46      c.enqueue(theObject);47    }48  });49  const branch = rs.tee();50  const branch1 = branch[0];51  const branch2 = branch[1];52  const reader1 = branch1.getReader();53  const reader2 = branch2.getReader();54  return Promise.all([reader1.read(), reader2.read()]).then(values => {55    assert_object_equals(values[0], values[1], 'the values should be equal');56  });57}, 'ReadableStream teeing: values should be equal across each branch');58promise_test(t => {59  const theError = { name: 'boo!' };60  const rs = new ReadableStream({61    start(c) {62      c.enqueue('a');63      c.enqueue('b');64    },65    pull() {66      throw theError;67    }68  });69  const branches = rs.tee();70  const reader1 = branches[0].getReader();71  const reader2 = branches[1].getReader();72  reader1.label = 'reader1';73  reader2.label = 'reader2';74  return Promise.all([75    promise_rejects(t, theError, reader1.closed),76    promise_rejects(t, theError, reader2.closed),77    reader1.read().then(r => {78      assert_object_equals(r, { value: 'a', done: false }, 'should be able to read the first chunk in branch1');79    }),80    reader1.read().then(r => {81      assert_object_equals(r, { value: 'b', done: false }, 'should be able to read the second chunk in branch1');82      return promise_rejects(t, theError, reader2.read());83    })84    .then(() => promise_rejects(t, theError, reader1.read()))85  ]);86}, 'ReadableStream teeing: errors in the source should propagate to both branches');87promise_test(() => {88  const rs = new ReadableStream({89    start(c) {90      c.enqueue('a');91      c.enqueue('b');92      c.close();93    }94  });95  const branches = rs.tee();96  const branch1 = branches[0];97  const branch2 = branches[1];98  branch1.cancel();99  return Promise.all([100    readableStreamToArray(branch1).then(chunks => {101      assert_array_equals(chunks, [], 'branch1 should have no chunks');102    }),103    readableStreamToArray(branch2).then(chunks => {104      assert_array_equals(chunks, ['a', 'b'], 'branch2 should have two chunks');105    })106  ]);107}, 'ReadableStream teeing: canceling branch1 should not impact branch2');108promise_test(() => {109  const rs = new ReadableStream({110    start(c) {111      c.enqueue('a');112      c.enqueue('b');113      c.close();114    }115  });116  const branches = rs.tee();117  const branch1 = branches[0];118  const branch2 = branches[1];119  branch2.cancel();120  return Promise.all([121    readableStreamToArray(branch1).then(chunks => {122      assert_array_equals(chunks, ['a', 'b'], 'branch1 should have two chunks');123    }),124    readableStreamToArray(branch2).then(chunks => {125      assert_array_equals(chunks, [], 'branch2 should have no chunks');126    })127  ]);128}, 'ReadableStream teeing: canceling branch2 should not impact branch2');129promise_test(() => {130  const reason1 = new Error('We\'re wanted men.');131  const reason2 = new Error('I have the death sentence on twelve systems.');132  let resolve;133  const promise = new Promise(r => resolve = r);134  const rs = new ReadableStream({135    cancel(reason) {136      assert_array_equals(reason, [reason1, reason2],137                          'the cancel reason should be an array containing those from the branches');138      resolve();139    }140  });141  const branch = rs.tee();142  const branch1 = branch[0];143  const branch2 = branch[1];144  branch1.cancel(reason1);145  branch2.cancel(reason2);146  return promise;147}, 'ReadableStream teeing: canceling both branches should aggregate the cancel reasons into an array');148promise_test(t => {149  const theError = { name: 'I\'ll be careful.' };150  const rs = new ReadableStream({151    cancel() {152      throw theError;153    }154  });155  const branch = rs.tee();156  const branch1 = branch[0];157  const branch2 = branch[1];158  return Promise.all([159    promise_rejects(t, theError, branch1.cancel()),160    promise_rejects(t, theError, branch2.cancel())161  ]);162}, 'ReadableStream teeing: failing to cancel the original stream should cause cancel() to reject on branches');163test(t => {164  let controller;165  const stream = new ReadableStream({ start(c) { controller = c; } });166  const [branch1, branch2] = stream.tee();167  const promise = controller.error("error");168  branch1.cancel().catch(_=>_);169  branch2.cancel().catch(_=>_);170  return promise;171}, 'ReadableStream teeing: erroring a teed stream should properly handle canceled branches');172promise_test(() => {173  let controller;174  const rs = new ReadableStream({175    start(c) {176      controller = c;177    }178  });179  const branches = rs.tee();180  const reader1 = branches[0].getReader();181  const reader2 = branches[1].getReader();182  const promise = Promise.all([reader1.closed, reader2.closed]);183  controller.close();184  return promise;185}, 'ReadableStream teeing: closing the original should immediately close the branches');186promise_test(t => {187  let controller;188  const rs = new ReadableStream({189    start(c) {190      controller = c;191    }192  });193  const branches = rs.tee();194  const reader1 = branches[0].getReader();195  const reader2 = branches[1].getReader();196  const theError = { name: 'boo!' };197  const promise = Promise.all([198    promise_rejects(t, theError, reader1.closed),199    promise_rejects(t, theError, reader2.closed)200  ]);201  controller.error(theError);202  return promise;203}, 'ReadableStream teeing: erroring the original should immediately error the branches');204test(t => {205  // Copy original global.206  const oldReadableStream = ReadableStream;207  const getReader = ReadableStream.prototype.getReader;208  const origRS = new ReadableStream();209  // Replace the global ReadableStream constructor with one that doesn't work.210  ReadableStream = function() {211    throw new Error('global ReadableStream constructor called');212  };213  t.add_cleanup(() => {214    ReadableStream = oldReadableStream;215  });216  // This will probably fail if the global ReadableStream constructor was used.217  const [rs1, rs2] = origRS.tee();218  // These will definitely fail if the global ReadableStream constructor was used.219  assert_not_equals(getReader.call(rs1), undefined, 'getReader should work on rs1');220  assert_not_equals(getReader.call(rs2), undefined, 'getReader should work on rs2');...Using AI Code Generation
1var wptools = require('wptools');2var origRS = wptools.origRS;3var options = {4};5wptools.origRS = function (page, options, callback) {6    var self = this;7    if (!callback && typeof options === 'function') {8        callback = options;9        options = {};10    }11    options = self.defaults(options);12    options.action = 'query';13    options.prop = 'revisions';14    options.rvprop = 'content';15    options.rvslots = 'main';16    options.rvlimit = 1;17    options.titles = page;18    options.redirects = '';19    self.api(options, function (err, resp) {20        if (err) {21            return callback(err);22        }23        var pageid = Object.keys(resp.query.pages)[0];24        var page = resp.query.pages[pageid];25        if (page.missing) {26            return callback(new Error('Page not found'));27        }28        if (page.redirect) {29            return self.origRS(page.redirects[0].to, options, callback);30        }31        var revision = page.revisions[0];32        var content = revision.slots.main['*'];33        return callback(null, content);34    });35};36var wptools = require('wptools');37var origRS = wptools.origRS;38var options = {39};40wptools.origRS = function (page, options, callback) {41    var self = this;42    if (!callback && typeof options === 'function') {43        callback = options;44        options = {};45    }46    options = self.defaults(options);47    options.action = 'query';48    options.prop = 'revisions';49    options.rvprop = 'content';50    options.rvslots = 'main';51    options.rvlimit = 1;52    options.titles = page;53    options.redirects = '';54    self.api(options, function (err, resp) {55        if (err) {56            return callback(err);57        }58        var pageid = Object.keys(resp.query.pages)[0];59        var page = resp.query.pages[pageid];60        if (page.missingUsing AI Code Generation
1var page = require('webpage').create();2var wpt = require('./wpt.js');3    console.log(status);4    phantom.exit();5});6var page = require('webpage').create();7var wpt = require('./wpt.js');8    console.log(result);9    phantom.exit();10});11var page = require('webpage').create();12var wpt = require('./wpt.js');13    console.log(result);14    phantom.exit();15});Using AI Code Generation
1var wpt = require('webpagetest-origRS');2var fs = require('fs');3var util = require('util');4var wptKey = 'A.7e0a52c1e7a1a2b0d2c2b9a9c2a2d2e2';5var wpt = new WebPageTest(wptServer, wptKey);6var testId = '';7var testResults = '';8var testResultsJson = '';9wpt.runTest(url, function(err, data) {10  if (err) return console.error(err);11  console.log('Test started: ' + data.data.testId);12  console.log('Poll the test results at: ' + data.data.jsonUrl);13  console.log('View the test at: ' + data.data.userUrl);14  testId = data.data.testId;15  console.log('testId = ' + testId);16});17wpt.getTestResults(testId, function(err, data) {18  if (err) return console.error(err);19  testResults = data;20  console.log('Test results: ' + util.inspect(data));21  console.log('testResults = ' + testResults);22  console.log('testResults.data.median.firstView.SpeedIndex = ' + testResults.data.median.firstView.SpeedIndex);23  testResultsJson = JSON.stringify(data);24  console.log('testResultsJson = ' + testResultsJson);25  fs.writeFile("testResults.json", testResultsJson, function(err) {26    if(err) {27        return console.log(err);28    }29    console.log("The file was saved!");30  }); 31});Using AI Code Generation
1var wpt = require('webpagetest');2var fs = require('fs');3var api = new wpt('www.webpagetest.org');4var testResult;5var testId;6var testResults;7var testResultsStr;8var testResultsObj;9var testResultsObjStr;10var testResultsObjStrPretty;11var testResultsObjStrPretty2;12var testResultsObjStrPretty3;13var testResultsObjStrPretty4;14var testResultsObjStrPretty5;15var testResultsObjStrPretty6;16var testResultsObjStrPretty7;17var testResultsObjStrPretty8;18var testResultsObjStrPretty9;19var testResultsObjStrPretty10;20var testResultsObjStrPretty11;21var testResultsObjStrPretty12;22var testResultsObjStrPretty13;23var testResultsObjStrPretty14;24var testResultsObjStrPretty15;25var testResultsObjStrPretty16;26var testResultsObjStrPretty17;27var testResultsObjStrPretty18;28var testResultsObjStrPretty19;29var testResultsObjStrPretty20;30var testResultsObjStrPretty21;31var testResultsObjStrPretty22;32var testResultsObjStrPretty23;33var testResultsObjStrPretty24;34var testResultsObjStrPretty25;35var testResultsObjStrPretty26;36var testResultsObjStrPretty27;37var testResultsObjStrPretty28;38var testResultsObjStrPretty29;39var testResultsObjStrPretty30;40var testResultsObjStrPretty31;41var testResultsObjStrPretty32;42var testResultsObjStrPretty33;43var testResultsObjStrPretty34;44var testResultsObjStrPretty35;45var testResultsObjStrPretty36;46var testResultsObjStrPretty37;47var testResultsObjStrPretty38;48var testResultsObjStrPretty39;49var testResultsObjStrPretty40;50var testResultsObjStrPretty41;51var testResultsObjStrPretty42;52var testResultsObjStrPretty43;53var testResultsObjStrPretty44;54var testResultsObjStrPretty45;55var testResultsObjStrPretty46;56var testResultsObjStrPretty47;57var testResultsObjStrPretty48;58var testResultsObjStrPretty49;59var testResultsObjStrPretty50;60var testResultsObjStrPretty51;61var testResultsObjStrPretty52;62var testResultsObjStrPretty53;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!!
