For a test, I have a command, defined as a simple ...
# best-practices
a
For a test, I have a command, defined as a simple function at the top of my file, and I have a simple array of four role strings. Then I call it four times by using
roles.map( role => function(role) )
. In my command, there is a
Cypress.log()
block at the top, and it immediately prints this out for all four roles before it moves on to the actual test logic. I'm assuming this has something to do with hoisting and/or the async nature of Cypress. Is this something that could be resolved if I defined that function as a Cypress Custom Command, instead of just as a simple function defined as a
const
?
Also, how can custom commands be added directly into test files when using Typescript? I know how to add the types in the definitions file for when I put a custom command in
/support/
, but I'm asking about when I want to drop one into a test file directly. How can I declare its types?
g
can you provide more details? like what is the cy.log printing? where is it?
a
the Cypress.log (not cy.log) is printing what I expect. It's at the top of the function I've declared in my test body. What is unexpected to me is that it prints all four times before being called via
map
g
well, hard to say without seeing the spec code that is acting weird
a
Here's the spec code:
Copy code
it("C69484 Verify Settings Defaults: All Roles", () => {
    Object.keys(roles).map((role) => {
      verifySettings(role);
    });
  });
And here's the
verifySettings
custom command:
Copy code
const verifySettings = (role) => {
    Cypress.log({
      name: "verifySettings",
      displayName: "Verify Default Settings for the role:",
      message: `${role}`,
    });

    // Do stuff
  };
The logic of the custom command isn't relevant. My question is just about the log printing happening all four times right at the outset, instead of waiting to fire, in-between each
// Do stuff
chunk.
From what I understand of Cypress' async nature, the
.map()
is basically expanding the logic of the custom command out into a giant virtual text file and then because of Javascript Hoisting, the
Cypress.log()
sections are being boosted to the top and run immediately. I'm looking to confirm if my reasoning is correct on this, and if there's anything that can be done to have the
Cypress.log
sections print just before the logic the follows them.
g
I don't think this is correct. But I cannot say without seeing what
// Do stuff
means there. I would create a tiny public repo with the spec so other people can run and see it and explain it. This is also helpful in this case https://glebbahmutov.com/blog/visualize-cypress-command-queue