Skip to content

Commit

Permalink
Remove setTimeout usages in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Jan 31, 2023
1 parent ccd2a3f commit ce44ff7
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 66 deletions.
6 changes: 4 additions & 2 deletions src/__testUtils__/resolveOnNextTick.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export function resolveOnNextTick(): Promise<void> {
return Promise.resolve(undefined);
export function resolveOnNextTick(): Promise<void>;
export function resolveOnNextTick<T>(value: T): Promise<T>;
export function resolveOnNextTick(value: unknown = undefined): Promise<unknown> {
return Promise.resolve(value);
}
147 changes: 83 additions & 64 deletions src/execution/__tests__/executor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1318,87 +1318,106 @@ describe('Execute: Handles basic execution tasks', () => {
expect(possibleTypes).to.deep.equal([fooObject]);
});

/* c8 ignore start */
if (hasAbortControllerSupport) {
it('stops execution and throws an error when signal is aborted', async () => {
/**
* This test has 3 resolvers nested in each other.
* Every resolve function waits 200ms before returning data.
*
* The test waits for the first resolver and half of the 2nd resolver execution time (200ms + 100ms)
* and then aborts the execution.
*
* 2nd resolver execution finishes, and we then expect to not execute the 3rd resolver
* and to get an error about aborted operation.
*/

const WAIT_MS_BEFORE_RESOLVING = 200;
const ABORT_IN_MS_AFTER_STARTING_EXECUTION =
WAIT_MS_BEFORE_RESOLVING + WAIT_MS_BEFORE_RESOLVING / 2;

const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
resolvesIn500ms: {
type: new GraphQLObjectType({
name: 'ResolvesIn500ms',
fields: {
resolvesIn400ms: {
type: new GraphQLObjectType({
name: 'ResolvesIn400ms',
fields: {
shouldNotBeResolved: {
type: GraphQLString,
resolve: () => {
throw new Error('This should not be executed!');
},
},
},
}),
resolve: () =>
new Promise((resolve) => {
setTimeout(() => resolve({}), WAIT_MS_BEFORE_RESOLVING);
}),
},
},
}),
resolve: () =>
new Promise((resolve) => {
setTimeout(() => resolve({}), WAIT_MS_BEFORE_RESOLVING);
}),
const TestType: GraphQLObjectType = new GraphQLObjectType({
name: 'TestType',
fields: () => ({
resolveOnNextTick: {
type: TestType,
resolve: () => resolveOnNextTick({}),
},
string: {
type: GraphQLString,
args: {
value: { type: new GraphQLNonNull(GraphQLString) },
},
resolve: (_, { value }) => value,
},
abortExecution: {
type: GraphQLString,
resolve: () => {
abortController.abort();
return 'aborted';
},
},
shouldNotBeResolved: {
type: GraphQLString,
/* c8 ignore next */
resolve: () => 'This should not be executed!',
},
}),
});

const schema = new GraphQLSchema({
query: TestType,
});

const document = parse(`
query {
resolvesIn500ms {
resolvesIn400ms {
shouldNotBeResolved
query {
value1: string(value: "1")
resolveOnNextTick {
value2: string(value: "2")
resolveOnNextTick {
resolveOnNextTick {
shouldNotBeResolved
}
abortExecution
}
}
alternativeBranch: resolveOnNextTick {
value3: string(value: "3")
resolveOnNextTick {
shouldNotBeResolved
}
}
}
}
`);
`);

const abortController = new AbortController();
const executionPromise = execute({
const result = await execute({
schema,
document,
signal: abortController.signal,
});

setTimeout(
() => abortController.abort(),
ABORT_IN_MS_AFTER_STARTING_EXECUTION,
);

const result = await executionPromise;
expect(result.errors?.[0].message).to.eq('Execution aborted.');
expect(result.data).to.eql({
resolvesIn500ms: { resolvesIn400ms: null },
expectJSON(result).toDeepEqual({
data: {
value1: '1',
resolveOnNextTick: {
value2: '2',
resolveOnNextTick: {
abortExecution: null,
resolveOnNextTick: null,
},
},
alternativeBranch: {
resolveOnNextTick: null,
value3: '3',
},
},
errors: [
{
message: 'Execution aborted.',
path: ['resolveOnNextTick', 'resolveOnNextTick', 'abortExecution'],
locations: [{ line: 10, column: 15 }],
},
{
message: 'Execution aborted.',
path: ['alternativeBranch', 'resolveOnNextTick'],
locations: [{ line: 15, column: 13 }],
},
{
message: 'Execution aborted.',
path: [
'resolveOnNextTick',
'resolveOnNextTick',
'resolveOnNextTick',
],
locations: [{ line: 7, column: 15 }],
},
],
});
});
}
/* c8 ignore stop */
});

0 comments on commit ce44ff7

Please sign in to comment.