Challenge - 5 Problems
TypeScript Custom Commands Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a custom Cypress command with TypeScript
What will be the output in the Cypress test runner console after running this test code with the custom command?
Cypress
declare namespace Cypress {
interface Chainable {
login(email: string, password: string): Chainable<string>;
}
}
Cypress.Commands.add('login', (email, password) => {
cy.get('#email').type(email);
cy.get('#password').type(password);
cy.get('#submit').click();
return cy.url();
});
describe('Login Test', () => {
it('should navigate to dashboard after login', () => {
cy.login('user@example.com', 'pass123').then(url => {
cy.log(url);
});
});
});Attempts:
2 left
💡 Hint
Check what the custom command returns and how the .then() callback receives it.
✗ Incorrect
The custom command returns cy.url(), which yields the current URL string. The .then() callback receives this string and logs it. The TypeScript declaration ensures the command is recognized with correct parameters and return type.
❓ assertion
intermediate2:00remaining
Correct TypeScript assertion for a custom command result
Given a custom command that returns a user object, which assertion correctly checks the user's email property in TypeScript?
Cypress
declare namespace Cypress {
interface Chainable {
getUser(): Chainable<{ email: string; id: number }>;
}
}
Cypress.Commands.add('getUser', () => {
return cy.wrap({ email: 'test@domain.com', id: 42 });
});
cy.getUser().should( /* Which assertion? */ );Attempts:
2 left
💡 Hint
Use the correct Chai assertion syntax supported by Cypress.
✗ Incorrect
Option B uses the correct Chai assertion syntax with .to.have.property() and .equal() to check the email property value. Option B is close but .to.equal() is not used as a function inside should(). Option B uses Jest syntax (.toBe) which is invalid here. Option B uses .equals() without .to, which is incorrect.
🔧 Debug
advanced2:00remaining
Debugging TypeScript error in custom command declaration
Why does this TypeScript error occur when adding a custom command?
Error: Property 'fillForm' does not exist on type 'Chainable'.
Cypress
Cypress.Commands.add('fillForm', (name: string, age: number) => { cy.get('#name').type(name); cy.get('#age').type(age.toString()); }); // Usage cy.fillForm('Alice', 30); // TypeScript declaration missing
Attempts:
2 left
💡 Hint
Check if the TypeScript interface was extended to include the new command.
✗ Incorrect
TypeScript requires extending the Cypress.Chainable interface to declare new custom commands. Without this declaration, TypeScript does not recognize 'fillForm' as a valid method on cy.
🧠 Conceptual
advanced2:00remaining
Understanding TypeScript declaration merging for Cypress commands
Which statement best describes how TypeScript supports adding custom commands in Cypress?
Attempts:
2 left
💡 Hint
Think about how TypeScript merges declarations from multiple files.
✗ Incorrect
TypeScript supports declaration merging, so you extend existing interfaces by redeclaring them inside a declare namespace block. This way, your custom commands become recognized as part of Cypress.Chainable.
❓ framework
expert3:00remaining
TypeScript typing for custom command returning a typed promise
You want to create a custom Cypress command 'fetchUser' that returns a Promise resolving to a User object with properties 'id' (number) and 'name' (string). How should you declare it in TypeScript to ensure proper typing and chaining?
Attempts:
2 left
💡 Hint
Remember Cypress commands always return Chainable wrapping the yielded value, not a raw Promise.
✗ Incorrect
Cypress commands return Chainable wrapping the yielded value, not a raw Promise. Declaring fetchUser() as returning Chainable<{ id: number; name: string }> correctly types the command's yield. Option D wraps a Promise inside Chainable which is incorrect. Option D returns a raw Promise, breaking Cypress chaining. Option D loses the typed return value.