Skip to content
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

subscribe-test: extract subscribeWithBadFn function #3630

Merged
merged 1 commit into from
Jun 8, 2022
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
67 changes: 28 additions & 39 deletions src/execution/__tests__/subscribe-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { GraphQLList, GraphQLObjectType } from '../../type/definition';
import { GraphQLBoolean, GraphQLInt, GraphQLString } from '../../type/scalars';
import { GraphQLSchema } from '../../type/schema';

import type { ExecutionResult } from '../execute';
import { createSourceEventStream, subscribe } from '../subscribe';

import { SimplePubSub } from './simplePubSub';
Expand Down Expand Up @@ -151,6 +152,28 @@ const DummyQueryType = new GraphQLObjectType({
},
});

async function subscribeWithBadFn(
subscribeFn: () => unknown,
): Promise<ExecutionResult> {
const schema = new GraphQLSchema({
query: DummyQueryType,
subscription: new GraphQLObjectType({
name: 'Subscription',
fields: {
foo: { type: GraphQLString, subscribe: subscribeFn },
},
}),
});
const document = parse('subscription { foo }');
const result = await subscribe({ schema, document });

assert(!isAsyncIterable(result));
expectJSON(await createSourceEventStream(schema, document)).toDeepEqual(
result,
);
return result;
}

/* eslint-disable @typescript-eslint/require-await */
// Check all error cases when initializing the subscription.
describe('Subscription Initialization Phase', () => {
Expand Down Expand Up @@ -431,46 +454,12 @@ describe('Subscription Initialization Phase', () => {
});

it('throws an error if subscribe does not return an iterator', async () => {
const schema = new GraphQLSchema({
query: DummyQueryType,
subscription: new GraphQLObjectType({
name: 'Subscription',
fields: {
foo: {
type: GraphQLString,
subscribe: () => 'test',
},
},
}),
});

const document = parse('subscription { foo }');

(await expectPromise(subscribe({ schema, document }))).toRejectWith(
(await expectPromise(subscribeWithBadFn(() => 'test'))).toRejectWith(
'Subscription field must return Async Iterable. Received: "test".',
);
});

it('resolves to an error for subscription resolver errors', async () => {
async function subscribeWithFn(subscribeFn: () => unknown) {
const schema = new GraphQLSchema({
query: DummyQueryType,
subscription: new GraphQLObjectType({
name: 'Subscription',
fields: {
foo: { type: GraphQLString, subscribe: subscribeFn },
},
}),
});
const document = parse('subscription { foo }');
const result = await subscribe({ schema, document });

expectJSON(await createSourceEventStream(schema, document)).toDeepEqual(
result,
);
return result;
}

const expectedResult = {
errors: [
{
Expand All @@ -483,24 +472,24 @@ describe('Subscription Initialization Phase', () => {

expectJSON(
// Returning an error
await subscribeWithFn(() => new Error('test error')),
await subscribeWithBadFn(() => new Error('test error')),
).toDeepEqual(expectedResult);

expectJSON(
// Throwing an error
await subscribeWithFn(() => {
await subscribeWithBadFn(() => {
throw new Error('test error');
}),
).toDeepEqual(expectedResult);

expectJSON(
// Resolving to an error
await subscribeWithFn(() => Promise.resolve(new Error('test error'))),
await subscribeWithBadFn(() => Promise.resolve(new Error('test error'))),
).toDeepEqual(expectedResult);

expectJSON(
// Rejecting with an error
await subscribeWithFn(() => Promise.reject(new Error('test error'))),
await subscribeWithBadFn(() => Promise.reject(new Error('test error'))),
).toDeepEqual(expectedResult);
});

Expand Down