Skip to content

Commit ce44ff7

Browse files
committed
Remove setTimeout usages in tests
1 parent ccd2a3f commit ce44ff7

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
@@ -1318,87 +1318,106 @@ describe('Execute: Handles basic execution tasks', () => {
13181318
expect(possibleTypes).to.deep.equal([fooObject]);
13191319
});
13201320

1321-
/* c8 ignore start */
13221321
if (hasAbortControllerSupport) {
13231322
it('stops execution and throws an error when signal is aborted', async () => {
1324-
/**
1325-
* This test has 3 resolvers nested in each other.
1326-
* Every resolve function waits 200ms before returning data.
1327-
*
1328-
* The test waits for the first resolver and half of the 2nd resolver execution time (200ms + 100ms)
1329-
* and then aborts the execution.
1330-
*
1331-
* 2nd resolver execution finishes, and we then expect to not execute the 3rd resolver
1332-
* and to get an error about aborted operation.
1333-
*/
1334-
1335-
const WAIT_MS_BEFORE_RESOLVING = 200;
1336-
const ABORT_IN_MS_AFTER_STARTING_EXECUTION =
1337-
WAIT_MS_BEFORE_RESOLVING + WAIT_MS_BEFORE_RESOLVING / 2;
1338-
1339-
const schema = new GraphQLSchema({
1340-
query: new GraphQLObjectType({
1341-
name: 'Query',
1342-
fields: {
1343-
resolvesIn500ms: {
1344-
type: new GraphQLObjectType({
1345-
name: 'ResolvesIn500ms',
1346-
fields: {
1347-
resolvesIn400ms: {
1348-
type: new GraphQLObjectType({
1349-
name: 'ResolvesIn400ms',
1350-
fields: {
1351-
shouldNotBeResolved: {
1352-
type: GraphQLString,
1353-
resolve: () => {
1354-
throw new Error('This should not be executed!');
1355-
},
1356-
},
1357-
},
1358-
}),
1359-
resolve: () =>
1360-
new Promise((resolve) => {
1361-
setTimeout(() => resolve({}), WAIT_MS_BEFORE_RESOLVING);
1362-
}),
1363-
},
1364-
},
1365-
}),
1366-
resolve: () =>
1367-
new Promise((resolve) => {
1368-
setTimeout(() => resolve({}), WAIT_MS_BEFORE_RESOLVING);
1369-
}),
1323+
const TestType: GraphQLObjectType = new GraphQLObjectType({
1324+
name: 'TestType',
1325+
fields: () => ({
1326+
resolveOnNextTick: {
1327+
type: TestType,
1328+
resolve: () => resolveOnNextTick({}),
1329+
},
1330+
string: {
1331+
type: GraphQLString,
1332+
args: {
1333+
value: { type: new GraphQLNonNull(GraphQLString) },
13701334
},
1335+
resolve: (_, { value }) => value,
1336+
},
1337+
abortExecution: {
1338+
type: GraphQLString,
1339+
resolve: () => {
1340+
abortController.abort();
1341+
return 'aborted';
1342+
},
1343+
},
1344+
shouldNotBeResolved: {
1345+
type: GraphQLString,
1346+
/* c8 ignore next */
1347+
resolve: () => 'This should not be executed!',
13711348
},
13721349
}),
13731350
});
1351+
1352+
const schema = new GraphQLSchema({
1353+
query: TestType,
1354+
});
1355+
13741356
const document = parse(`
1375-
query {
1376-
resolvesIn500ms {
1377-
resolvesIn400ms {
1378-
shouldNotBeResolved
1357+
query {
1358+
value1: string(value: "1")
1359+
resolveOnNextTick {
1360+
value2: string(value: "2")
1361+
resolveOnNextTick {
1362+
resolveOnNextTick {
1363+
shouldNotBeResolved
1364+
}
1365+
abortExecution
1366+
}
1367+
}
1368+
alternativeBranch: resolveOnNextTick {
1369+
value3: string(value: "3")
1370+
resolveOnNextTick {
1371+
shouldNotBeResolved
1372+
}
13791373
}
13801374
}
1381-
}
1382-
`);
1375+
`);
13831376

13841377
const abortController = new AbortController();
1385-
const executionPromise = execute({
1378+
const result = await execute({
13861379
schema,
13871380
document,
13881381
signal: abortController.signal,
13891382
});
13901383

1391-
setTimeout(
1392-
() => abortController.abort(),
1393-
ABORT_IN_MS_AFTER_STARTING_EXECUTION,
1394-
);
1395-
1396-
const result = await executionPromise;
1397-
expect(result.errors?.[0].message).to.eq('Execution aborted.');
1398-
expect(result.data).to.eql({
1399-
resolvesIn500ms: { resolvesIn400ms: null },
1384+
expectJSON(result).toDeepEqual({
1385+
data: {
1386+
value1: '1',
1387+
resolveOnNextTick: {
1388+
value2: '2',
1389+
resolveOnNextTick: {
1390+
abortExecution: null,
1391+
resolveOnNextTick: null,
1392+
},
1393+
},
1394+
alternativeBranch: {
1395+
resolveOnNextTick: null,
1396+
value3: '3',
1397+
},
1398+
},
1399+
errors: [
1400+
{
1401+
message: 'Execution aborted.',
1402+
path: ['resolveOnNextTick', 'resolveOnNextTick', 'abortExecution'],
1403+
locations: [{ line: 10, column: 15 }],
1404+
},
1405+
{
1406+
message: 'Execution aborted.',
1407+
path: ['alternativeBranch', 'resolveOnNextTick'],
1408+
locations: [{ line: 15, column: 13 }],
1409+
},
1410+
{
1411+
message: 'Execution aborted.',
1412+
path: [
1413+
'resolveOnNextTick',
1414+
'resolveOnNextTick',
1415+
'resolveOnNextTick',
1416+
],
1417+
locations: [{ line: 7, column: 15 }],
1418+
},
1419+
],
14001420
});
14011421
});
14021422
}
1403-
/* c8 ignore stop */
14041423
});

0 commit comments

Comments
 (0)