How to use newPromiseCapability method in wpt

Best JavaScript code snippet using wpt

es.promise.js

Source:es.promise.js Github

copy

Full Screen

1import { DESCRIPTORS, GLOBAL, PROTO, STRICT } from '../helpers/constants';2import { createIterable } from '../helpers/helpers';3import { Promise, Symbol } from 'core-js-pure';4import getIteratorMethod from 'core-js-pure/features/get-iterator-method';5import { setPrototypeOf, create } from 'core-js-pure/features/object';6import bind from 'core-js-pure/features/function/bind';7QUnit.test('Promise', assert => {8 assert.isFunction(Promise);9 assert.throws(() => {10 Promise();11 }, 'throws w/o `new`');12 new Promise(function (resolve, reject) {13 assert.isFunction(resolve, 'resolver is function');14 assert.isFunction(reject, 'rejector is function');15 if (STRICT) assert.same(this, undefined, 'correct executor context');16 });17});18if (DESCRIPTORS) QUnit.test('Promise operations order', assert => {19 let resolve, resolve2;20 assert.expect(1);21 const EXPECTED_ORDER = 'DEHAFGBC';22 const async = assert.async();23 let result = '';24 const promise1 = new Promise(r => {25 resolve = r;26 });27 resolve({28 then() {29 result += 'A';30 throw Error();31 },32 });33 promise1.catch(() => {34 result += 'B';35 });36 promise1.catch(() => {37 result += 'C';38 assert.same(result, EXPECTED_ORDER);39 async();40 });41 const promise2 = new Promise(r => {42 resolve2 = r;43 });44 resolve2(Object.defineProperty({}, 'then', {45 get() {46 result += 'D';47 throw Error();48 },49 }));50 result += 'E';51 promise2.catch(() => {52 result += 'F';53 });54 promise2.catch(() => {55 result += 'G';56 });57 result += 'H';58 setTimeout(() => {59 if (!~result.indexOf('C')) {60 assert.same(result, EXPECTED_ORDER);61 async();62 }63 }, 1e3);64});65QUnit.test('Promise#then', assert => {66 assert.isFunction(Promise.prototype.then);67 assert.nonEnumerable(Promise.prototype, 'then');68 let promise = new Promise(resolve => {69 resolve(42);70 });71 let FakePromise1 = promise.constructor = function (executor) {72 executor(() => { /* empty */ }, () => { /* empty */ });73 };74 const FakePromise2 = FakePromise1[Symbol.species] = function (executor) {75 executor(() => { /* empty */ }, () => { /* empty */ });76 };77 assert.ok(promise.then(() => { /* empty */ }) instanceof FakePromise2, 'subclassing, @@species pattern');78 promise = new Promise(resolve => {79 resolve(42);80 });81 promise.constructor = FakePromise1 = function (executor) {82 executor(() => { /* empty */ }, () => { /* empty */ });83 };84 assert.ok(promise.then(() => { /* empty */ }) instanceof Promise, 'subclassing, incorrect `this` pattern');85 promise = new Promise(resolve => {86 resolve(42);87 });88 promise.constructor = FakePromise1 = function (executor) {89 executor(() => { /* empty */ }, () => { /* empty */ });90 };91 FakePromise1[Symbol.species] = function () { /* empty */ };92 assert.throws(() => {93 promise.then(() => { /* empty */ });94 }, 'NewPromiseCapability validations, #1');95 FakePromise1[Symbol.species] = function (executor) {96 executor(null, () => { /* empty */ });97 };98 assert.throws(() => {99 promise.then(() => { /* empty */ });100 }, 'NewPromiseCapability validations, #2');101 FakePromise1[Symbol.species] = function (executor) {102 executor(() => { /* empty */ }, null);103 };104 assert.throws(() => {105 promise.then(() => { /* empty */ });106 }, 'NewPromiseCapability validations, #3');107});108QUnit.test('Promise#catch', assert => {109 assert.isFunction(Promise.prototype.catch);110 assert.nonEnumerable(Promise.prototype, 'catch');111 let promise = new Promise(resolve => {112 resolve(42);113 });114 let FakePromise1 = promise.constructor = function (executor) {115 executor(() => { /* empty */ }, () => { /* empty */ });116 };117 const FakePromise2 = FakePromise1[Symbol.species] = function (executor) {118 executor(() => { /* empty */ }, () => { /* empty */ });119 };120 assert.ok(promise.catch(() => { /* empty */ }) instanceof FakePromise2, 'subclassing, @@species pattern');121 promise = new Promise(resolve => {122 resolve(42);123 });124 promise.constructor = FakePromise1 = function (executor) {125 executor(() => { /* empty */ }, () => { /* empty */ });126 };127 assert.ok(promise.catch(() => { /* empty */ }) instanceof Promise, 'subclassing, incorrect `this` pattern');128 promise = new Promise(resolve => {129 resolve(42);130 });131 promise.constructor = FakePromise1 = function (executor) {132 executor(() => { /* empty */ }, () => { /* empty */ });133 };134 FakePromise1[Symbol.species] = function () { /* empty */ };135 assert.throws(() => {136 promise.catch(() => { /* empty */ });137 }, 'NewPromiseCapability validations, #1');138 FakePromise1[Symbol.species] = function (executor) {139 executor(null, () => { /* empty */ });140 };141 assert.throws(() => {142 promise.catch(() => { /* empty */ });143 }, 'NewPromiseCapability validations, #2');144 FakePromise1[Symbol.species] = function (executor) {145 executor(() => { /* empty */ }, null);146 };147 assert.throws(() => {148 promise.catch(() => { /* empty */ });149 }, 'NewPromiseCapability validations, #3');150 assert.same(Promise.prototype.catch.call({151 then(x, y) {152 return y;153 },154 }, 42), 42, 'calling `.then`');155});156QUnit.test('Promise#@@toStringTag', assert => {157 assert.ok(Promise.prototype[Symbol.toStringTag] === 'Promise', 'Promise::@@toStringTag is `Promise`');158 assert.strictEqual(String(new Promise(() => { /* empty */ })), '[object Promise]', 'correct stringification');159});160QUnit.test('Promise.all', assert => {161 const { all, resolve } = Promise;162 assert.isFunction(all);163 assert.arity(all, 1);164 const iterable = createIterable([1, 2, 3]);165 Promise.all(iterable).catch(() => { /* empty */ });166 assert.ok(iterable.received, 'works with iterables: iterator received');167 assert.ok(iterable.called, 'works with iterables: next called');168 const array = [];169 let done = false;170 array['@@iterator'] = undefined;171 array[Symbol.iterator] = function () {172 done = true;173 return getIteratorMethod([]).call(this);174 };175 Promise.all(array);176 assert.ok(done);177 assert.throws(() => {178 all.call(null, []).catch(() => { /* empty */ });179 }, TypeError, 'throws without context');180 done = false;181 try {182 Promise.resolve = function () {183 throw new Error();184 };185 Promise.all(createIterable([1, 2, 3], {186 return() {187 done = true;188 },189 })).catch(() => { /* empty */ });190 } catch (error) { /* empty */ }191 Promise.resolve = resolve;192 assert.ok(done, 'iteration closing');193 let FakePromise1 = function (executor) {194 executor(() => { /* empty */ }, () => { /* empty */ });195 };196 let FakePromise2 = FakePromise1[Symbol.species] = function (executor) {197 executor(() => { /* empty */ }, () => { /* empty */ });198 };199 FakePromise1.resolve = FakePromise2.resolve = bind(resolve, Promise);200 assert.ok(all.call(FakePromise1, [1, 2, 3]) instanceof FakePromise1, 'subclassing, `this` pattern');201 FakePromise1 = function () { /* empty */ };202 FakePromise2 = function (executor) {203 executor(null, () => { /* empty */ });204 };205 const FakePromise3 = function (executor) {206 executor(() => { /* empty */ }, null);207 };208 FakePromise1.resolve = FakePromise2.resolve = FakePromise3.resolve = bind(resolve, Promise);209 assert.throws(() => {210 all.call(FakePromise1, [1, 2, 3]);211 }, 'NewPromiseCapability validations, #1');212 assert.throws(() => {213 all.call(FakePromise2, [1, 2, 3]);214 }, 'NewPromiseCapability validations, #2');215 assert.throws(() => {216 all.call(FakePromise3, [1, 2, 3]);217 }, 'NewPromiseCapability validations, #3');218});219QUnit.test('Promise.race', assert => {220 const { race, resolve } = Promise;221 assert.isFunction(race);222 assert.arity(race, 1);223 const iterable = createIterable([1, 2, 3]);224 Promise.race(iterable).catch(() => { /* empty */ });225 assert.ok(iterable.received, 'works with iterables: iterator received');226 assert.ok(iterable.called, 'works with iterables: next called');227 const array = [];228 let done = false;229 array['@@iterator'] = undefined;230 array[Symbol.iterator] = function () {231 done = true;232 return getIteratorMethod([]).call(this);233 };234 Promise.race(array);235 assert.ok(done);236 assert.throws(() => {237 race.call(null, []).catch(() => { /* empty */ });238 }, TypeError, 'throws without context');239 done = false;240 try {241 Promise.resolve = function () {242 throw new Error();243 };244 Promise.race(createIterable([1, 2, 3], {245 return() {246 done = true;247 },248 })).catch(() => { /* empty */ });249 } catch (error) { /* empty */ }250 Promise.resolve = resolve;251 assert.ok(done, 'iteration closing');252 let FakePromise1 = function (executor) {253 executor(() => { /* empty */ }, () => { /* empty */ });254 };255 let FakePromise2 = FakePromise1[Symbol.species] = function (executor) {256 executor(() => { /* empty */ }, () => { /* empty */ });257 };258 FakePromise1.resolve = FakePromise2.resolve = bind(resolve, Promise);259 assert.ok(race.call(FakePromise1, [1, 2, 3]) instanceof FakePromise1, 'subclassing, `this` pattern');260 FakePromise1 = function () { /* empty */ };261 FakePromise2 = function (executor) {262 executor(null, () => { /* empty */ });263 };264 const FakePromise3 = function (executor) {265 executor(() => { /* empty */ }, null);266 };267 FakePromise1.resolve = FakePromise2.resolve = FakePromise3.resolve = bind(resolve, Promise);268 assert.throws(() => {269 race.call(FakePromise1, [1, 2, 3]);270 }, 'NewPromiseCapability validations, #1');271 assert.throws(() => {272 race.call(FakePromise2, [1, 2, 3]);273 }, 'NewPromiseCapability validations, #2');274 assert.throws(() => {275 race.call(FakePromise3, [1, 2, 3]);276 }, 'NewPromiseCapability validations, #3');277});278QUnit.test('Promise.resolve', assert => {279 const { resolve } = Promise;280 assert.isFunction(resolve);281 assert.throws(() => {282 resolve.call(null, 1).catch(() => { /* empty */ });283 }, TypeError, 'throws without context');284 function FakePromise1(executor) {285 executor(() => { /* empty */ }, () => { /* empty */ });286 }287 FakePromise1[Symbol.species] = function (executor) {288 executor(() => { /* empty */ }, () => { /* empty */ });289 };290 assert.ok(resolve.call(FakePromise1, 42) instanceof FakePromise1, 'subclassing, `this` pattern');291 assert.throws(() => {292 resolve.call(() => { /* empty */ }, 42);293 }, 'NewPromiseCapability validations, #1');294 assert.throws(() => {295 resolve.call(executor => {296 executor(null, () => { /* empty */ });297 }, 42);298 }, 'NewPromiseCapability validations, #2');299 assert.throws(() => {300 resolve.call(executor => {301 executor(() => { /* empty */ }, null);302 }, 42);303 }, 'NewPromiseCapability validations, #3');304});305QUnit.test('Promise.reject', assert => {306 const { reject } = Promise;307 assert.isFunction(reject);308 assert.throws(() => {309 reject.call(null, 1).catch(() => { /* empty */ });310 }, TypeError, 'throws without context');311 function FakePromise1(executor) {312 executor(() => { /* empty */ }, () => { /* empty */ });313 }314 FakePromise1[Symbol.species] = function (executor) {315 executor(() => { /* empty */ }, () => { /* empty */ });316 };317 assert.ok(reject.call(FakePromise1, 42) instanceof FakePromise1, 'subclassing, `this` pattern');318 assert.throws(() => {319 reject.call(() => { /* empty */ }, 42);320 }, 'NewPromiseCapability validations, #1');321 assert.throws(() => {322 reject.call(executor => {323 executor(null, () => { /* empty */ });324 }, 42);325 }, 'NewPromiseCapability validations, #2');326 assert.throws(() => {327 reject.call(executor => {328 executor(() => { /* empty */ }, null);329 }, 42);330 }, 'NewPromiseCapability validations, #3');331});332if (PROTO) QUnit.test('Promise subclassing', assert => {333 function SubPromise(executor) {334 const self = new Promise(executor);335 setPrototypeOf(self, SubPromise.prototype);336 self.mine = 'subclass';337 return self;338 }339 setPrototypeOf(SubPromise, Promise);340 SubPromise.prototype = create(Promise.prototype);341 SubPromise.prototype.constructor = SubPromise;342 let promise1 = SubPromise.resolve(5);343 assert.strictEqual(promise1.mine, 'subclass');344 promise1 = promise1.then(it => {345 assert.strictEqual(it, 5);346 });347 assert.strictEqual(promise1.mine, 'subclass');348 let promise2 = new SubPromise(resolve => {349 resolve(6);350 });351 assert.strictEqual(promise2.mine, 'subclass');352 promise2 = promise2.then(it => {353 assert.strictEqual(it, 6);354 });355 assert.strictEqual(promise2.mine, 'subclass');356 const promise3 = SubPromise.all([promise1, promise2]);357 assert.strictEqual(promise3.mine, 'subclass');358 assert.ok(promise3 instanceof Promise);359 assert.ok(promise3 instanceof SubPromise);360 promise3.then(assert.async(), error => {361 assert.ok(false, error);362 });363});364// qunit@2.5 strange bug365QUnit.skip('Unhandled rejection tracking', assert => {366 let done = false;367 const resume = assert.async();368 if (GLOBAL.process) {369 assert.expect(3);370 function onunhandledrejection(reason, promise) {371 process.removeListener('unhandledRejection', onunhandledrejection);372 assert.same(promise, $promise, 'unhandledRejection, promise');373 assert.same(reason, 42, 'unhandledRejection, reason');374 $promise.catch(() => {375 // empty376 });377 }378 function onrejectionhandled(promise) {379 process.removeListener('rejectionHandled', onrejectionhandled);380 assert.same(promise, $promise, 'rejectionHandled, promise');381 done || resume();382 done = true;383 }384 process.on('unhandledRejection', onunhandledrejection);385 process.on('rejectionHandled', onrejectionhandled);386 } else {387 if (GLOBAL.addEventListener) {388 assert.expect(8);389 function onunhandledrejection(it) {390 assert.same(it.promise, $promise, 'addEventListener(unhandledrejection), promise');391 assert.same(it.reason, 42, 'addEventListener(unhandledrejection), reason');392 GLOBAL.removeEventListener('unhandledrejection', onunhandledrejection);393 }394 GLOBAL.addEventListener('rejectionhandled', onunhandledrejection);395 function onrejectionhandled(it) {396 assert.same(it.promise, $promise, 'addEventListener(rejectionhandled), promise');397 assert.same(it.reason, 42, 'addEventListener(rejectionhandled), reason');398 GLOBAL.removeEventListener('rejectionhandled', onrejectionhandled);399 }400 GLOBAL.addEventListener('rejectionhandled', onrejectionhandled);401 } else assert.expect(4);402 GLOBAL.onunhandledrejection = function (it) {403 assert.same(it.promise, $promise, 'onunhandledrejection, promise');404 assert.same(it.reason, 42, 'onunhandledrejection, reason');405 setTimeout(() => {406 $promise.catch(() => {407 // empty408 });409 }, 1);410 GLOBAL.onunhandledrejection = null;411 };412 GLOBAL.onrejectionhandled = function (it) {413 assert.same(it.promise, $promise, 'onrejectionhandled, promise');414 assert.same(it.reason, 42, 'onrejectionhandled, reason');415 GLOBAL.onrejectionhandled = null;416 done || resume();417 done = true;418 };419 }420 Promise.reject(43).catch(() => {421 // empty422 });423 const $promise = Promise.reject(42);424 setTimeout(() => {425 done || resume();426 done = true;427 }, 3e3);...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var newPromiseCapability = function (C) {2 var resolve, reject;3 var promise = new C(function ($$resolve, $$reject) {4 if (resolve !== undefined || reject !== undefined)5 throw TypeError('Bad Promise constructor');6 resolve = $$resolve;7 reject = $$reject;8 });9 if (resolve === undefined || reject === undefined)10 throw TypeError('Bad Promise constructor');11 return { promise: promise, resolve: resolve, reject: reject };12};13var p = newPromiseCapability(Promise);14p.resolve(42);15p.promise.then(function (v) {16 $DONE('This promise should not be fulfilled: ' + v);17}, function (e) {18 $DONE('This promise should not be rejected: ' + e);19});

