Skip to content

Commit

Permalink
subscribe hooks fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
vejrj committed Dec 17, 2024
1 parent e7f15ff commit 76abd5d
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 80 deletions.
106 changes: 85 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
Loading

0 comments on commit 76abd5d

Please sign in to comment.