Skip to content

Cypress Overwrite Command Not Replacing Fix

DodaTech Updated 2026-06-24 2 min read

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 originalFn as 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

  1. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  2. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  3. Misunderstanding that String is [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

### What's the difference between overwrite and add?

<a href="/testing-qa/cypress/">Cypress</a>.Commands.add() creates a new command. <a href="/testing-qa/cypress/">Cypress</a>.Commands.overwrite() replaces an existing command (built-in or custom). Use overwrite to extend built-in commands with default options or logging.

Can I overwrite a custom command?

Yes. <a href="/testing-qa/cypress/">Cypress</a>.Commands.overwrite('myCustomCommand', ...) works on both built-in and custom commands. The original function is always the first argument.

How do I prevent infinite recursion in overwrites?

Always call originalFn(...args) not cy.originalName(...). The originalFn parameter points to the previous implementation, not the overwritten one.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro