Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Getgauge Taiko ClearIntercept

From Leeroopedia

Template:Implementation

Overview

ClearIntercept is the Taiko API function for removing previously registered request interceptors, restoring normal network behavior for cleared URL patterns.

Description

The clearIntercept() function provides cleanup operations for the interception system. It supports two modes of operation:

  • Targeted removal — When called with a URL pattern argument, it removes only the interceptor registered for that specific pattern. Other interceptors remain active.
  • Bulk reset — When called with no arguments, it removes all registered interceptors at once. This is the recommended approach for test teardown.

After clearing, requests that previously matched the removed interceptor(s) will proceed normally to the network without any interception.

Usage

clearIntercept() is primarily used in test teardown hooks to ensure that interceptors from one test do not leak into subsequent tests. It should be called in afterEach, afterScenario, or equivalent teardown blocks.

Code Reference

Source Location

  • Public API: lib/taiko.js:L2824-2836
  • Targeted removal: lib/handlers/fetchHandler.js:L256-262 (resetInterceptor)
  • Bulk reset: lib/handlers/fetchHandler.js:L264-267 (resetInterceptors)

The relevant code paths:

// Public API in taiko.js
module.exports.clearIntercept = (requestUrl) => {
    if (requestUrl) {
        fetchHandler.resetInterceptor(requestUrl);
    } else {
        fetchHandler.resetInterceptors();
    }
};

// resetInterceptor - removes a specific interceptor
function resetInterceptor(requestUrl) {
    interceptors.delete(requestUrl);
}

// resetInterceptors - removes all interceptors
function resetInterceptors() {
    interceptors.clear();
}

Signature

clearIntercept(requestUrl)

Import

const { clearIntercept } = require('taiko');

I/O Contract

Inputs

Parameter Type Required Description
requestUrl string No URL pattern of the interceptor to remove. If omitted, all interceptors are removed.

Outputs

Return Type Description
void The function returns immediately. Interceptor removal is synchronous.

Side Effect: The specified interceptor (or all interceptors) are removed from the internal registry. Subsequent requests to the cleared URL patterns will proceed normally to the network.

Usage Examples

Clear a specific interceptor:

const { openBrowser, goto, intercept, clearIntercept, closeBrowser } = require('taiko');

(async () => {
    await openBrowser();

    // Register mock interceptor
    await intercept('https://api.example.com/data', {
        status: 200,
        body: '{"mocked": true}'
    });
    await goto('https://example.com');
    // Test with mocked data...

    // Clear specific interceptor
    clearIntercept('https://api.example.com/data');

    // Now requests to /data will reach the real server
    await goto('https://example.com');
    await closeBrowser();
})();

Clear all interceptors (recommended for teardown):

const { intercept, clearIntercept } = require('taiko');

// In test setup
await intercept('https://api.example.com/users', { status: 200, body: '[]' });
await intercept('https://api.example.com/products', { status: 200, body: '[]' });
await intercept('https://analytics.example.com/track');

// ... run test ...

// In test teardown - clear everything
clearIntercept();

Use in Gauge step implementation with teardown:

const { intercept, clearIntercept, goto } = require('taiko');

// Setup step
step('Mock API with empty data', async () => {
    await intercept('https://api.example.com/items', {
        status: 200,
        contentType: 'application/json',
        body: { items: [] }
    });
});

// Test step
step('Navigate to items page', async () => {
    await goto('https://example.com/items');
});

// Teardown - runs after each scenario
afterScenario(async () => {
    clearIntercept();
});

Selective cleanup mid-test:

// Phase 1: Mock both APIs
await intercept('https://api.example.com/auth', {
    status: 200,
    body: '{"token": "fake-token"}'
});
await intercept('https://api.example.com/data', {
    status: 200,
    body: '{"items": []}'
});

// Test with both mocked...

// Phase 2: Clear only the data mock, keep auth mock
clearIntercept('https://api.example.com/data');

// Now auth is still mocked but data hits the real server

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment