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()
    });
  1. Run a producer that produces messages to a topic on time and then disconnect
  2. Run a consumer that subscribes to that topic and logs each topic offset
  3. 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)

Most upvoted comments

As a data point: I’m seeing this error message with different stack traces when running tests locally:

 FAIL  src/consumer/__tests__/subscribe.spec.js
  ● Consumer › with regex › subscribes to all matching topics

    TypeError: request is not a function



      at 2 (src/protocol/requests/apiVersions/index.js:28:16)
      at Broker.apiVersions (src/broker/index.js:163:52)
      at Broker.connect (src/broker/index.js:109:36)
      at src/cluster/brokerPool.js:317:9
      at BrokerPool.findBroker (src/cluster/brokerPool.js:242:5)
      at Cluster.findBroker (src/cluster/index.js:234:14)
      at src/consumer/consumerGroup.js:453:24