简体   繁体   中英

TypeScript and Cypress can't return true/false in custom cypress command

 Cypress.Commands.add('checkIfTenantExist', (tenantName: string) => { let result: boolean = false; getTenant().each(($tenant) => { if ($tenant.text() === tenantName){ result = true; } }).then(() => { return result; }); });

I write tests using cypress and typescript. I would like to add custom cypress command which return bool. It always return false even if it was changed in if block. Could you help me with that?

It works fine, presuming getTenant() returns one or more elements (which it should considering you follow it with .each() ).

But the way you use the custom command may be incorrect.

It should be used with a .then()

cy.checkIfTenantExist('something').then(exists => {
  // further tests here
}) 

or with a .should()

cy.checkIfTenantExist('something').should('eq', true) 

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