DWilches
DWilches

Reputation: 23015

How to write a custom command in Cypress?

I'm trying to implement a hover command in Cypress, so I'm doing this:

Cypress.Commands.add("hover", { prevSubject: "element" }, (subject) => {
    return subject.trigger("mouseenter");
});

And using it like this:

cy.get(`[test-id="my-test-id"]`).hover();

But it's not working: the hover call doesn't seem to have any effect on the execution of the test.

If I remove the custom command and just put the contents of that function inline, it works:

cy.get(`[test-id="my-test-id"]`).trigger("mouseenter");

What am I doing wrong with the custom command?

Upvotes: 0

Views: 543

Answers (1)

MetaWhirledPeas
MetaWhirledPeas

Reputation: 486

Have you tried wrapping the subject first?

cy.wrap(subject).trigger("mouseenter");

Upvotes: 3

Related Questions