Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test: Uncaptured errors in source event stream.
Browse files Browse the repository at this point in the history
Added a test to illustrate a broken issue and potentially underspecified in spec:

If a source event stream emits an *error* instead of an *event*, then that error is passing up through the whole stack and throwing at the consumer of the response event stream. That's very likely not what we want. I have a proposal in this test case for what should happen in that case, similar to what would happen if an error occurred during during the second step of executing an event from the source stream.
leebyron committed May 21, 2017

Verified

This commit was signed with the committer’s verified signature. The key has expired.
addaleax Anna Henningsen
1 parent f6f26fd commit 8288f7c
Showing 1 changed file with 185 additions and 0 deletions.
185 changes: 185 additions & 0 deletions src/subscription/__tests__/subscribe-test.js
Original file line number Diff line number Diff line change
@@ -630,4 +630,189 @@ describe('Subscribe', () => {
);
}).to.throw('test error');
});

it('should handle error during execuction of source event', async () => {
const erroringEmailSchema = new GraphQLSchema({
query: QueryType,
subscription: new GraphQLObjectType({
name: 'Subscription',
fields: {
importantEmail: {
type: GraphQLString,
resolve(event) {
if (event === 'Goodbye') {
throw new Error('Never leave.');
}
return event;
},
subscribe: async function* importantEmail() {
yield 'Hello';
yield 'Goodbye';
},
},
},
})
});

const subscription = subscribe(
erroringEmailSchema,
parse(`
subscription {
importantEmail
}
`)
);

const payload1 = await subscription.next();
expect(payload1).to.jsonEqual({
done: false,
value: {
data: {
importantEmail: 'Hello'
}
}
});

const payload2 = await subscription.next();
expect(payload2).to.jsonEqual({
done: false,
value: {
errors: [
{
message: 'Never leave.',
locations: [ { line: 3, column: 11 } ],
path: [ 'importantEmail' ],
}
],
data: {
importantEmail: null,
}
}
});
});

function emailSchemaWithSubscribeFn(subscribeFn) {
return new GraphQLSchema({
query: QueryType,
subscription: new GraphQLObjectType({
name: 'Subscription',
fields: {
importantEmail: {
type: GraphQLString,
resolve(event) {
return event;
},
subscribe: subscribeFn,
},
},
})
});
}

it('should pass through error thrown in source event stream', async () => {
const erroringEmailSchema = emailSchemaWithSubscribeFn(
async function* importantEmail() {
yield 'Hello';
throw new Error('test error');
}
);

const subscription = subscribe(
erroringEmailSchema,
parse(`
subscription {
importantEmail
}
`)
);

const payload1 = await subscription.next();
expect(payload1).to.jsonEqual({
done: false,
value: {
data: {
importantEmail: 'Hello'
}
}
});

let expectedError;
try {
await subscription.next();
} catch (error) {
expectedError = error;
}

expect(expectedError).to.deep.equal(new Error('test error'));

const payload2 = await subscription.next();
expect(payload2).to.jsonEqual({
done: true,
value: undefined
});
});

it('represents thrown error as rejected event stream', async () => {
const erroringEmailSchema = emailSchemaWithSubscribeFn(
function importantEmail() {
throw new Error('test error');
}
);

const subscription = subscribe(
erroringEmailSchema,
parse(`
subscription {
importantEmail
}
`)
);

let expectedError;
try {
await subscription.next();
} catch (error) {
expectedError = error;
}

expect(expectedError).to.deep.equal(new Error('test error'));

const payload1 = await subscription.next();
expect(payload1).to.jsonEqual({
done: true,
value: undefined
});
});

it('represents returned error as rejected event stream', async () => {
const erroringEmailSchema = emailSchemaWithSubscribeFn(
function importantEmail() {
return new Error('test error');
}
);

const subscription = subscribe(
erroringEmailSchema,
parse(`
subscription {
importantEmail
}
`)
);

let expectedError;
try {
await subscription.next();
} catch (error) {
expectedError = error;
}

expect(expectedError).to.deep.equal(new Error('test error'));

const payload1 = await subscription.next();
expect(payload1).to.jsonEqual({
done: true,
value: undefined
});
});
});

0 comments on commit 8288f7c

Please sign in to comment.