0
0
Cypresstesting~20 mins

TypeScript support for custom commands in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TypeScript Custom Commands Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
    });
  });
});
ALogs the URL string of the page after clicking submit, e.g., 'https://app.example.com/dashboard'
BLogs undefined because the custom command does not return a value
CThrows a TypeScript error due to missing return type in the custom command
DLogs the Cypress chainable object instead of the URL string
Attempts:
2 left
💡 Hint
Check what the custom command returns and how the .then() callback receives it.
assertion
intermediate
2: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? */ );
Auser => expect(user.email).toBe('test@domain.com')
Buser => expect(user).to.have.property('email', 'test@domain.com')
Cuser => expect(user.email).to.equal('test@domain.com')
Duser => expect(user['email']).equals('test@domain.com')
Attempts:
2 left
💡 Hint
Use the correct Chai assertion syntax supported by Cypress.
🔧 Debug
advanced
2: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
ABecause the custom command 'fillForm' was added but not declared in Cypress.Chainable interface
BBecause the command implementation has a syntax error in the arrow function
CBecause the parameters types are incorrect and cause a type mismatch
DBecause Cypress.Commands.add cannot add commands with multiple parameters
Attempts:
2 left
💡 Hint
Check if the TypeScript interface was extended to include the new command.
🧠 Conceptual
advanced
2:00remaining
Understanding TypeScript declaration merging for Cypress commands
Which statement best describes how TypeScript supports adding custom commands in Cypress?
AYou create a new interface with the same name as Cypress.Chainable and export it to override the original.
BYou add JSDoc comments above the command implementation to inform TypeScript about the new command.
CYou extend the Cypress.Chainable interface inside a declare namespace block to add method signatures for your commands.
DYou must modify the Cypress source code to add new commands to the Chainable interface.
Attempts:
2 left
💡 Hint
Think about how TypeScript merges declarations from multiple files.
framework
expert
3: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?
A
declare namespace Cypress {
  interface Chainable {
    fetchUser(): Chainable&lt;Promise&lt;{ id: number; name: string }&gt;&gt;;
  }
}
B
declare namespace Cypress {
  interface Chainable {
    fetchUser(): Promise&lt;{ id: number; name: string }&gt;;
  }
}
C
declare namespace Cypress {
  interface Chainable {
    fetchUser(): Chainable&lt;void&gt;;
  }
}
D
declare namespace Cypress {
  interface Chainable {
    fetchUser(): Chainable&lt;{ id: number; name: string }&gt;;
  }
}
Attempts:
2 left
💡 Hint
Remember Cypress commands always return Chainable wrapping the yielded value, not a raw Promise.