Skip to content

Commit

Permalink
subscribe-test: extract subscribeWithBadFn function
Browse files Browse the repository at this point in the history
Two test groups use essentially the same logic to set up a subscription with an improper subscribeFn, testing both `subscribe` and `createSourceEventStream`.

This PR extracts the duplicated logic into a single common `subscribeWithBadFn` function.

For convenience, the common function is typed to appropriately return a `Promise<ExecutionResult>` rather than a `Promise<ExecutionResult | AsyncGenerator<...>>`). Because the `subscribeFn` is expected to be "bad," an `AsyncGenerator` should never be returned. If a valid `subscribeFn` is mistakenly passed, an assertion failure is triggered.

extracted from graphql#3620
  • Loading branch information
yaacovCR committed Jun 7, 2022
1 parent 385597e commit 75d3061
Showing 1 changed file with 28 additions and 39 deletions.
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

0 comments on commit 75d3061

Please sign in to comment.