2

Technologies : NodeJs, Kafka, Mocha

The issue : I have a nodeJS service which given a String returns events with a many data.

How can I test this using Mocha ?

In a simple scenario the service returns one event with some data attached. In a more complex scenario the service publishes multiple events.

Is there a way I can test the services output at unit test level?

Possible solutions : A solution I can think of is to create a consumer inside the mocha test. Is there another practice/method I can follow ? Should I test this at a different level aka integration level ?

  • It is unclear what you are testing, is it function getEvents(inputstring), or are you testing the API method itself from the web service? Also, if the function you are testing has dependencies it would be an integration test. Unit tests are typically testing code in isolation (zero dependencies) – Jon Raynor Oct 9 at 17:26
  • @JonRaynor I think the problem is that the output event is on the queue and you cannot just inspect a third party queue to look if your event is there: you need to subscribe to it and hope to be notified. Once notified you also need to relate the received notification to the unit test to check if it matches the expected results. – Christophe Oct 9 at 17:53
  • 1
    If that is the case, it sounds like this is an integration test – Jon Raynor Oct 9 at 17:58
4

I haven't done extensive unit testing in java script. But the principles remain fairly same in all the languages.

Properties of a good unit test would be

  • Runs quickly and provides nearly immediate feedback
  • Doesn't have side effects, like charging a credit card, or modifying a database or event publishing an event

What needs to be tested in your case

  • Is it publishing the event in the needed scenario
  • Is it publishing the event with the correct values

Suggested design

  • Design a "Event Publisher" object that is publishes the given event (string) to the broker
  • Design the service to use the "Event Publisher"
  • Inject the event publisher in the service when creating the service
  • Create a "Mock Event Publisher" that is having the same method as "Event Publisher". But instead of publishing the message, it records the message published using it. Add a method to verify the messages published in the "Mock Event Publisher".
  • While testing inject the mock event publisher in to the service instead of event publisher, and verify if the correct message is published at the correct time.

Integration Tests

I wouldn't test the actual insertion of the event to the kafka broker in unit test. I would test this in integration test.

  • A very good answer! Thanks. It seems I already have the "Event publisher" component. And this indeed sounds like an integration test. Thanks for clarifying. I wonder if you have an out of the box solution for an integration test on this context ? (Maybe I should write another question on this..). – Cap Baracudas Oct 10 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.