kafkajs: Question: TypeError: request is not a function
Describe the bug
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down.
at 5 (node_modules/kafkajs/src/protocol/requests/metadata/index.js:28:21)
at Broker.metadata (node_modules/kafkajs/src/broker/index.js:190:7)
at node_modules/kafkajs/src/cluster/brokerPool.js:127:38
at retry (node_modules/kafkajs/src/retry/index.js:43:5)
at node_modules/kafkajs/src/retry/index.js:61:5
and eachMessage()is not able to consume messages from Kafka topic
To Reproduce
Consumer code:
test('test consumer consumes correct data from producer', async () => {
const restartOnFailure = jest.fn(async () => false)
const kafka = new Kafka({
clientId: 'example-consumer',
logLevel: logLevel.ERROR,
brokers: ['localhost:9092'],
})
const consumer = kafka.consumer({
groupId: 'test-group',
retry: {
retries: 0,
initialRetryTime: 1,
restartOnFailure,
},
});
const run = async () => {
await consumer.connect()
await consumer.subscribe({ topic: 'test-topic', fromBeginning: true })
await consumer.run({
eachMessage: async ({ topic, partition, message }) => {
const decodedKey = await registry.decode(message.key)
const decodedValue = await registry.decode(message.value)
console.log({ decodedKey, decodedValue })
},
})
}
await run().catch(console.error)
});
afterAll(async () => {
await consumer.disconnect()
});
- Run a producer that produces messages to a topic on time and then disconnect
- Run a consumer that subscribes to that topic and logs each topic offset
- Error occurs and no message or topic are logged
Expected behavior Being able to consume messages and verify correctness in an integration test
Observed behavior Error described above
Environment:
- OS: [Mac OS 10.15.5]
- KafkaJS version [1.13.0-beta.38]
- NodeJS version [v14.3.0]
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 1
- Comments: 22 (5 by maintainers)
As a data point: I’m seeing this error message with different stack traces when running tests locally: