Skip to content

Commit cf48da4

Browse files
committed
Remove setTimeout usages in tests
1 parent c7994da commit cf48da4

File tree

2 files changed

+87
-66
lines changed

2 files changed

+87
-66
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
export function resolveOnNextTick(): Promise<void> {
2-
return Promise.resolve(undefined);
1+
export function resolveOnNextTick(): Promise<void>;
2+
export function resolveOnNextTick<T>(value: T): Promise<T>;
3+
export function resolveOnNextTick(value: unknown = undefined): Promise<unknown> {
4+
return Promise.resolve(value);
35
}

src/execution/__tests__/executor-test.ts

Lines changed: 83 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,87 +1280,106 @@ describe('Execute: Handles basic execution tasks', () => {
12801280
expect(possibleTypes).to.deep.equal([fooObject]);
12811281
});
12821282

1283-
/* c8 ignore start */
12841283
if (hasAbortControllerSupport) {
12851284
it('stops execution and throws an error when signal is aborted', async () => {
1286-
/**
1287-
* This test has 3 resolvers nested in each other.
1288-
* Every resolve function waits 200ms before returning data.
1289-
*
1290-
* The test waits for the first resolver and half of the 2nd resolver execution time (200ms + 100ms)
1291-
* and then aborts the execution.
1292-
*
1293-
* 2nd resolver execution finishes, and we then expect to not execute the 3rd resolver
1294-
* and to get an error about aborted operation.
1295-
*/
1296-
1297-
const WAIT_MS_BEFORE_RESOLVING = 200;
1298-
const ABORT_IN_MS_AFTER_STARTING_EXECUTION =
1299-
WAIT_MS_BEFORE_RESOLVING + WAIT_MS_BEFORE_RESOLVING / 2;
1300-
1301-
const schema = new GraphQLSchema({
1302-
query: new GraphQLObjectType({
1303-
name: 'Query',
1304-
fields: {
1305-
resolvesIn500ms: {
1306-
type: new GraphQLObjectType({
1307-
name: 'ResolvesIn500ms',
1308-
fields: {
1309-
resolvesIn400ms: {
1310-
type: new GraphQLObjectType({
1311-
name: 'ResolvesIn400ms',
1312-
fields: {
1313-
shouldNotBeResolved: {
1314-
type: GraphQLString,
1315-
resolve: () => {
1316-
throw new Error('This should not be executed!');
1317-
},
1318-
},
1319-
},
1320-
}),
1321-
resolve: () =>
1322-
new Promise((resolve) => {
1323-
setTimeout(() => resolve({}), WAIT_MS_BEFORE_RESOLVING);
1324-
}),
1325-
},
1326-
},
1327-
}),
1328-
resolve: () =>
1329-
new Promise((resolve) => {
1330-
setTimeout(() => resolve({}), WAIT_MS_BEFORE_RESOLVING);
1331-
}),
1285+
const TestType: GraphQLObjectType = new GraphQLObjectType({
1286+
name: 'TestType',
1287+
fields: () => ({
1288+
resolveOnNextTick: {
1289+
type: TestType,
1290+
resolve: () => resolveOnNextTick({}),
1291+
},
1292+
string: {
1293+
type: GraphQLString,
1294+
args: {
1295+
value: { type: new GraphQLNonNull(GraphQLString) },
13321296
},
1297+
resolve: (_, { value }) => value,
1298+
},
1299+
abortExecution: {
1300+
type: GraphQLString,
1301+
resolve: () => {
1302+
abortController.abort();
1303+
return 'aborted';
1304+
},
1305+
},
1306+
shouldNotBeResolved: {
1307+
type: GraphQLString,
1308+
/* c8 ignore next */
1309+
resolve: () => 'This should not be executed!',
13331310
},
13341311
}),
13351312
});
1313+
1314+
const schema = new GraphQLSchema({
1315+
query: TestType,
1316+
});
1317+
13361318
const document = parse(`
1337-
query {
1338-
resolvesIn500ms {
1339-
resolvesIn400ms {
1340-
shouldNotBeResolved
1319+
query {
1320+
value1: string(value: "1")
1321+
resolveOnNextTick {
1322+
value2: string(value: "2")
1323+
resolveOnNextTick {
1324+
resolveOnNextTick {
1325+
shouldNotBeResolved
1326+
}
1327+
abortExecution
1328+
}
1329+
}
1330+
alternativeBranch: resolveOnNextTick {
1331+
value3: string(value: "3")
1332+
resolveOnNextTick {
1333+
shouldNotBeResolved
1334+
}
13411335
}
13421336
}
1343-
}
1344-
`);
1337+
`);
13451338

13461339
const abortController = new AbortController();
1347-
const executionPromise = execute({
1340+
const result = await execute({
13481341
schema,
13491342
document,
13501343
signal: abortController.signal,
13511344
});
13521345

1353-
setTimeout(
1354-
() => abortController.abort(),
1355-
ABORT_IN_MS_AFTER_STARTING_EXECUTION,
1356-
);
1357-
1358-
const result = await executionPromise;
1359-
expect(result.errors?.[0].message).to.eq('Execution aborted.');
1360-
expect(result.data).to.eql({
1361-
resolvesIn500ms: { resolvesIn400ms: null },
1346+
expectJSON(result).toDeepEqual({
1347+
data: {
1348+
value1: '1',
1349+
resolveOnNextTick: {
1350+
value2: '2',
1351+
resolveOnNextTick: {
1352+
abortExecution: null,
1353+
resolveOnNextTick: null,
1354+
},
1355+
},
1356+
alternativeBranch: {
1357+
resolveOnNextTick: null,
1358+
value3: '3',
1359+
},
1360+
},
1361+
errors: [
1362+
{
1363+
message: 'Execution aborted.',
1364+
path: ['resolveOnNextTick', 'resolveOnNextTick', 'abortExecution'],
1365+
locations: [{ line: 10, column: 15 }],
1366+
},
1367+
{
1368+
message: 'Execution aborted.',
1369+
path: ['alternativeBranch', 'resolveOnNextTick'],
1370+
locations: [{ line: 15, column: 13 }],
1371+
},
1372+
{
1373+
message: 'Execution aborted.',
1374+
path: [
1375+
'resolveOnNextTick',
1376+
'resolveOnNextTick',
1377+
'resolveOnNextTick',
1378+
],
1379+
locations: [{ line: 7, column: 15 }],
1380+
},
1381+
],
13621382
});
13631383
});
13641384
}
1365-
/* c8 ignore stop */
13661385
});

0 commit comments

Comments
 (0)