Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise returned when using async...await from another function #55

Open
nara-golfdistrict-com opened this issue Feb 22, 2024 · 0 comments

Comments

@nara-golfdistrict-com
Copy link

I created a utility function to retrieve the count from an API. The value is properly returned in the utility function though the caller (the actual test) is getting a promise. Is there a way to get the actual value in the test? See below code samples.

const cyAwaitPreprocessor = require("cypress-await/src/preprocessor");
setupNodeEvents(on, config) {
      // implement node event listeners here
      on(
        "file:preprocessor",
        cyAwaitPreprocessor({
          debugOutput: true,
        })
      );
...

emailUtility.js

export async function getCount() {
  var url = "https://api.publicapis.org/entries";
  var count = -1;

  cy.log(url);

  var res = await cy.request({
    method: "GET",
    url: url,
  });
  // .its("body.count")
  // .should("be.a", "number");

  cy.log(`Response body count = ${res.body.count}`);
  count = res.body.count;
  cy.logcc(`Count variable = ${count}`);

  return count;
}
...

test.cy.js

import {
  getCount,
} from "../support/emailUtility";

describe("Testing Environment", () => {
  it("Get API Count", () => {
    cy.logcc("Get API Count");
    cy.logcc("Begin");
    var value = getCount();
    cy.logcc("End");

    cy.logcc(value);
  });
});

Below is the screenshot of the test run.

image

If I used async...await in the test then I get the below error.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant