Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Oct 30, 2024
1 parent 3dd9fb2 commit 7791939
Showing 1 changed file with 108 additions and 10 deletions.
118 changes: 108 additions & 10 deletions src/execution/__tests__/abort-signal-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async function complete(
const schema = buildSchema(`
type Todo {
id: ID
text: String
items: [String]
author: User
}
Expand Down Expand Up @@ -91,7 +91,6 @@ describe('Execute: Cancellation', () => {
todo: async () =>
Promise.resolve({
id: '1',
text: 'Hello, World!',
/* c8 ignore next */
author: () => expect.fail('Should not be called'),
}),
Expand Down Expand Up @@ -186,7 +185,6 @@ describe('Execute: Cancellation', () => {
todo: async () =>
Promise.resolve({
id: '1',
text: 'Hello, World!',
/* c8 ignore next */
author: () => expect.fail('Should not be called'),
}),
Expand Down Expand Up @@ -235,7 +233,6 @@ describe('Execute: Cancellation', () => {
todo: async () =>
Promise.resolve({
id: '1',
text: 'Hello, World!',
/* c8 ignore next */
author: () => expect.fail('Should not be called'),
}),
Expand Down Expand Up @@ -280,7 +277,6 @@ describe('Execute: Cancellation', () => {
rootValue: {
todo: {
id: '1',
text: 'Hello, World!',
/* c8 ignore next 3 */
author: async () =>
Promise.resolve(() => expect.fail('Should not be called')),
Expand Down Expand Up @@ -354,6 +350,56 @@ describe('Execute: Cancellation', () => {
});
});

it('should stop the execution when aborted despite a hanging item', async () => {
const abortController = new AbortController();
const document = parse(`
query {
todo {
id
items
}
}
`);

const resultPromise = execute({
document,
schema,
abortSignal: abortController.signal,
rootValue: {
todo: () => ({
id: '1',
items: [
new Promise(() => {
/* will never resolve */
}),
],
}),
},
});

abortController.abort();

const result = await resultPromise;

expect(result.errors?.[0].originalError?.name).to.equal('AbortError');

expectJSON(result).toDeepEqual({
data: {
todo: {
id: '1',
items: [null],
},
},
errors: [
{
message: 'This operation was aborted',
path: ['todo', 'items', 0],
locations: [{ line: 5, column: 11 }],
},
],
});
});

it('should stop the execution when aborted with proper null bubbling', async () => {
const abortController = new AbortController();
const document = parse(`
Expand All @@ -375,7 +421,6 @@ describe('Execute: Cancellation', () => {
nonNullableTodo: async () =>
Promise.resolve({
id: '1',
text: 'Hello, World!',
/* c8 ignore next */
author: () => expect.fail('Should not be called'),
}),
Expand Down Expand Up @@ -407,7 +452,6 @@ describe('Execute: Cancellation', () => {
todo {
id
... on Todo @defer {
text
author {
id
}
Expand All @@ -423,7 +467,6 @@ describe('Execute: Cancellation', () => {
todo: async () =>
Promise.resolve({
id: '1',
text: 'hello world',
/* c8 ignore next */
author: () => expect.fail('Should not be called'),
}),
Expand Down Expand Up @@ -456,7 +499,6 @@ describe('Execute: Cancellation', () => {
... on Query @defer {
todo {
id
text
author {
id
}
Expand All @@ -471,7 +513,6 @@ describe('Execute: Cancellation', () => {
todo: async () =>
Promise.resolve({
id: '1',
text: 'hello world',
/* c8 ignore next 2 */
author: async () =>
Promise.resolve(() => expect.fail('Should not be called')),
Expand Down Expand Up @@ -512,6 +553,63 @@ describe('Execute: Cancellation', () => {
]);
});

it('should stop streamed execution when aborted', async () => {
const abortController = new AbortController();
const document = parse(`
query {
todo {
id
items @stream
}
}
`);

const resultPromise = complete(
document,
{
todo: {
id: '1',
items: [Promise.resolve('item')],
},
},
abortController.signal,
);

abortController.abort();

const result = await resultPromise;

expectJSON(result).toDeepEqual([
{
data: {
todo: {
id: '1',
items: [],
},
},
pending: [{ id: '0', path: ['todo', 'items'] }],
hasNext: true,
},
{
incremental: [
{
items: [null],
errors: [
{
message: 'This operation was aborted',
path: ['todo', 'items', 0],
locations: [{ line: 5, column: 11 }],
},
],
id: '0',
},
],
completed: [{ id: '0' }],
hasNext: false,
},
]);
});

it('should stop the execution when aborted mid-mutation', async () => {
const abortController = new AbortController();
const document = parse(`
Expand Down

0 comments on commit 7791939

Please sign in to comment.