Skip to content

Commit cc44e9d

Browse files
Ensure that readdir.iterable() behaves consistently regardless of the syntax used to read the iterator
1 parent e73021b commit cc44e9d

File tree

1 file changed

+61
-3
lines changed

1 file changed

+61
-3
lines changed

test/utils/for-each-api.js

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function forEachApi (tests) {
4040

4141
describe("Callback API", () => {
4242
for (let test of tests) {
43-
testApi(test, "async", done => {
43+
testApi(test, "callback", done => {
4444
let args = test.args.length === 0 ? [undefined, done] : [...test.args, done];
4545
readdir.async.apply(null, args);
4646
});
@@ -81,7 +81,7 @@ function forEachApi (tests) {
8181
}
8282
});
8383

84-
describe("Iterator API", () => {
84+
describe("Iterator API (for await...of)", () => {
8585
for (let test of tests) {
8686
testApi(test, "iterator", async (done) => {
8787
try {
@@ -98,13 +98,71 @@ function forEachApi (tests) {
9898
});
9999
}
100100
});
101+
102+
describe("Iterator API (await iterator.next())", () => {
103+
for (let test of tests) {
104+
testApi(test, "await next", async (done) => {
105+
try {
106+
let iterator = readdir.iterator.apply(null, test.args);
107+
let data = [];
108+
109+
// eslint-disable-next-line no-constant-condition
110+
while (true) {
111+
let result = await iterator.next();
112+
113+
if (result.done) {
114+
break;
115+
}
116+
else {
117+
data.push(result.value);
118+
}
119+
}
120+
121+
done(null, data);
122+
}
123+
catch (error) {
124+
done(error);
125+
}
126+
});
127+
}
128+
});
129+
130+
describe("Iterator API (iterator.next() without await)", () => {
131+
for (let test of tests) {
132+
testApi(test, "next", async (done) => {
133+
try {
134+
let iterator = readdir.iterator.apply(null, test.args);
135+
let promises = [];
136+
137+
for (let i = 0; i < 50; i++) {
138+
let promise = iterator.next();
139+
promises.push(promise);
140+
}
141+
142+
let results = await Promise.all(promises);
143+
let data = [];
144+
145+
for (let result of results) {
146+
if (!result.done) {
147+
data.push(result.value);
148+
}
149+
}
150+
151+
done(null, data);
152+
}
153+
catch (error) {
154+
done(error);
155+
}
156+
});
157+
}
158+
});
101159
}
102160

103161
/**
104162
* Runs a single test against a single readdir-enhanced API.
105163
*
106164
* @param {object} test - An object containing test info, parameters, and assertions
107-
* @param {string} apiName - The name of the API being tested ("sync", "async", "stream", or "iterator")
165+
* @param {string} apiName - The name of the API being tested ("sync", "async", "stream", etc.)
108166
* @param {function} api - A function that calls the readdir-enhanced API and returns its results
109167
*/
110168
function testApi (test, apiName, api) {

0 commit comments

Comments
 (0)