{"id":24427430,"url":"https://github.com/unlight/awaitable-emit","last_synced_at":"2026-02-11T15:02:32.653Z","repository":{"id":270560738,"uuid":"910600917","full_name":"unlight/awaitable-emit","owner":"unlight","description":"Emit message to kafka and wait while nestjs process it","archived":false,"fork":false,"pushed_at":"2025-01-01T15:00:18.000Z","size":17,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-13T04:39:37.551Z","etag":null,"topics":["end-to-end-testing","kafka","microservices","nestjs","nestjs-testing","testing"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/unlight.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-31T19:13:15.000Z","updated_at":"2025-01-01T15:00:21.000Z","dependencies_parsed_at":"2025-02-19T12:05:19.226Z","dependency_job_id":"a9d2d190-4977-47fe-96bd-b5479b2d870d","html_url":"https://github.com/unlight/awaitable-emit","commit_stats":null,"previous_names":["unlight/awaitable-emit"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/unlight/awaitable-emit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unlight%2Fawaitable-emit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unlight%2Fawaitable-emit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unlight%2Fawaitable-emit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unlight%2Fawaitable-emit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/unlight","download_url":"https://codeload.github.com/unlight/awaitable-emit/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unlight%2Fawaitable-emit/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29336024,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-11T14:34:07.188Z","status":"ssl_error","status_checked_at":"2026-02-11T14:34:06.809Z","response_time":97,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["end-to-end-testing","kafka","microservices","nestjs","nestjs-testing","testing"],"created_at":"2025-01-20T12:10:37.747Z","updated_at":"2026-02-11T15:02:32.638Z","avatar_url":"https://github.com/unlight.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## awaitable-emit\n\nEmit message to kafka and wait while nestjs process it.\n\n### How it works\n\nCreate helper utils for end to end test:\n\n- `emitMessage` - function to emit message to kafka\n- `AwaitableEmitInterceptor` - nestjs global interceptor (required for emitMessage)\n- `dispose` - function to run on test tear down stage\n\n### Usage\n\n1. Create helper objects\n\n```ts\nconst { emitMessage, AwaitableEmitInterceptor, dispose } = createAwaitableEmit(options)`\n```\n\n2. Add interceptor to nestjs app\n\n```ts\napp.useGlobalInterceptors(new AwaitableEmitInterceptor());\n```\n\n3. Use `emitMessage` in test\n4. Run `dispose` in 'after all' stage\n\n### Options\n\n`getKafkaClient: () =\u003e ClientKafka` Factory function which returns kafka client instance  \n`wait?: number` Wait time in milliseconds (if controller cannot handle message in this time, promise will be resolved)  \n`brokers?: string[]` Kafka brokers, this is optional and uses kafka admin under the hood - waits when all consumners groups will have no lag\n(this may or may not be useful for parallel running tests)\n\n### Example\n\n```ts\n@Controller()\nexport class AppController {\n  constructor(private readonly appService: AppService) {}\n\n  @EventPattern('user-created')\n  async handleEntityCreated(@Payload() payload: object) {\n    console.log('user created', payload);\n    await setTimeout(2000); // Imitate long running process\n    this.appService.shared.push(payload);\n  }\n}\n```\n\n```ts\ndescribe('AppController (e2e)', () =\u003e {\n  let app: INestMicroservice;\n  let clientKafka: ClientKafka;\n  let service: AppService;\n\n  const { emitMessage, AwaitableEmitInterceptor, dispose } =\n    createAwaitableEmit({\n      getKafkaClient: () =\u003e clientKafka,\n    });\n\n  // Before all\n  before(async () =\u003e {\n    const providers: Provider[] = [\n      {\n        provide: 'KAFKA_CLIENT',\n        useFactory: () =\u003e {\n          return ClientProxyFactory.create({\n            transport: Transport.KAFKA,\n            options: {\n              client: {\n                clientId: 'KAFKA_CLIENT',\n                brokers: ['127.0.0.1:9092'],\n              },\n            },\n          });\n        },\n      },\n    ];\n    const testingModule = await Test.createTestingModule({\n      imports: [AppModule],\n      providers,\n    }).compile();\n\n    app = testingModule.createNestMicroservice({\n      transport: Transport.KAFKA,\n      options: {\n        client: {\n          clientId: 'KAFKA_CLIENT',\n          brokers: ['127.0.0.1:9092'],\n        },\n      },\n    });\n\n    app.useGlobalInterceptors(new AwaitableEmitInterceptor());\n\n    await app.init();\n    await app.listen();\n\n    clientKafka = app.get\u003cClientKafka\u003e('KAFKA_CLIENT');\n    service = app.get(AppService);\n  });\n\n  // After all\n  after(async () =\u003e {\n    await dispose();\n    await clientKafka?.close();\n    await app?.close();\n  });\n\n  it('smoke', () =\u003e {\n    expect(clientKafka).toBeTruthy();\n  });\n\n  it('test emit message', async () =\u003e {\n    await emitMessage('user-created', {\n      key: Date.now.toString(),\n      value: { name: 'Bob' },\n    });\n    expect(service.shared.at(-1)).toEqual({ name: 'Bob' });\n  });\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funlight%2Fawaitable-emit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Funlight%2Fawaitable-emit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funlight%2Fawaitable-emit/lists"}