Best JavaScript code snippet using wpt
testharness.js
Source:testharness.js
...579 * that the promise is from the current realm, that can always be used with580 * `await`, etc. We therefore create a new promise in this realm that581 * inherit the value and status from the given promise.582 */583 function bring_promise_to_current_realm(promise) {584 return new Promise(promise.then.bind(promise));585 }586 function promise_rejects_js(test, constructor, promise, description) {587 return bring_promise_to_current_realm(promise)588 .then(test.unreached_func("Should have rejected: " + description))589 .catch(function(e) {590 assert_throws_js_impl(constructor, function() { throw e },591 description, "promise_rejects_js");592 });593 }594 /**595 * Assert that a Promise is rejected with the right DOMException.596 *597 * @param test the test argument passed to promise_test598 * @param {number|string} type. See documentation for assert_throws_dom.599 *600 * For the remaining arguments, there are two ways of calling601 * promise_rejects_dom:602 *603 * 1) If the DOMException is expected to come from the current global, the604 * third argument should be the promise expected to reject, and a fourth,605 * optional, argument is the assertion description.606 *607 * 2) If the DOMException is expected to come from some other global, the608 * third argument should be the DOMException constructor from that global,609 * the fourth argument the promise expected to reject, and the fifth,610 * optional, argument the assertion description.611 */612 function promise_rejects_dom(test, type, promiseOrConstructor, descriptionOrPromise, maybeDescription) {613 let constructor, promise, description;614 if (typeof promiseOrConstructor === "function" &&615 promiseOrConstructor.name === "DOMException") {616 constructor = promiseOrConstructor;617 promise = descriptionOrPromise;618 description = maybeDescription;619 } else {620 constructor = self.DOMException;621 promise = promiseOrConstructor;622 description = descriptionOrPromise;623 assert(maybeDescription === undefined,624 "Too many args pased to no-constructor version of promise_rejects_dom");625 }626 return bring_promise_to_current_realm(promise)627 .then(test.unreached_func("Should have rejected: " + description))628 .catch(function(e) {629 assert_throws_dom_impl(type, function() { throw e }, description,630 "promise_rejects_dom", constructor);631 });632 }633 function promise_rejects_exactly(test, exception, promise, description) {634 return bring_promise_to_current_realm(promise)635 .then(test.unreached_func("Should have rejected: " + description))636 .catch(function(e) {637 assert_throws_exactly_impl(exception, function() { throw e },638 description, "promise_rejects_exactly");639 });640 }641 /**642 * This constructor helper allows DOM events to be handled using Promises,643 * which can make it a lot easier to test a very specific series of events,644 * including ensuring that unexpected events are not fired at any point.645 */646 function EventWatcher(test, watchedNode, eventTypes, timeoutPromise)647 {648 if (typeof eventTypes == 'string') {...
aflprep_testharness.js
Source:aflprep_testharness.js
...520 * In functions we define here, there is an expectation from the caller521 * that the promise is from the current realm, that can always be used with522 * `await`, etc. We therefore create a new promise in this realm that523 * inherit the value and status from the given promise.524 function bring_promise_to_current_realm(promise) {525 return new Promise(promise.then.bind(promise));526 }527 function promise_rejects_js(test, constructor, promise, description) {528 return bring_promise_to_current_realm(promise)529 .then(test.unreached_func("Should have rejected: " + description))530 .catch(function(e) {531 assert_throws_js_impl(constructor, function() { throw e },532 description, "promise_rejects_js");533 });534 }535 * Assert that a Promise is rejected with the right DOMException.536 *537 * @param test the test argument passed to promise_test538 * @param {number|string} type. See documentation for assert_throws_dom.539 *540 * For the remaining arguments, there are two ways of calling541 * promise_rejects_dom:542 *543 * 1) If the DOMException is expected to come from the current global, the544 * third argument should be the promise expected to reject, and a fourth,545 * optional, argument is the assertion description.546 *547 * 2) If the DOMException is expected to come from some other global, the548 * third argument should be the DOMException constructor from that global,549 * the fourth argument the promise expected to reject, and the fifth,550 * optional, argument the assertion description.551 function promise_rejects_dom(test, type, promiseOrConstructor, descriptionOrPromise, maybeDescription) {552 let constructor, promise, description;553 if (typeof promiseOrConstructor === "function" &&554 promiseOrConstructor.name === "DOMException") {555 constructor = promiseOrConstructor;556 promise = descriptionOrPromise;557 description = maybeDescription;558 } else {559 constructor = self.DOMException;560 promise = promiseOrConstructor;561 description = descriptionOrPromise;562 assert(maybeDescription === undefined,563 "Too many args pased to no-constructor version of promise_rejects_dom");564 }565 return bring_promise_to_current_realm(promise)566 .then(test.unreached_func("Should have rejected: " + description))567 .catch(function(e) {568 assert_throws_dom_impl(type, function() { throw e }, description,569 "promise_rejects_dom", constructor);570 });571 }572 function promise_rejects_exactly(test, exception, promise, description) {573 return bring_promise_to_current_realm(promise)574 .then(test.unreached_func("Should have rejected: " + description))575 .catch(function(e) {576 assert_throws_exactly_impl(exception, function() { throw e },577 description, "promise_rejects_exactly");578 });579 }580 * This constructor helper allows DOM events to be handled using Promises,581 * which can make it a lot easier to test a very specific series of events,582 * including ensuring that unexpected events are not fired at any point.583 function EventWatcher(test, watchedNode, eventTypes, timeoutPromise)584 {585 if (typeof eventTypes == 'string') {586 eventTypes = [eventTypes];587 }...
assert.js
Source:assert.js
...3import { assert, AssertionError } from 'chai';4export function assert_unreached(message) {5 assert.fail(message || 'Executed code is supposed to be unreachable');6}7function bring_promise_to_current_realm(promise) {8 return new Promise(promise.then.bind(promise));9}10export function step_func(fn) {11 return _ => fn();12}13export function unreached_func(description) {14 return step_func(function () {15 assert_unreached(description);16 });17};18function merge(a, b) {19 const rv = {};20 let p;21 for (p in a) {22 rv[p] = a[p];23 }24 for (p in b) {25 rv[p] = b[p];26 }27 return rv;28}29function make_message(function_name, description, error, substitutions) {30 for (const p in substitutions) {31 if (Object.prototype.hasOwnProperty.call(substitutions, p)) {32 substitutions[p] = format_value(substitutions[p]);33 }34 }35 const node_form = substitute(['{text}', '${function_name}: ${description}' + error],36 merge(37 {38 function_name: function_name,39 description: (description ? description + ' ' : '')40 },41 substitutions42 )43 );44 return node_form.slice(1).join('');45}46function format_value(value) {47 return node_util.inspect(value);48}49function is_single_node(template) {50 return typeof template[0] === 'string';51}52function substitute(template, substitutions) {53 if (typeof template === 'function') {54 const replacement = template(substitutions);55 if (!replacement) {56 return null;57 }58 return substitute(replacement, substitutions);59 }60 if (is_single_node(template)) {61 return substitute_single(template, substitutions);62 }63 return filter(map(template, function (x) {64 return substitute(x, substitutions);65 }), function (x) { return x !== null; });66}67function filter(array, callable, thisObj) {68 const rv = [];69 for (let i = 0; i < array.length; i++) {70 if (Object.prototype.hasOwnProperty.call(array, i)) {71 const pass = callable.call(thisObj, array[i], i, array);72 if (pass) {73 rv.push(array[i]);74 }75 }76 }77 return rv;78}79function map(array, callable, thisObj) {80 const rv = [];81 rv.length = array.length;82 for (let i = 0; i < array.length; i++) {83 if (Object.prototype.hasOwnProperty.call(array, i)) {84 rv[i] = callable.call(thisObj, array[i], i, array);85 }86 }87 return rv;88}89function extend(array, items) {90 Array.prototype.push.apply(array, items);91}92function substitute_single(template, substitutions) {93 const substitution_re = /\$\{([^ }]*)\}/g;94 function do_substitution(input) {95 const components = input.split(substitution_re);96 const rv = [];97 for (let i = 0; i < components.length; i += 2) {98 rv.push(components[i]);99 if (components[i + 1]) {100 rv.push(String(substitutions[components[i + 1]]));101 }102 }103 return rv;104 }105 function substitute_attrs(attrs, rv) {106 rv[1] = {};107 for (const name in template[1]) {108 if (Object.prototype.hasOwnProperty.call(attrs, name)) {109 const new_name = do_substitution(name).join('');110 const new_value = do_substitution(attrs[name]).join('');111 rv[1][new_name] = new_value;112 }113 }114 }115 function substitute_children(children, rv) {116 for (let i = 0; i < children.length; i++) {117 if (children[i] instanceof Object) {118 const replacement = substitute(children[i], substitutions);119 if (replacement !== null) {120 if (is_single_node(replacement)) {121 rv.push(replacement);122 } else {123 extend(rv, replacement);124 }125 }126 } else {127 extend(rv, do_substitution(String(children[i])));128 }129 }130 return rv;131 }132 const rv = [];133 rv.push(do_substitution(String(template[0])).join(''));134 if (template[0] === '{text}') {135 substitute_children(template.slice(1), rv);136 } else {137 substitute_attrs(template[1], rv);138 substitute_children(template.slice(2), rv);139 }140 return rv;141}142export function assert_throws_js_impl(constructor, func, description, assertion_type) {143 try {144 func.call(this);145 assert.fail(make_message(assertion_type, description, '${func} did not throw', { func: func }));146 } catch (e) {147 if (e instanceof AssertionError) {148 throw e;149 }150 // Basic sanity-checks on the thrown exception.151 assert(typeof e === 'object', make_message(152 assertion_type,153 description,154 '${func} threw ${e} with type ${type}, not an object',155 { func: func, e: e, type: typeof e }156 ));157 assert(e !== null, make_message(assertion_type, description, '${func} threw null, not an object', { func: func }));158 // Basic sanity-check on the passed-in constructor159 assert(typeof constructor === 'function', make_message(160 assertion_type,161 description,162 '${constructor} is not a constructor',163 { constructor: constructor }164 ));165 let obj = constructor;166 while (obj) {167 if (typeof obj === 'function' && obj.name === 'Error') {168 break;169 }170 obj = Object.getPrototypeOf(obj);171 }172 assert(obj != null, make_message(173 assertion_type,174 description,175 '${constructor} is not an Error subtype',176 { constructor: constructor }));177 // And checking that our exception is reasonable178 assert(e.constructor === constructor && e.name === constructor.name, make_message(179 assertion_type, description,180 '${func} threw ${actual} (${actual_name}) expected instance of ${expected} (${expected_name})',181 {182 func: func,183 actual: e,184 actual_name: e.name,185 expected: constructor,186 expected_name: constructor.name187 }188 ));189 }190}191export function assert_throws_js(constructor, func, description) {192 assert_throws_js_impl(constructor, func, description, 'assert_throws_js');193}194export function promise_rejects_js(test, constructor, promise, description) {195 return bring_promise_to_current_realm(promise)196 .then(unreached_func('Should have rejected: ' + description))197 .catch(function (e) {198 assert_throws_js_impl(constructor, function () { throw e; },199 description, 'promise_rejects_js');200 });201}202export function step_timeout(f, t) {203 const outer_this = this;204 const args = Array.prototype.slice.call(arguments, 2);205 return setTimeout(function () {206 f.apply(outer_this, args);207 }, t);208}209export function promise_rejects_exactly(test, exception, promise, description) {210 return bring_promise_to_current_realm(promise)211 .then(unreached_func('Should have rejected: ' + description))212 .catch(function (e) {213 assert_throws_exactly_impl(214 exception,215 function () { throw e; },216 description,217 'promise_rejects_exactly'218 );219 });220}221function assert_throws_exactly_impl(exception, func, description, assertion_type) {222 try {223 func.call(this);224 assert.fail(make_message(assertion_type, description, '${func} did not throw', { func: func }));...
Using AI Code Generation
1function bring_promise_to_current_realm(promise) {2 return promise;3}4function promise_rejects_js(t, constructor, promise, reason) {5 return promise.then(6 v => {7 assert_unreached('Promise unexpectedly fulfilled with: ' + v);8 },9 r => {10 assert_equals(r.constructor, constructor, 'Bad rejection constructor');11 if (reason !== undefined)12 assert_equals(r.message, reason, 'Bad rejection message');13 });14}15function promise_rejects_dom(t, constructor, promise, reason) {16 return promise.then(17 v => {18 assert_unreached('Promise unexpectedly fulfilled with: ' + v);19 },20 r => {21 assert_equals(r.constructor, constructor, 'Bad rejection constructor');22 if (reason !== undefined)23 assert_equals(r.name, reason, 'Bad rejection name');24 });25}26function promise_rejects_exactly(t, expected, promise, reason) {27 return promise.then(28 v => {29 assert_unreached('Promise unexpectedly fulfilled with: ' + v);30 },31 r => {32 assert_equals(r, expected, 'Bad rejection value');33 });34}35function promise_rejects(t, constructor, promise, reason) {36 return promise.then(37 v => {38 assert_unreached('Promise unexpectedly fulfilled with: ' + v);39 },40 r => {41 assert_true(r instanceof constructor, 'Bad rejection constructor');42 if (reason !== undefined)43 assert_equals(r.message, reason, 'Bad rejection message');44 });45}46function promise_rejects(t, constructor, promise, reason) {47 return promise.then(48 v => {49 assert_unreached('Promise unexpectedly fulfilled with: ' + v);50 },51 r => {52 assert_true(r instanceof constructor, 'Bad rejection constructor');53 if (reason !== undefined)54 assert_equals(r.message, reason, 'Bad rejection message');55 });56}57function promise_rejects(t, constructor, promise,
Using AI Code Generation
1var bring_promise_to_current_realm = $262.bringPromiseToCurrentRealm;2var resolve = bring_promise_to_current_realm($262.createRealm().global.Promise.resolve);3var promise = resolve(1);4promise.then(function(value) {5 $DONE('This promise should not be fulfilled.');6}, function(reason) {7 $DONE('This promise should not be rejected.');8});
Using AI Code Generation
1var global = this;2global.bring_promise_to_current_realm = function (promise) {3 global.Promise = promise;4};5global.bring_promise_to_current_realm(Promise);6global.bring_promise_to_current_realm = undefined;7var p = new Promise(function (resolve, reject) {8 resolve(42);9});10p.then(function (value) {11 console.log(value);12}).catch(function (err) {13 console.log(err);14});
Using AI Code Generation
1var wpt = require('./wpt_bring_promise_to_current_realm.js');2wpt.bring_promise_to_current_realm();3var wpt = require('./wpt.js');4 .then(function (result) {5 console.log(result);6 })7 .catch(function (error) {8 console.log(error);9 });
Using AI Code Generation
1var global = this;2var p = new Promise(function(resolve, reject) {3 resolve(42);4});5global.p = wpt.bring_promise_to_current_realm(p);6global.done = false;7global.p.then(function(v) {8 global.done = true;9 global.value = v;10});
Using AI Code Generation
1importScripts('wpt.js');2var promise = new Promise(function(resolve, reject) {3 resolve();4});5bring_promise_to_current_realm(promise);6promise.then(function() {7 postMessage('done');8});9 onmessage = function(e) {10 if (e.data == "done") {11 test(function() {12 assert_true(true);13 }, "promise in worker");14 done();15 }16 };17importScripts('wpt.js');18var promise = new Promise(function(resolve, reject) {19 resolve();20});21bring_promise_to_current_realm(promise);22promise.then(function() {23 postMessage('done');24});25 onmessage = function(e) {26 if (e.data == "done") {27 test(function() {28 assert_true(true);29 }, "promise in worker");30 done();31 }32 };33#### bring_promise_to_current_realm(p)
Using AI Code Generation
1const wpt = require('wpt');2const fs = require('fs');3const util = require('util');4const wpt = new WebPageTest('www.webpagetest.org');5 if (err) {6 console.error(err);7 } else {8 console.log(util.inspect(data));9 }10});11wpt.getTestResults('170612_8R_1b6d2a7a3c0e8f7a3a3b3d3b3c3f3d3e', function(err, data) {12 if (err) {13 console.error(err);14 } else {15 console.log(util.inspect(data));16 }17});18wpt.getLocations(function(err, data) {19 if (err) {20 console.error(err);21 } else {22 console.log(util.inspect(data));23 }24});25wpt.getBrowsers(function(err, data) {26 if (err) {27 console.error(err);28 } else {29 console.log(util.inspect(data));30 }31});32wpt.getTesters(function(err, data) {33 if (err) {34 console.error(err);35 } else {36 console.log(util.inspect(data));37 }38});39wpt.getConnectivity(function(err, data) {40 if (err) {41 console.error(err);42 } else {43 console.log(util.inspect(data));44 }45});46wpt.getVideoCapture(function(err, data) {47 if (err) {48 console.error(err);49 } else {50 console.log(util.inspect(data));51 }52});53wpt.getTestStatus(function(err, data) {54 if (err) {55 console.error(err);56 } else {57 console.log(util.inspect(data));58 }59});60wpt.getTestStatus(function(err, data) {61 if (err) {62 console.error(err);63 } else {64 console.log(util.inspect
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!!