Cypress Overwrite Command Not Replacing Fix
In this tutorial, you'll learn about Cypress Overwrite Command Not Replacing Fix. We cover key concepts, practical examples, and best practices.
Your <a href="/testing-qa/cypress/">Cypress</a>.Commands.overwrite() doesn't take effect — the original command still runs with its default behavior.
The Problem
// WRONG — overwrite defined but the overwrite function signature is wrong
Cypress.Commands.overwrite('visit', (url) => {
// Missing the original function parameter
cy.visit(url, { timeout: 30000 });
});
This causes infinite recursion because the overwrite calls itself instead of the original.
Step-by-Step Fix
1. Properly overwrite with original function
// RIGHT — cypress/support/commands.js
Cypress.Commands.overwrite('visit', (originalFn, url, options) => {
// Add default options
const mergedOptions = { timeout: 30000, ...options };
return originalFn(url, mergedOptions);
});
// Usage — now has 30s timeout by default
cy.visit('/dashboard');
2. Overwrite type command
// RIGHT — add logging to type
Cypress.Commands.overwrite('type', (originalFn, element, text, options) => {
console.log(`Typing: "${text}" into element`);
return originalFn(element, text, { ...options, delay: 50 });
});
cy.get('input').type('Hello'); // Logs and types with delay
3. Overwrite intercept for global error logging
// RIGHT — global intercept modifications
Cypress.Commands.overwrite('intercept', (originalFn, ...args) => {
return originalFn(...args).then((interception) => {
if (interception.response.statusCode >= 400) {
console.error('API error:', interception);
}
return interception;
});
});
4. Overwrite with conditional behavior
// RIGHT — conditional overwrite
Cypress.Commands.overwrite('request', (originalFn, options) => {
if (typeof options === 'string') {
options = { url: options };
}
options.headers = { 'X-Testing': 'true', ...options.headers };
return originalFn(options);
});
Expected output:
✓ visit has default timeout
✓ type command logs input
Prevention Tips
- Always include
originalFnas the first parameter in overwrite - Call
originalFn()to invoke the original command - Avoid overwriting commands in
beforeEach— do it once in support - Use overwrites for cross-cutting concerns (logging, defaults)
- Document overwritten commands for team awareness
Common Mistakes with custom command overwrite
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations
These mistakes appear frequently in real-world CYPRESS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro