1

I have used KafkaJS library and configured an consumer code to fetch messages from a topic of one of my streaming API.

I have created a consumer.js file and I'm able to run the file from the location in the commandline using node consumer.js

But my requirement is to execute the consumer.js file from my javascript/cypress code. I have put the complete consumer.js code inside a method under a Class and tried to call but it fails.

Is there a way to run the node consumer.js command directly from the code OR else how can I call it from my class.

Tried the consumer code similar to this https://thecodebarbarian.com/getting-started-with-apache-kafka-in-node-js.html

Any help would be highly appreciated.

Thanks

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

1 Answers1

1

cy.exec() allows for execution of system commands. Note: there are some weird things to consider with this, that aren't readily apparent and could cause unexplained behaviors.

cy.exec('node consumer.js')
  // may be worthwhile to assert that the file ran successfully
  .its('code')
  .should('equal', 0);
agoff
  • 5,818
  • 1
  • 7
  • 20
  • 0 Thanks for the reply. I tried your code but assert fails with 1, so doesn't look like it is running. I tried keeping the consumer.js file at the root directory and also at a different location and tried this.. cy.exec('node ./cypress/integration/kafka/consumer.js') but no luck. – Bijo Joseph May 08 '22 at 18:48
  • Without seeing your entire setup, I don't really have anymore advice to give. `cy.exec` is used to execute command line functions. I'd try making sure that the file is actually running, perhaps trying a few different functions to get your `pwd` and work backwards from there. – agoff May 08 '22 at 21:45
  • Why should this be needed, though? Can't the the consumer file be imported with require? – OneCricketeer May 09 '22 at 05:19
  • I have used "require" and ran the test but in Cypress App it gives error for kafka connection. In the Command line there is no error and other steps of the step definition works fine but not the consumer file. Used code: const cons = require('../kafka/consumer') let messages = toString(cons) cy.log(toString(messages)) – Bijo Joseph May 09 '22 at 08:21
  • This is my consumer.js file code which is not running and complaining for net.connect issue while using cy.exec: const Kafka = require("kafkajs").Kafka run().then(() => { console.log('Done'), err => console.log(err) }) async function run() { const kafka = new Kafka({ brokers: [‘’], sasl: { mechanism: 'plain', username: ‘’, password: ‘’ } }) – Bijo Joseph May 11 '22 at 08:46
  • await consumer.connect() console.log("Connected!") await consumer.subscribe({ topic: ‘<>Topic’, fromBeginning: true }) let startTime = Date.now() await consumer.run({ eachMessage: async (data) => { console.log(Date.now() - startTime, data.message.value.toString('utf8')) const messgaeValue = data.message.value.toString('utf8') console.log(messgaeValue) console.log('Captured') } }) } – Bijo Joseph May 11 '22 at 08:49