Skip to content

mapAsyncIterator: improve coverage #2353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 103 additions & 26 deletions src/subscription/__tests__/mapAsyncIterator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import invariant from '../../jsutils/invariant';

import mapAsyncIterator from '../mapAsyncIterator';

describe('mapAsyncIterator', () => {
it('maps over async values', async () => {
it('maps over async generator', async () => {
async function* source() {
yield 1;
yield 2;
Expand All @@ -27,6 +29,49 @@ describe('mapAsyncIterator', () => {
});
});

it('maps over async iterator', async () => {
const items = [1, 2, 3];

const iterator: any = {
// $FlowFixMe Blocked by https://github.com/facebook/flow/issues/3258
[Symbol.asyncIterator]() {
return this;
},
next() {
return Promise.resolve({
done: items.length === 0,
value: items.shift(),
});
},
};

const doubles = mapAsyncIterator(iterator, x => x + x);

expect(await doubles.next()).to.deep.equal({ value: 2, done: false });
expect(await doubles.next()).to.deep.equal({ value: 4, done: false });
expect(await doubles.next()).to.deep.equal({ value: 6, done: false });
expect(await doubles.next()).to.deep.equal({
value: undefined,
done: true,
});
});

it('compatible with for-await-of', async () => {
async function* source() {
yield 1;
yield 2;
yield 3;
}

const doubles = mapAsyncIterator(source(), x => x + x);

const result = [];
for await (const x of doubles) {
result.push(x);
}
expect(result).to.deep.equal([2, 4, 6]);
});

it('maps over async values with async function', async () => {
async function* source() {
yield 1;
Expand All @@ -49,10 +94,12 @@ describe('mapAsyncIterator', () => {
});
});

it('allows returning early from async values', async () => {
it('allows returning early from mapped async generator', async () => {
async function* source() {
yield 1;
yield 2;

/* istanbul ignore next (shouldn't be reached) */
yield 3;
}

Expand All @@ -78,11 +125,41 @@ describe('mapAsyncIterator', () => {
});
});

it('allows returning early from mapped async iterator', async () => {
const items = [1, 2, 3];

const iterator: any = {
// $FlowFixMe Blocked by https://github.com/facebook/flow/issues/3258
[Symbol.asyncIterator]() {
return this;
},
next() {
return Promise.resolve({
done: items.length === 0,
value: items.shift(),
});
},
};

const doubles = mapAsyncIterator(iterator, x => x + x);

expect(await doubles.next()).to.deep.equal({ value: 2, done: false });
expect(await doubles.next()).to.deep.equal({ value: 4, done: false });

// Early return
expect(await doubles.return()).to.deep.equal({
value: undefined,
done: true,
});
});

it('passes through early return from async values', async () => {
async function* source() {
try {
yield 1;
yield 2;

/* istanbul ignore next (shouldn't be reached) */
yield 3;
} finally {
yield 'Done';
Expand Down Expand Up @@ -112,14 +189,23 @@ describe('mapAsyncIterator', () => {
});
});

it('allows throwing errors through async generators', async () => {
async function* source() {
yield 1;
yield 2;
yield 3;
}
it('allows throwing errors through async iterators', async () => {
const items = [1, 2, 3];

const doubles = mapAsyncIterator(source(), x => x + x);
const iterator: any = {
// $FlowFixMe Blocked by https://github.com/facebook/flow/issues/3258
[Symbol.asyncIterator]() {
return this;
},
next() {
return Promise.resolve({
done: items.length === 0,
value: items.shift(),
});
},
};

const doubles = mapAsyncIterator(iterator, x => x + x);

expect(await doubles.next()).to.deep.equal({ value: 2, done: false });
expect(await doubles.next()).to.deep.equal({ value: 4, done: false });
Expand All @@ -132,22 +218,15 @@ describe('mapAsyncIterator', () => {
caughtError = e;
}
expect(caughtError).to.equal('ouch');

expect(await doubles.next()).to.deep.equal({
value: undefined,
done: true,
});
expect(await doubles.next()).to.deep.equal({
value: undefined,
done: true,
});
});

it('passes through caught errors through async generators', async () => {
async function* source() {
try {
yield 1;
yield 2;

/* istanbul ignore next (shouldn't be reached) */
yield 3;
} catch (e) {
yield e;
Expand Down Expand Up @@ -232,6 +311,8 @@ describe('mapAsyncIterator', () => {
try {
yield 1;
yield 2;

/* istanbul ignore next (shouldn't be reached) */
yield 3;
} finally {
didVisitFinally = true;
Expand All @@ -250,10 +331,8 @@ describe('mapAsyncIterator', () => {
expectedError = error;
}

expect(expectedError).to.be.an('error');
if (expectedError) {
expect(expectedError.message).to.equal('Cannot count to 2');
}
invariant(expectedError instanceof Error);
expect(expectedError.message).to.equal('Cannot count to 2');

expect(await throwOver1.next()).to.deep.equal({
value: undefined,
Expand Down Expand Up @@ -297,10 +376,8 @@ describe('mapAsyncIterator', () => {
expectedError = error;
}

expect(expectedError).to.be.an('error');
if (expectedError) {
expect(expectedError.message).to.equal('Cannot count to 2');
}
invariant(expectedError instanceof Error);
expect(expectedError.message).to.equal('Cannot count to 2');

expect(await throwOver1.next()).to.deep.equal({
value: undefined,
Expand Down