- KafkaTestHelper
KafkaTestHelper is the class that helps you interacting with Apache Kafka
- createKafkaTestHelper(kafka, topic) ⇒
KafkaTestHelper
Creates and returns an instance of KafkaTestHelper
- ConsumedMessage :
Object
- ProducibleMessage :
Object
KafkaTestHelper is the class that helps you interacting with Apache Kafka
Kind: global class
See: You can construct a KafkaTestHelper instancevia createKafkaTestHelper method
Resets the helper to the current offset
Kind: instance method of KafkaTestHelper
Example
await helper.reset()
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()
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}',
},
...
]
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
})
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 |
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 |