Full Screen

Using AI Code Generation

copy

Full Screen

1var newPromiseCapability = function (C) {2 if (typeof C !== 'function') {3 throw new TypeError('Bad promise constructor');4 }5 var capability = {6 };7 var executor = function (resolve, reject) {8 if (capability.resolve !== undefined || capability.reject !== undefined) {9 throw new TypeError('Bad Promise implementation!');10 }11 capability.resolve = resolve;12 capability.reject = reject;13 };14 try {15 var promise = new C(executor);16 if (Object(promise) !== promise) {17 throw new TypeError('Bad promise constructor');18 }19 capability.promise = promise;20 } catch (e) {21 capability.reject(e);22 }23 return capability;24};25var p = newPromiseCapability(Promise);26p.resolve(42);27p.promise.then(function (v) {28});

Full Screen

Using AI Code Generation

copy

Full Screen

1var newPromiseCapability = require('wpt').newPromiseCapability;2var promiseCapability = newPromiseCapability(Promise);3promiseCapability.promise.then(function(value) {4 console.log(value);5});6promiseCapability.resolve('foo');

Full Screen

Using AI Code Generation

copy

Full Screen

1var test = require('wpt').test;2var newPromiseCapability = require('wpt').newPromiseCapability;3var p = newPromiseCapability(test.Promise);4p.promise.then(function (x) {5 test.step(function () {6 test.assert_equals(x, 42, 'Promise fulfilled with 42');7 });8 test.done();9});10p.resolve(42);

Full Screen

Using AI Code Generation

copy

Full Screen

1const wpt = require('webpagetest');2const wptTest = wpt('A.345f4a6a7f0f3d2b0a1d3e6a0f6c2f6e');3}, (err, data) => {4 if (err) {5 console.log(err);6 }7 console.log(data);8});9const wpt = require('webpagetest');10const wptTest = wpt('A.345f4a6a7f0f3d2b0a1d3e6a0f6c2f6e');11}, (err, data) => {12 if (err) {13 console.log(err);14 }15 console.log(data);16});17const wpt = require('webpagetest');18const wptTest = wpt('A.345f4a6a7f0f3d2b0a1d3e6a0f6c2f6e');19}, (err, data) => {20 if (err) {21 console.log(err);22 }23 console.log(data);24});

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful