Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

awaitable-emit

Package Overview
Dependencies
Maintainers
0
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

awaitable-emit

Emit message to kafka and wait while nestjs process it.

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
61
Maintainers
0
Weekly downloads
 
Created
Source

awaitable-emit

Emit message to kafka and wait while nestjs process it.

How it works

Create helper utils for end to end test:

  • emitMessage - function to emit message to kafka
  • AwaitableEmitInterceptor - nestjs global interceptor (required for emitMessage)
  • dispose - function to run on test tear down stage

Usage

  1. Create helper objects
const { emitMessage, AwaitableEmitInterceptor, dispose } = createAwaitableEmit(options)`
  1. Add interceptor to nestjs app
app.useGlobalInterceptors(new AwaitableEmitInterceptor());
  1. Use emitMessage in test
  2. Run dispose in 'after all' stage

Options

getKafkaClient: () => ClientKafka Factory function which returns kafka client instance
wait?: number Wait time in milliseconds (if controller cannot handle message in this time, promise will be resolved)
brokers?: string[] Kafka brokers, this is optional and uses kafka admin under the hood - waits when all consumners groups will have no lag (this may or may not be useful for parallel running tests)

Example

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @EventPattern('user-created')
  async handleEntityCreated(@Payload() payload: object) {
    console.log('user created', payload);
    await setTimeout(2000); // Imitate long running process
    this.appService.shared.push(payload);
  }
}
describe('AppController (e2e)', () => {
  let app: INestMicroservice;
  let clientKafka: ClientKafka;
  let service: AppService;

  const { emitMessage, AwaitableEmitInterceptor, dispose } =
    createAwaitableEmit({
      getKafkaClient: () => clientKafka,
    });

  // Before all
  before(async () => {
    const providers: Provider[] = [
      {
        provide: 'KAFKA_CLIENT',
        useFactory: () => {
          return ClientProxyFactory.create({
            transport: Transport.KAFKA,
            options: {
              client: {
                clientId: 'KAFKA_CLIENT',
                brokers: ['127.0.0.1:9092'],
              },
            },
          });
        },
      },
    ];
    const testingModule = await Test.createTestingModule({
      imports: [AppModule],
      providers,
    }).compile();

    app = testingModule.createNestMicroservice({
      transport: Transport.KAFKA,
      options: {
        client: {
          clientId: 'KAFKA_CLIENT',
          brokers: ['127.0.0.1:9092'],
        },
      },
    });

    app.useGlobalInterceptors(new AwaitableEmitInterceptor());

    await app.init();
    await app.listen();

    clientKafka = app.get<ClientKafka>('KAFKA_CLIENT');
    service = app.get(AppService);
  });

  // After all
  after(async () => {
    await dispose();
    await clientKafka?.close();
    await app?.close();
  });

  it('smoke', () => {
    expect(clientKafka).toBeTruthy();
  });

  it('test emit message', async () => {
    await emitMessage('user-created', {
      key: Date.now.toString(),
      value: { name: 'Bob' },
    });
    expect(service.shared.at(-1)).toEqual({ name: 'Bob' });
  });
});

Keywords

FAQs

Package last updated on 01 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc