简体   繁体   中英

Error caused by unhandled promise rejection in cypress

i am getting this error and not sure how to solve it could you please have a look.

My code, i am fetching latest email based on subject using 3rd party service.

export class TestmailService {
  static async pollEmail(subject: string, tag: string): Promise<TestmailEmail | null> {
    const namespace = 'nv5zl';
    const apiKey = '7d4c581c-ca94-46c8-a016-230619876eff';

    const timestamp = `${+Date.now()}`;

    for (let i = 0; i <= 20; i++) {
      const { body } = await new Cypress.Promise<Cypress.Response<TestmailInbox>>((resolve) => {
        const url = new URL('https://api.testmail.app/api/json');
        url.searchParams.append('apikey', apiKey);
        url.searchParams.append('namespace', namespace);
        url.searchParams.append('tag', tag);
        url.searchParams.append('timestamp_from', timestamp);

        cy.request<TestmailInbox>('GET', `${url}`).then((response) => resolve(response));
      });

      if (body.emails.length) {
        const email = body.emails.find((e) => e.subject === subject);
        if (email) return email;
      }

      // eslint-disable-next-line cypress/no-unnecessary-waiting
      cy.wait(3000);
    }

    return null;
  }
}
  1. Reset password should not be able to log in with old password: CypressError: The following error originated from your test code, not from Cypress. It was caused by an unhandled promise rejection.

Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.

The command that returned the promise was:

cy.get()

The cy command you invoked inside the promise was:

cy.wait()

Because Cypress commands are already promise-like, you don't need to wrap them or return your own promise.

Cypress will resolve your command with whatever the final Cypress command yields.

The reason this is an error instead of a warning is because Cypress internally queues commands serially whereas Promises execute as soon as they are invoked. Attempting to reconcile this would prevent Cypress from ever resolving.

When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.

I don't use cypress, but looking at the code you provided it looks like it's already promise based, if so, don't create a Promise constructor. -> new Promise() , and keep everything using await .

So something like this should work. ->

export class TestmailService {
  static async pollEmail(subject: string, tag: string): Promise<TestmailEmail | null> {
    const namespace = 'nv5zl';
    const apiKey = '7d4c581c-ca94-46c8-a016-230619876eff';
    const timestamp = `${+Date.now()}`;
    const url = new URL('https://api.testmail.app/api/json');
    url.searchParams.append('apikey', apiKey);
    url.searchParams.append('namespace', namespace);
    url.searchParams.append('tag', tag);
    url.searchParams.append('timestamp_from', timestamp);

    for (let i = 0; i <= 20; i++) {
      const { body } = await cy.request<TestmailInbox>('GET', `${url}`);
      if (body.emails.length) {
        const email = body.emails.find((e) => e.subject === subject);
        if (email) return email;
      }
      // eslint-disable-next-line cypress/no-unnecessary-waiting
      await cy.wait(3000); //don't forget to await
    }

    return null;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM