Skip to content

Latest commit

 

History

History
207 lines (169 loc) · 5.94 KB

api.md

File metadata and controls

207 lines (169 loc) · 5.94 KB

Kafka Test Helper APIs

Classes

KafkaTestHelper

KafkaTestHelper is the class that helps you interacting with Apache Kafka

Functions

createKafkaTestHelper(kafka, topic)KafkaTestHelper

Creates and returns an instance of KafkaTestHelper

Typedefs

ConsumedMessage : Object
ProducibleMessage : Object

Specs

KafkaTestHelper

KafkaTestHelper is the class that helps you interacting with Apache Kafka

Kind: global class
See: You can construct a KafkaTestHelper instancevia createKafkaTestHelper method

kafkaTestHelper.reset()

Resets the helper to the current offset

Kind: instance method of KafkaTestHelper
Example

await helper.reset()

kafkaTestHelper.ensureTopicExists([timeout])

Creates a topic if doesn't exist

Kind: instance method of KafkaTestHelper

Param Type Default Description
[timeout] number 5000 Timeout in ms

Example

await helper.ensureTopicExists()

kafkaTestHelper.ensureTopicDeleted([timeout])

Deletes a topic if exists

Kind: instance method of KafkaTestHelper

Param Type Default Description
[timeout] number 5000 Timeout in ms

Example

await helper.ensureTopicDeleted()

kafkaTestHelper.messages() ⇒ Array.<ConsumedMessage>

Returns a list of messages published to the topic from last helper reset

Kind: instance method of KafkaTestHelper
Example

const msgs = await helper.messages()
[
 {
    headers: {}
    partition: 0,
    buffer: <Buffer 7b 22 62 61 72 22 3a 34 32 7d>,
    json: { "bar": 42 },
    string: '{"bar":42}',
 },
 ...
]

kafkaTestHelper.publishMessages(messages)

Publishes a list of messages to the topic

Kind: instance method of KafkaTestHelper

Param Type Description
messages Array.<ProducibleMessage> List of messages to publish

Example

await helper.publishMessages([
 {
   partition: 0,
   key: 'key1',
   string: "hello world",
 },
...
])

Example

await helper.publishMessages([
 {
   partition: 0,
   key: 'key1',
   json: { "foo": "bar" },
 },
...
])

Example

await helper.publishMessages([
 {
   partition: 0,
   key: 'key1',
   buffer: Buffer.from('hello world')
 },
...
])

createKafkaTestHelper(kafka, topic) ⇒ KafkaTestHelper

Creates and returns an instance of KafkaTestHelper

Kind: global function

Param Type Description
kafka Kafka KafkaJS instance. See https://kafka.js.org/docs/configuration
topic string Topic that the helper is going to monitor

Example

import { createKafkaTestHelper } from 'kafka-test-helper'

test('your test', async () => {
 const kafka = getKafka() // see https://kafka.js.org/docs/configuration
 const topicPrefix = Date.now() //  this avoids cross test interference
 const topicHelper = await createKafkaTestHelper(kafka, topicPrefix + 'test-topic')
 //do your tests here
})

ConsumedMessage : Object

Kind: global typedef
Properties

Name Type Description
headers Object Object with headers
partition number Partition number
buffer Buffer Buffer with message
json Object Object with message
string string String with message

ProducibleMessage : Object

Kind: global typedef
Properties

Name Type Description
partition number Partition number
key string Message key
buffer Buffer Message value as Buffer
json Object Message value as object and serialized with JSON.stringify()
string string Message value as string