Full Typescript Cypress project.
I get the following error when trying to use the following custom command in this way:
Usage:
cy.restGetUserId(userEmail).then(userId => {
//do something with userId
});
Custom command:
Cypress.Commands.add('restGetUserId', (userEmail) => {
expect(userEmail).to.not.equal(undefined);
cy.request({
//some request, works!
}).then((response) => {
cy.wrap(response.body.id);
});
});
Currently documented:
/**
* also works as a normal user
*
* @example cy.restGetUserId('[email protected]');
* @param userEmail - get the userId from this email
* @returns the id of the given email/user.
*/
restGetUserId(userEmail: string): string;
My error is most likely in the documentation of this method, what is the recommended way to document this method? (especially the return value i would guess)
CodePudding user response:
If you want to continue to use the something that is used or created within your custom command, you'll have to return the chain of cypress commands that yields the item.
For your case you'll have to do the following:
Cypress.Commands.add('restGetUserId', (userEmail) => {
expect(userEmail).to.not.equal(undefined);
return cy.request({
//some request, works!
}).its('response.body.id') // get the id we need
});
Now you'll have access to the id returned by the request.
cy.restGetUserId(userEmail).then(cy.log) //logs the id from request
CodePudding user response:
A custom command may not return anything other than Chainable<T>
.
So a valid type definition for you example may look like this:
restGetUserId(userEmail: string): Chainable<string>;