Skip to content

Instantly share code, notes, and snippets.

@ivcosla
Last active June 16, 2023 07:15
Show Gist options
  • Save ivcosla/908e717af746801787cced1f80c1c327 to your computer and use it in GitHub Desktop.
Save ivcosla/908e717af746801787cced1f80c1c327 to your computer and use it in GitHub Desktop.
double spending in nestjs
import { Module } from "@nestjs/common";
import { NestFactory } from "@nestjs/core";
import { CqrsModule, EventBus, EventsHandler } from "@nestjs/cqrs";
class Event {}
@EventsHandler(Event)
class EvntHandler {
async handle(event: Event) {
console.log("Event handled");
}
}
@Module({
imports: [CqrsModule],
providers: [EvntHandler],
})
class Base {}
describe("double", () => {
it("should be true", async () => {
// if we create a microservice, the event is delivered twice
const app = await NestFactory.createMicroservice(Base);
// where if we create a regular app, the event is delivered once
// const app = await NestFactory.create(Base);
await app.init();
const eventBus = await app.resolve(EventBus);
eventBus.publish(new Event());
// sleep for 0.5 seconds to see if the event is handled
await new Promise((resolve) => setTimeout(resolve, 500));
expect(true).toBe(true);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment