-
I try to figure out how I add assertions in my test to assert that an event was emitted.
|
Beta Was this translation helpful? Give feedback.
Answered by
a-kouyate
Jan 18, 2025
Replies: 1 comment
-
I found the response. For those who are interested to assert an event is emitted, you can process as follows: import { medusaIntegrationTestRunner } from "@medusajs/test-utils";
import EventBusService from "@medusajs/test-utils/dist/mock-event-bus-service";
import { asValue } from "awilix";
import { Modules } from "@medusajs/framework/utils";
medusaIntegrationTestRunner({
moduleName: "example-module",
testSuite: ({ getContainer }) => {
let fakeEventBus: jest.Mocked<EventBusService>;
beforeAll(() => {
const container = getContainer();
// Mock EventBusService and spy on the `emit` method
fakeEventBus = new EventBusService() as jest.Mocked<EventBusService>;
jest.spyOn(fakeEventBus, "emit").mockImplementation(() => Promise.resolve());
// Register the fake EventBus in the container
container.register(Modules.EVENT_BUS, asValue(fakeEventBus));
});
it("should emits an event when workflow completes", async () => {
const container = getContainer();
// Example workflow input
const exampleInput = { entityId: "example-id", value: "test-value" };
const { result } = await exampleWorkflow(container).run({
exampleInput,
});
// Validate workflow result
expect(result.status).toEqual("success");
// Assert that the event was emitted
expect(fakeEventBus.emit).toHaveBeenCalledTimes(1);
// Assert the event type and payload
const [eventType] = fakeEventBus.emit.mock.calls[0];
expect(eventType[0].name).toEqual("example.event.completed");
expect(eventType[0].data).toEqual({ entityId: "example-id", status: "completed" });
});
},
});
jest.setTimeout(60 * 1000); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
a-kouyate
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found the response. For those who are interested to assert an event is emitted, you can process as follows: