Best JavaScript code snippet using wpt
async-iterator.any.js
Source:async-iterator.any.js
...3// META: script=../resources/test-utils.js4// META: script=../resources/recording-streams.js5'use strict';6const error1 = new Error('error1');7function assert_iter_result(iterResult, value, done, message) {8 const prefix = message === undefined ? '' : `${message} `;9 assert_equals(typeof iterResult, 'object', `${prefix}type is object`);10 assert_equals(Object.getPrototypeOf(iterResult), Object.prototype, `${prefix}[[Prototype]]`);11 assert_array_equals(Object.getOwnPropertyNames(iterResult).sort(), ['done', 'value'], `${prefix}property names`);12 assert_equals(iterResult.value, value, `${prefix}value`);13 assert_equals(iterResult.done, done, `${prefix}done`);14}15test(() => {16 assert_equals(ReadableStream.prototype[Symbol.asyncIterator], ReadableStream.prototype.values);17}, '@@asyncIterator() method is === to values() method');18test(() => {19 const s = new ReadableStream();20 const it = s.values();21 const proto = Object.getPrototypeOf(it);22 const AsyncIteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf(async function* () {}).prototype);23 assert_equals(Object.getPrototypeOf(proto), AsyncIteratorPrototype, 'prototype should extend AsyncIteratorPrototype');24 const methods = ['next', 'return'].sort();25 assert_array_equals(Object.getOwnPropertyNames(proto).sort(), methods, 'should have all the correct methods');26 for (const m of methods) {27 const propDesc = Object.getOwnPropertyDescriptor(proto, m);28 assert_true(propDesc.enumerable, 'method should be enumerable');29 assert_true(propDesc.configurable, 'method should be configurable');30 assert_true(propDesc.writable, 'method should be writable');31 assert_equals(typeof it[m], 'function', 'method should be a function');32 assert_equals(it[m].name, m, 'method should have the correct name');33 }34 assert_equals(it.next.length, 0, 'next should have no parameters');35 assert_equals(it.return.length, 1, 'return should have 1 parameter');36 assert_equals(typeof it.throw, 'undefined', 'throw should not exist');37}, 'Async iterator instances should have the correct list of properties');38promise_test(async () => {39 const s = new ReadableStream({40 start(c) {41 c.enqueue(1);42 c.enqueue(2);43 c.enqueue(3);44 c.close();45 }46 });47 const chunks = [];48 for await (const chunk of s) {49 chunks.push(chunk);50 }51 assert_array_equals(chunks, [1, 2, 3]);52}, 'Async-iterating a push source');53promise_test(async () => {54 let i = 1;55 const s = new ReadableStream({56 pull(c) {57 c.enqueue(i);58 if (i >= 3) {59 c.close();60 }61 i += 1;62 }63 });64 const chunks = [];65 for await (const chunk of s) {66 chunks.push(chunk);67 }68 assert_array_equals(chunks, [1, 2, 3]);69}, 'Async-iterating a pull source');70promise_test(async () => {71 const s = new ReadableStream({72 start(c) {73 c.enqueue(undefined);74 c.enqueue(undefined);75 c.enqueue(undefined);76 c.close();77 }78 });79 const chunks = [];80 for await (const chunk of s) {81 chunks.push(chunk);82 }83 assert_array_equals(chunks, [undefined, undefined, undefined]);84}, 'Async-iterating a push source with undefined values');85promise_test(async () => {86 let i = 1;87 const s = new ReadableStream({88 pull(c) {89 c.enqueue(undefined);90 if (i >= 3) {91 c.close();92 }93 i += 1;94 }95 });96 const chunks = [];97 for await (const chunk of s) {98 chunks.push(chunk);99 }100 assert_array_equals(chunks, [undefined, undefined, undefined]);101}, 'Async-iterating a pull source with undefined values');102promise_test(async () => {103 let i = 1;104 const s = recordingReadableStream({105 pull(c) {106 c.enqueue(i);107 if (i >= 3) {108 c.close();109 }110 i += 1;111 },112 }, new CountQueuingStrategy({ highWaterMark: 0 }));113 const it = s.values();114 assert_array_equals(s.events, []);115 const read1 = await it.next();116 assert_iter_result(read1, 1, false);117 assert_array_equals(s.events, ['pull']);118 const read2 = await it.next();119 assert_iter_result(read2, 2, false);120 assert_array_equals(s.events, ['pull', 'pull']);121 const read3 = await it.next();122 assert_iter_result(read3, 3, false);123 assert_array_equals(s.events, ['pull', 'pull', 'pull']);124 const read4 = await it.next();125 assert_iter_result(read4, undefined, true);126 assert_array_equals(s.events, ['pull', 'pull', 'pull']);127}, 'Async-iterating a pull source manually');128promise_test(async () => {129 const s = new ReadableStream({130 start(c) {131 c.error('e');132 },133 });134 try {135 for await (const chunk of s) {}136 assert_unreached();137 } catch (e) {138 assert_equals(e, 'e');139 }140}, 'Async-iterating an errored stream throws');141promise_test(async () => {142 const s = new ReadableStream({143 start(c) {144 c.close();145 }146 });147 for await (const chunk of s) {148 assert_unreached();149 }150}, 'Async-iterating a closed stream never executes the loop body, but works fine');151promise_test(async () => {152 const s = new ReadableStream();153 const loop = async () => {154 for await (const chunk of s) {155 assert_unreached();156 }157 assert_unreached();158 };159 await Promise.race([160 loop(),161 flushAsyncEvents()162 ]);163}, 'Async-iterating an empty but not closed/errored stream never executes the loop body and stalls the async function');164promise_test(async () => {165 const s = new ReadableStream({166 start(c) {167 c.enqueue(1);168 c.enqueue(2);169 c.enqueue(3);170 c.close();171 },172 });173 const reader = s.getReader();174 const readResult = await reader.read();175 assert_iter_result(readResult, 1, false);176 reader.releaseLock();177 const chunks = [];178 for await (const chunk of s) {179 chunks.push(chunk);180 }181 assert_array_equals(chunks, [2, 3]);182}, 'Async-iterating a partially consumed stream');183for (const type of ['throw', 'break', 'return']) {184 for (const preventCancel of [false, true]) {185 promise_test(async () => {186 const s = recordingReadableStream({187 start(c) {188 c.enqueue(0);189 }190 });191 // use a separate function for the loop body so return does not stop the test192 const loop = async () => {193 for await (const c of s.values({ preventCancel })) {194 if (type === 'throw') {195 throw new Error();196 } else if (type === 'break') {197 break;198 } else if (type === 'return') {199 return;200 }201 }202 };203 try {204 await loop();205 } catch (e) {}206 if (preventCancel) {207 assert_array_equals(s.events, ['pull'], `cancel() should not be called`);208 } else {209 assert_array_equals(s.events, ['pull', 'cancel', undefined], `cancel() should be called`);210 }211 }, `Cancellation behavior when ${type}ing inside loop body; preventCancel = ${preventCancel}`);212 }213}214for (const preventCancel of [false, true]) {215 promise_test(async () => {216 const s = recordingReadableStream({217 start(c) {218 c.enqueue(0);219 }220 });221 const it = s.values({ preventCancel });222 await it.return();223 if (preventCancel) {224 assert_array_equals(s.events, [], `cancel() should not be called`);225 } else {226 assert_array_equals(s.events, ['cancel', undefined], `cancel() should be called`);227 }228 }, `Cancellation behavior when manually calling return(); preventCancel = ${preventCancel}`);229}230promise_test(async t => {231 let timesPulled = 0;232 const s = new ReadableStream({233 pull(c) {234 if (timesPulled === 0) {235 c.enqueue(0);236 ++timesPulled;237 } else {238 c.error(error1);239 }240 }241 });242 const it = s[Symbol.asyncIterator]();243 const iterResult1 = await it.next();244 assert_iter_result(iterResult1, 0, false, '1st next()');245 await promise_rejects_exactly(t, error1, it.next(), '2nd next()');246}, 'next() rejects if the stream errors');247promise_test(async () => {248 let timesPulled = 0;249 const s = new ReadableStream({250 pull(c) {251 if (timesPulled === 0) {252 c.enqueue(0);253 ++timesPulled;254 } else {255 c.error(error1);256 }257 }258 });259 const it = s[Symbol.asyncIterator]();260 const iterResult = await it.return('return value');261 assert_iter_result(iterResult, 'return value', true);262}, 'return() does not rejects if the stream has not errored yet');263promise_test(async t => {264 let timesPulled = 0;265 const s = new ReadableStream({266 pull(c) {267 // Do not error in start() because doing so would prevent acquiring a reader/async iterator.268 c.error(error1);269 }270 });271 const it = s[Symbol.asyncIterator]();272 await flushAsyncEvents();273 await promise_rejects_exactly(t, error1, it.return('return value'));274}, 'return() rejects if the stream has errored');275promise_test(async t => {276 let timesPulled = 0;277 const s = new ReadableStream({278 pull(c) {279 if (timesPulled === 0) {280 c.enqueue(0);281 ++timesPulled;282 } else {283 c.error(error1);284 }285 }286 });287 const it = s[Symbol.asyncIterator]();288 const iterResult1 = await it.next();289 assert_iter_result(iterResult1, 0, false, '1st next()');290 await promise_rejects_exactly(t, error1, it.next(), '2nd next()');291 const iterResult3 = await it.next();292 assert_iter_result(iterResult3, undefined, true, '3rd next()');293}, 'next() that succeeds; next() that reports an error; next()');294promise_test(async () => {295 let timesPulled = 0;296 const s = new ReadableStream({297 pull(c) {298 if (timesPulled === 0) {299 c.enqueue(0);300 ++timesPulled;301 } else {302 c.error(error1);303 }304 }305 });306 const it = s[Symbol.asyncIterator]();307 const iterResults = await Promise.allSettled([it.next(), it.next(), it.next()]);308 assert_equals(iterResults[0].status, 'fulfilled', '1st next() promise status');309 assert_iter_result(iterResults[0].value, 0, false, '1st next()');310 assert_equals(iterResults[1].status, 'rejected', '2nd next() promise status');311 assert_equals(iterResults[1].reason, error1, '2nd next() rejection reason');312 assert_equals(iterResults[2].status, 'fulfilled', '3rd next() promise status');313 assert_iter_result(iterResults[2].value, undefined, true, '3rd next()');314}, 'next() that succeeds; next() that reports an error(); next() [no awaiting]');315promise_test(async t => {316 let timesPulled = 0;317 const s = new ReadableStream({318 pull(c) {319 if (timesPulled === 0) {320 c.enqueue(0);321 ++timesPulled;322 } else {323 c.error(error1);324 }325 }326 });327 const it = s[Symbol.asyncIterator]();328 const iterResult1 = await it.next();329 assert_iter_result(iterResult1, 0, false, '1st next()');330 await promise_rejects_exactly(t, error1, it.next(), '2nd next()');331 const iterResult3 = await it.return('return value');332 assert_iter_result(iterResult3, 'return value', true, 'return()');333}, 'next() that succeeds; next() that reports an error(); return()');334promise_test(async () => {335 let timesPulled = 0;336 const s = new ReadableStream({337 pull(c) {338 if (timesPulled === 0) {339 c.enqueue(0);340 ++timesPulled;341 } else {342 c.error(error1);343 }344 }345 });346 const it = s[Symbol.asyncIterator]();347 const iterResults = await Promise.allSettled([it.next(), it.next(), it.return('return value')]);348 assert_equals(iterResults[0].status, 'fulfilled', '1st next() promise status');349 assert_iter_result(iterResults[0].value, 0, false, '1st next()');350 assert_equals(iterResults[1].status, 'rejected', '2nd next() promise status');351 assert_equals(iterResults[1].reason, error1, '2nd next() rejection reason');352 assert_equals(iterResults[2].status, 'fulfilled', 'return() promise status');353 assert_iter_result(iterResults[2].value, 'return value', true, 'return()');354}, 'next() that succeeds; next() that reports an error(); return() [no awaiting]');355promise_test(async () => {356 let timesPulled = 0;357 const s = new ReadableStream({358 pull(c) {359 c.enqueue(timesPulled);360 ++timesPulled;361 }362 });363 const it = s[Symbol.asyncIterator]();364 const iterResult1 = await it.next();365 assert_iter_result(iterResult1, 0, false, 'next()');366 const iterResult2 = await it.return('return value');367 assert_iter_result(iterResult2, 'return value', true, 'return()');368 assert_equals(timesPulled, 2);369}, 'next() that succeeds; return()');370promise_test(async () => {371 let timesPulled = 0;372 const s = new ReadableStream({373 pull(c) {374 c.enqueue(timesPulled);375 ++timesPulled;376 }377 });378 const it = s[Symbol.asyncIterator]();379 const iterResults = await Promise.allSettled([it.next(), it.return('return value')]);380 assert_equals(iterResults[0].status, 'fulfilled', 'next() promise status');381 assert_iter_result(iterResults[0].value, 0, false, 'next()');382 assert_equals(iterResults[1].status, 'fulfilled', 'return() promise status');383 assert_iter_result(iterResults[1].value, 'return value', true, 'return()');384 assert_equals(timesPulled, 2);385}, 'next() that succeeds; return() [no awaiting]');386promise_test(async () => {387 const rs = new ReadableStream();388 const it = rs.values();389 const iterResult1 = await it.return('return value');390 assert_iter_result(iterResult1, 'return value', true, 'return()');391 const iterResult2 = await it.next();392 assert_iter_result(iterResult2, undefined, true, 'next()');393}, 'return(); next()');394promise_test(async () => {395 const rs = new ReadableStream();396 const it = rs.values();397 const iterResults = await Promise.allSettled([it.return('return value'), it.next()]);398 assert_equals(iterResults[0].status, 'fulfilled', 'return() promise status');399 assert_iter_result(iterResults[0].value, 'return value', true, 'return()');400 assert_equals(iterResults[1].status, 'fulfilled', 'next() promise status');401 assert_iter_result(iterResults[1].value, undefined, true, 'next()');402}, 'return(); next() [no awaiting]');403promise_test(async () => {404 const rs = new ReadableStream();405 const it = rs.values();406 const iterResult1 = await it.return('return value 1');407 assert_iter_result(iterResult1, 'return value 1', true, '1st return()');408 const iterResult2 = await it.return('return value 2');409 assert_iter_result(iterResult2, 'return value 2', true, '1st return()');410}, 'return(); return()');411promise_test(async () => {412 const rs = new ReadableStream();413 const it = rs.values();414 const iterResults = await Promise.allSettled([it.return('return value 1'), it.return('return value 2')]);415 assert_equals(iterResults[0].status, 'fulfilled', '1st return() promise status');416 assert_iter_result(iterResults[0].value, 'return value 1', true, '1st return()');417 assert_equals(iterResults[1].status, 'fulfilled', '2nd return() promise status');418 assert_iter_result(iterResults[1].value, 'return value 2', true, '1st return()');419}, 'return(); return() [no awaiting]');420test(() => {421 const s = new ReadableStream({422 start(c) {423 c.enqueue(0);424 c.close();425 },426 });427 s.values();428 assert_throws_js(TypeError, () => s.values(), 'values() should throw');429}, 'values() throws if there\'s already a lock');430promise_test(async () => {431 const s = new ReadableStream({432 start(c) {433 c.enqueue(1);434 c.enqueue(2);435 c.enqueue(3);436 c.close();437 }438 });439 const chunks = [];440 for await (const chunk of s) {441 chunks.push(chunk);442 }443 assert_array_equals(chunks, [1, 2, 3]);444 const reader = s.getReader();445 await reader.closed;446}, 'Acquiring a reader after exhaustively async-iterating a stream');447promise_test(async t => {448 let timesPulled = 0;449 const s = new ReadableStream({450 pull(c) {451 if (timesPulled === 0) {452 c.enqueue(0);453 ++timesPulled;454 } else {455 c.error(error1);456 }457 }458 });459 const it = s[Symbol.asyncIterator]({ preventCancel: true });460 const iterResult1 = await it.next();461 assert_iter_result(iterResult1, 0, false, '1st next()');462 await promise_rejects_exactly(t, error1, it.next(), '2nd next()');463 const iterResult2 = await it.return('return value');464 assert_iter_result(iterResult2, 'return value', true, 'return()');465 // i.e. it should not reject with a generic "this stream is locked" TypeError.466 const reader = s.getReader();467 await promise_rejects_exactly(t, error1, reader.closed, 'closed on the new reader should reject with the error');468}, 'Acquiring a reader after return()ing from a stream that errors');469promise_test(async () => {470 const s = new ReadableStream({471 start(c) {472 c.enqueue(1);473 c.enqueue(2);474 c.enqueue(3);475 c.close();476 },477 });478 // read the first two chunks, then cancel479 const chunks = [];480 for await (const chunk of s) {481 chunks.push(chunk);482 if (chunk >= 2) {483 break;484 }485 }486 assert_array_equals(chunks, [1, 2]);487 const reader = s.getReader();488 await reader.closed;489}, 'Acquiring a reader after partially async-iterating a stream');490promise_test(async () => {491 const s = new ReadableStream({492 start(c) {493 c.enqueue(1);494 c.enqueue(2);495 c.enqueue(3);496 c.close();497 },498 });499 // read the first two chunks, then release lock500 const chunks = [];501 for await (const chunk of s.values({preventCancel: true})) {502 chunks.push(chunk);503 if (chunk >= 2) {504 break;505 }506 }507 assert_array_equals(chunks, [1, 2]);508 const reader = s.getReader();509 const readResult = await reader.read();510 assert_iter_result(readResult, 3, false);511 await reader.closed;512}, 'Acquiring a reader and reading the remaining chunks after partially async-iterating a stream with preventCancel = true');513for (const preventCancel of [false, true]) {514 test(() => {515 const rs = new ReadableStream();516 rs.values({ preventCancel }).return();517 // The test passes if this line doesn't throw.518 rs.getReader();519 }, `return() should unlock the stream synchronously when preventCancel = ${preventCancel}`);...
Using AI Code Generation
1var assert_iter_result = $262.assertIteratorResult;2var iter = (function* () {3 yield 1;4 yield 2;5 yield 3;6}());7var result = iter.next();8assert_iter_result(result, { value: 1, done: false });9result = iter.next();10assert_iter_result(result, { value: 2, done: false });11result = iter.next();12assert_iter_result(result, { value: 3, done: false });13result = iter.next();14assert_iter_result(result, { value: undefined, done: true });
Using AI Code Generation
1function* g() {2 yield 1;3 yield 2;4 yield 3;5}6function* h() {7 yield* g();8 yield 4;9 yield 5;10 yield 6;11}12var iter = h();13assert_iter_result(iter.next(), { value: 1, done: false });14assert_iter_result(iter.next(), { value: 2, done: false });15assert_iter_result(iter.next(), { value: 3, done: false });16assert_iter_result(iter.next(), { value: 4, done: false });17assert_iter_result(iter.next(), { value: 5, done: false });18assert_iter_result(iter.next(), { value: 6, done: false });19assert_iter_result(iter.next(), { value: undefined, done: true });
Using AI Code Generation
1import {assert_iter_result} from "./assert.js";2import {iterator} from "./iterator.js";3import {iterator_helper} from "./iterator-helper.js";4import {async_iterator_helper} from "./async-iterator-helper.js";5import {async_iterator} from "./async-iterator.js";6import {async_iterator_from_array} from "./async-iterator-from-array.js";7import {async_iterator_from_array_buffer} from "./async-iterator-from-array-buffer.js";8import {async_iterator_from_string} from "./async-iterator-from-string.js";9import {async_iterator_from_stream} from "./async-iterator-from-stream.js";10import {async_iterator_from_reader} from "./async-iterator-from-reader.js";11import {async_iterator_from_blob} from "./async-iterator-from-blob.js";12import {async_iterator_from_file} from "./async-iterator-from-file.js";13import {async_iterator_from_file_list} from "./async-iterator-from-file-list.js";14import {async_iterator_from_data_transfer} from "./async-iterator-from-data-transfer.js";15import {async_iterator_from_data_transfer_item} from "./async-iterator-from-data-transfer-item.js";16import {async_iterator_from_data_transfer_item_list} from "./async-iterator-from-data-transfer-item-list.js";17import {async_iterator_from_url} from "./async-iterator-from-url.js";18import {async_iterator_from_url_list} from "./async-iterator-from-url-list.js";
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!!