npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

cypress-commands-kafka

v2.1.7

Published

package for cypress to produce or consume kafka events and more

Downloads

86

Readme

To get started :

npm install -D cypress-commands-kafka

Cypress configuration :

In your config file add :

const { CypressKafka } = require("cypress-commands-kafka")

Kafka and Schema registry configuration :

The first part is for Kafka, the second for the registry, you can send whatever is accepted by kafkaJs. Read the following documentation for more information : https://kafka.js.org/docs/configuration https://kafkajs.github.io/confluent-schema-registry/docs/usage

kafka configuration is required, schema registry is optional.

Example of a configuration with kafka and schema registry :

const cypressKafka = new CypressKafka(
	{
		clientId: process.env.KAFKA_CLIENT_ID,
		brokers: [process.env.KAFKA_BROKERS],
		ssl: true,
		sasl: {
			mechanism: "plain",
			username: process.env.KAFKA_SASL_USERNAME,
			password: process.env.KAFKA_SASL_PASSWORD,
		},
	},
	{
		host: process.env.KAFKA_SCHEMA_REGISTRY_HOST,
		auth: {
			username: process.env.KAFKA_SCHEMA_REGISTRY_USERNAME,
			password: process.env.KAFKA_SCHEMA_REGISTRY_PASSWORD,
		},
	}
);

The tasks :

async produceEvent(fileName) {
    return cypressKafka.produceEvent(fileName);
},
async consumeEvent(topics) {
    const groupId = "your-group-id"
    return cypressKafka.consumeEvent(topics, groupId);
},
async getConsumedEvent(topic) {
    return cypressKafka.getConsumedEvent(topic);
},
async cleanAndDisconnectConsumer() {
    return cypressKafka.cleanAndDisconnectConsumer();
}

In your index.d.ts add :

produceEvent(data: string): Chainable<void>;
consumeEvent(topic: Array<string>): Chainable<void>;
cleanAndDisconnectConsumer(): Chainable<void>;
getConsumedEvent(topic: string): Chainable<any[]>;

in your commands file add :

Cypress.Commands.add("produceEvent", (data: string) => {
	cy.task("produceEvent", data);
});
Cypress.Commands.add("consumeEvent", (topic: Array<string>) => {
	cy.task("consumeEvent", topic);
});
Cypress.Commands.add("getConsumedEvent", (topic: string) => {
	cy.task("getConsumedEvent", topic);
});
Cypress.Commands.add("cleanAndDisconnectConsumer", () => {
	cy.task("cleanAndDisconnectConsumer");
});

How to use :

To produce an event

To call it in your test :

cy.produceEvent(`cypress/fixtures/${Cypress.env('KAFKA_ENV')}/myFile.json`);

Here I even added an environment variable to be able to do my test on SIT or UAT. You'll need to give the path to a fixture.

The fixture will look like this :

[
    {
        "key": {
            "myKey": "myKeyValue",
            "myKey2": "myKeyValue"
        },
        "headers": {
            "event_type": "myHeader"
        },
        "payload": {
            "MyPayload1": "MyPayloadValue1",
            "MyPayload2": "MyPayloadValue2",
            "MyPayload3": "MyPayloadValue3",
        },
        "topic": "THE TOPIC TO SEND THE MESSAGE TO",
        "subject": "THE SCHEMA REGISTRY FOR THE PAYLOAD OF THE TOPIC",
        "subjectKey": "THE SCHEMA REGISTRY FOR THE KEY OF THE TOPIC"
    },
    {
        "key": {
            "myKey": "anotherExample"
        },
        "payload": {
            "MyPayload1": "anotherExample",
            "MyPayload2": "anotherExample",
            "MyPayload3": "anotherExample",
        },
        "topic": "anotherExample"
    }
]

You can send 1 message or more on 1 topic or on multiple topic at the same time. The only required things are the topic and the payload. In the first message in the example I send a message to a topic wich use avro on the key and the payload so I added the subject and the subject key. On the second message in the example, there is no avro used so there is no subject or subjectKey.

Always wait a bit ( cy.wait(3000) ? ) after the events are send to give some time for them to arrive and be consumed by your application

To consume an event

First don't forget to put const groupId = "your-group-id" with the right group id in your config file.

cy.consumeEvent(["Topic1", "Topic2"]);

Will listen to the topics given in the array, you can listen to 1 or more topic at the same time, the message are consume at the moment where the consumeEvent task is called. So lets say you wanna test if you get an event when you do something on your website, you'll need to start consuming BEFORE you do your test that will give you an event.

Every event consumed are stocked in a variable, so to test you'll need the next command

To check / test your consumed event

cy.getConsumedEvent("Mytopic")

The command to get the consumed event, sorted by topics so lets say you had 2 topics you could do :

cy.getConsumedEvent("myTopic1").then((messages) => {
    //A filter to get the right message by the key in this case
    const filteredMessages = messages.filter(message => message.key === "myKeyValue");
    filteredMessages.forEach(msg => {
        //The test to check the payload content and see if it was right
        expect(msg.payload.myId).to.equal("wow good job");
    });
});
cy.getConsumedEvent("myTopic2").then((messages) => {
    //A filter to get the right message by the id in the payload in this case
    const filteredMessages = messages.filter(message => message.payload.myId === "myIdValueInPayload");
    filteredMessages.forEach(msg => {
        //The test to check the key content and see if it was right
        expect(msg.key.myKeyName).to.equal("123465");
    });
});

With those 2 examples, I leave the rest to your imagination...

To disconnect the consumer after your test

In my case I just do this :

afterEach(() => {
    cy.cleanAndDisconnectConsumer();
});

It will disconnect the consumer and empty the variable that stocked the messages consumed, so I just do it after every test

Side notes :

This package is a work in progress so be mindfull some stuff might change.

Working on it :

  • Check if it works with tests that are running in parrallel
  • Rename my variables for more comprehension