Skip to content

Commit 2deb272

Browse files
authored
subscribe-test: extract subscribeWithBadFn function (#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 #3620
1 parent e54ed90 commit 2deb272

File tree

1 file changed

+28
-39
lines changed

1 file changed

+28
-39
lines changed

src/execution/__tests__/subscribe-test.ts

+28-39
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', () => {
@@ -431,46 +454,12 @@ describe('Subscription Initialization Phase', () => {
431454
});
432455

433456
it('throws an error if subscribe does not return an iterator', async () => {
434-
const schema = new GraphQLSchema({
435-
query: DummyQueryType,
436-
subscription: new GraphQLObjectType({
437-
name: 'Subscription',
438-
fields: {
439-
foo: {
440-
type: GraphQLString,
441-
subscribe: () => 'test',
442-
},
443-
},
444-
}),
445-
});
446-
447-
const document = parse('subscription { foo }');
448-
449-
(await expectPromise(subscribe({ schema, document }))).toRejectWith(
457+
(await expectPromise(subscribeWithBadFn(() => 'test'))).toRejectWith(
450458
'Subscription field must return Async Iterable. Received: "test".',
451459
);
452460
});
453461

454462
it('resolves to an error for subscription resolver errors', async () => {
455-
async function subscribeWithFn(subscribeFn: () => unknown) {
456-
const schema = new GraphQLSchema({
457-
query: DummyQueryType,
458-
subscription: new GraphQLObjectType({
459-
name: 'Subscription',
460-
fields: {
461-
foo: { type: GraphQLString, subscribe: subscribeFn },
462-
},
463-
}),
464-
});
465-
const document = parse('subscription { foo }');
466-
const result = await subscribe({ schema, document });
467-
468-
expectJSON(await createSourceEventStream(schema, document)).toDeepEqual(
469-
result,
470-
);
471-
return result;
472-
}
473-
474463
const expectedResult = {
475464
errors: [
476465
{
@@ -483,24 +472,24 @@ describe('Subscription Initialization Phase', () => {
483472

484473
expectJSON(
485474
// Returning an error
486-
await subscribeWithFn(() => new Error('test error')),
475+
await subscribeWithBadFn(() => new Error('test error')),
487476
).toDeepEqual(expectedResult);
488477

489478
expectJSON(
490479
// Throwing an error
491-
await subscribeWithFn(() => {
480+
await subscribeWithBadFn(() => {
492481
throw new Error('test error');
493482
}),
494483
).toDeepEqual(expectedResult);
495484

496485
expectJSON(
497486
// Resolving to an error
498-
await subscribeWithFn(() => Promise.resolve(new Error('test error'))),
487+
await subscribeWithBadFn(() => Promise.resolve(new Error('test error'))),
499488
).toDeepEqual(expectedResult);
500489

501490
expectJSON(
502491
// Rejecting with an error
503-
await subscribeWithFn(() => Promise.reject(new Error('test error'))),
492+
await subscribeWithBadFn(() => Promise.reject(new Error('test error'))),
504493
).toDeepEqual(expectedResult);
505494
});
506495

0 commit comments

Comments
 (0)