Skip to content

Commit de38940

Browse files
committed
subscribe-test: extract subscribeWithBadFn function (graphql#3630)
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
1 parent b15271c commit de38940

File tree

1 file changed

+28
-39
lines changed

1 file changed

+28
-39
lines changed

src/execution/__tests__/subscribe-test.ts

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { GraphQLList, GraphQLObjectType } from '../../type/definition';
1212
import { GraphQLBoolean, GraphQLInt, GraphQLString } from '../../type/scalars';
1313
import { GraphQLSchema } from '../../type/schema';
1414

15+
import type { ExecutionResult } from '../execute';
1516
import { createSourceEventStream, subscribe } from '../subscribe';
1617

1718
import { SimplePubSub } from './simplePubSub';
@@ -151,6 +152,28 @@ const DummyQueryType = new GraphQLObjectType({
151152
},
152153
});
153154

155+
async function subscribeWithBadFn(
156+
subscribeFn: () => unknown,
157+
): Promise<ExecutionResult> {
158+
const schema = new GraphQLSchema({
159+
query: DummyQueryType,
160+
subscription: new GraphQLObjectType({
161+
name: 'Subscription',
162+
fields: {
163+
foo: { type: GraphQLString, subscribe: subscribeFn },
164+
},
165+
}),
166+
});
167+
const document = parse('subscription { foo }');
168+
const result = await subscribe({ schema, document });
169+
170+
assert(!isAsyncIterable(result));
171+
expectJSON(await createSourceEventStream(schema, document)).toDeepEqual(
172+
result,
173+
);
174+
return result;
175+
}
176+
154177
/* eslint-disable @typescript-eslint/require-await */
155178
// Check all error cases when initializing the subscription.
156179
describe('Subscription Initialization Phase', () => {
@@ -490,46 +513,12 @@ describe('Subscription Initialization Phase', () => {
490513
});
491514

492515
it('throws an error if subscribe does not return an iterator', async () => {
493-
const schema = new GraphQLSchema({
494-
query: DummyQueryType,
495-
subscription: new GraphQLObjectType({
496-
name: 'Subscription',
497-
fields: {
498-
foo: {
499-
type: GraphQLString,
500-
subscribe: () => 'test',
501-
},
502-
},
503-
}),
504-
});
505-
506-
const document = parse('subscription { foo }');
507-
508-
(await expectPromise(subscribe({ schema, document }))).toRejectWith(
516+
(await expectPromise(subscribeWithBadFn(() => 'test'))).toRejectWith(
509517
'Subscription field must return Async Iterable. Received: "test".',
510518
);
511519
});
512520

513521
it('resolves to an error for subscription resolver errors', async () => {
514-
async function subscribeWithFn(subscribeFn: () => unknown) {
515-
const schema = new GraphQLSchema({
516-
query: DummyQueryType,
517-
subscription: new GraphQLObjectType({
518-
name: 'Subscription',
519-
fields: {
520-
foo: { type: GraphQLString, subscribe: subscribeFn },
521-
},
522-
}),
523-
});
524-
const document = parse('subscription { foo }');
525-
const result = await subscribe({ schema, document });
526-
527-
expectJSON(await createSourceEventStream(schema, document)).toDeepEqual(
528-
result,
529-
);
530-
return result;
531-
}
532-
533522
const expectedResult = {
534523
errors: [
535524
{
@@ -542,24 +531,24 @@ describe('Subscription Initialization Phase', () => {
542531

543532
expectJSON(
544533
// Returning an error
545-
await subscribeWithFn(() => new Error('test error')),
534+
await subscribeWithBadFn(() => new Error('test error')),
546535
).toDeepEqual(expectedResult);
547536

548537
expectJSON(
549538
// Throwing an error
550-
await subscribeWithFn(() => {
539+
await subscribeWithBadFn(() => {
551540
throw new Error('test error');
552541
}),
553542
).toDeepEqual(expectedResult);
554543

555544
expectJSON(
556545
// Resolving to an error
557-
await subscribeWithFn(() => Promise.resolve(new Error('test error'))),
546+
await subscribeWithBadFn(() => Promise.resolve(new Error('test error'))),
558547
).toDeepEqual(expectedResult);
559548

560549
expectJSON(
561550
// Rejecting with an error
562-
await subscribeWithFn(() => Promise.reject(new Error('test error'))),
551+
await subscribeWithBadFn(() => Promise.reject(new Error('test error'))),
563552
).toDeepEqual(expectedResult);
564553
});
565554

0 commit comments

Comments
 (0)