Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscribe hooks fixed #494

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "subscribe hooks fixed",
"packageName": "@graphitation/supermassive",
"email": "[email protected]",
"dependentChangeType": "none"
}
122 changes: 101 additions & 21 deletions packages/supermassive/src/__tests__/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ describe.each([
});
});

describe("error in subscription is thrown", () => {
describe("error in beforeSubscriptionEventEmit", () => {
beforeEach(() => {
jest.clearAllMocks();
});
Expand Down Expand Up @@ -1168,14 +1168,14 @@ describe.each([
);
});

describe("Error in subscription during creating event stream", () => {
describe("afterFieldSubscribe and beforeFieldSubscribe hook errors during creating event stream. It should throw if an error is thrown or returned", () => {
beforeEach(() => {
jest.clearAllMocks();
});

const testCases: Array<HookExceptionTestCase> = [
{
name: "beforeFieldSubscribe (Error is returned)",
name: "afterFieldSubscribe (Error is returned)",
document: `subscription EmitPersons($limit: Int!)
{
emitPersons(limit: $limit) {
Expand All @@ -1186,15 +1186,15 @@ describe.each([
limit: 1,
},
hooks: {
beforeFieldSubscribe: jest.fn().mockImplementation(() => {
afterFieldSubscribe: jest.fn().mockImplementation(() => {
return new Error("Hook error");
}),
},
expectedErrorMessage:
"Unexpected error in beforeFieldSubscribe hook: Hook error",
"Unexpected error in afterFieldSubscribe hook: Hook error",
},
{
name: "afterFieldSubscribe (Error is returned)",
name: "afterFieldSubscribe (Error is thrown)",
document: `subscription EmitPersons($limit: Int!)
{
emitPersons(limit: $limit) {
Expand All @@ -1206,14 +1206,14 @@ describe.each([
},
hooks: {
afterFieldSubscribe: jest.fn().mockImplementation(() => {
return new Error("Hook error");
throw new Error("Hook error");
}),
},
expectedErrorMessage:
"Unexpected error in afterFieldSubscribe hook: Hook error",
},
{
name: "beforeFieldSubscribe (Error is thrown)",
name: "afterFieldSubscribe (string is thrown)",
document: `subscription EmitPersons($limit: Int!)
{
emitPersons(limit: $limit) {
Expand All @@ -1224,15 +1224,15 @@ describe.each([
limit: 1,
},
hooks: {
beforeFieldSubscribe: jest.fn().mockImplementation(() => {
throw new Error("Hook error");
afterFieldSubscribe: jest.fn().mockImplementation(() => {
throw "Hook error";
}),
},
expectedErrorMessage:
"Unexpected error in beforeFieldSubscribe hook: Hook error",
'Unexpected error in afterFieldSubscribe hook: "Hook error"',
},
{
name: "beforeFieldSubscribe (string is thrown)",
name: "beforeFieldSubscribe (Error is thrown)",
document: `subscription EmitPersons($limit: Int!)
{
emitPersons(limit: $limit) {
Expand All @@ -1244,14 +1244,14 @@ describe.each([
},
hooks: {
beforeFieldSubscribe: jest.fn().mockImplementation(() => {
throw "Hook error";
throw new Error("Hook error");
}),
},
expectedErrorMessage:
'Unexpected error in beforeFieldSubscribe hook: "Hook error"',
"Unexpected error in beforeFieldSubscribe hook: Hook error",
},
{
name: "afterFieldSubscribe (Error is thrown)",
name: "beforeFieldSubscribe (Error is returned)",
document: `subscription EmitPersons($limit: Int!)
{
emitPersons(limit: $limit) {
Expand All @@ -1262,15 +1262,15 @@ describe.each([
limit: 1,
},
hooks: {
afterFieldSubscribe: jest.fn().mockImplementation(() => {
throw new Error("Hook error");
beforeFieldSubscribe: jest.fn().mockImplementation(() => {
return new Error("Hook error");
}),
},
expectedErrorMessage:
"Unexpected error in afterFieldSubscribe hook: Hook error",
"Unexpected error in beforeFieldSubscribe hook: Hook error",
},
{
name: "afterFieldSubscribe (string is thrown)",
name: "beforeFieldSubscribe (string is thrown)",
document: `subscription EmitPersons($limit: Int!)
{
emitPersons(limit: $limit) {
Expand All @@ -1281,12 +1281,12 @@ describe.each([
limit: 1,
},
hooks: {
afterFieldSubscribe: jest.fn().mockImplementation(() => {
beforeFieldSubscribe: jest.fn().mockImplementation(() => {
throw "Hook error";
}),
},
expectedErrorMessage:
'Unexpected error in afterFieldSubscribe hook: "Hook error"',
'Unexpected error in beforeFieldSubscribe hook: "Hook error"',
},
];

Expand Down Expand Up @@ -1527,6 +1527,70 @@ describe.each([
);
});

describe("Error in afterBuildResponse", () => {
beforeEach(() => {
jest.clearAllMocks();
});

test("afterBuildResponse (Error is thrown)", async () => {
expect.assertions(5);

const response = await drainExecution(
await execute(
parse(`{
film(id: 1) {
title
}
}`),
resolvers as UserResolvers,
{
afterBuildResponse: jest.fn().mockImplementation(() => {
throw new Error("Hook error");
}),
},
),
);
const result = Array.isArray(response) ? response[0] : response;
expect(isTotalExecutionResult(result)).toBe(true);
const errors = result.errors;
expect(result.data).toBeUndefined();
expect(errors).toBeDefined();
expect(errors).toHaveLength(1);
expect(errors?.[0].message).toBe(
"Unexpected error in afterBuildResponse hook: Hook error",
);
});

test("afterBuildResponse (Error is returned)", async () => {
expect.assertions(5);

const response = await drainExecution(
await execute(
parse(`{
film(id: 1) {
title
}
}`),
resolvers as UserResolvers,
{
afterBuildResponse: jest.fn().mockImplementation(() => {
return new Error("Hook error");
}),
},
),
);
const result = Array.isArray(response) ? response[0] : response;
expect(isTotalExecutionResult(result)).toBe(true);
const errors = result.errors;
expect(result.data).toBeDefined();
expect(errors).toBeDefined();
expect(errors).toHaveLength(1);
expect(errors?.[0].message).toBe(
"Unexpected error in afterBuildResponse hook: Hook error",
);
});
});

describe("Error thrown in the BEFORE OPERATION hook breaks execution", () => {
beforeEach(() => {
jest.clearAllMocks();
Expand Down Expand Up @@ -1608,6 +1672,22 @@ describe.each([
expectedErrorMessage:
"Unexpected error in beforeFieldResolve hook: Hook error",
},
{
name: "beforeOperationExecute (Error is thrown)",
document: `
{
film(id: 1) {
title
}
}`,
hooks: {
beforeOperationExecute: jest.fn().mockImplementation(() => {
return new Error("Hook error");
}),
},
expectedErrorMessage:
"Unexpected error in beforeOperationExecute hook: Hook error",
},
{
name: "afterFieldResolve (Error is thrown)",
document: `
Expand Down
Loading
Loading