Skip to content

Commit

Permalink
throws error if publish is called withing calling init method
Browse files Browse the repository at this point in the history
  • Loading branch information
Suraj Keshri committed Apr 13, 2024
1 parent f84c9ac commit 81d354b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-geese-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"graphql-eventbus": minor
---

The bus throws an error if publish is called without initializing.
22 changes: 22 additions & 0 deletions packages/core/src/GraphQLEventbus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ test("Allow invalid topic publishing", async () => {
});
});

test("Publishing fails if bus is not initialized", async () => {
const publishCb = jest.fn();
const bus = new GraphQLEventbus({
publisher: {
schema: pubSchema,
publish: async (d) => {
publishCb(d);
},
allowInvalidTopic: true,
},
});
await expect(() =>
bus.publish({
topic: "TestEvent",
payload: {
id: "123",
name: "name",
},
}),
).rejects.toThrow();
});

test("valid events are consumed and hooks are called", async () => {
const consumeCb = jest.fn();
let cbRef!: DataCb;
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/GraphQLEventbus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ export class GraphQLEventbus {
payload: {};
metadata?: Partial<GraphQLEventbusMetadata>;
}) => {
if (!this.isInitialized) {
throw new Error("The eventbus must be initialized before publishing.");
}
if (!this.publishValidator) {
throw new Error("Publish config not added!");
}
Expand Down

0 comments on commit 81d354b

Please sign in to comment.