CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-nock

HTTP server mocking and expectations library for Node.js testing environments

67

0.98x
Overview
Eval results
Files

task.mdevals/scenario-6/

Network Access Guard

Utility for controlling outbound HTTP usage during tests by gating real network traffic and toggling interceptors.

Capabilities

Blocks non-allowlisted hosts

  • After calling block with allowlist ["allowed.test"], a GET request to http://blocked.test/resource is rejected while http://allowed.test/ping proceeds. @test

Allows scoped pass-through

  • Opening the gate with allowAll lets real requests through even after block was invoked, until blocking is re-applied. @test

Toggles interception hooks

  • Calling deactivate lets real requests bypass interceptors without clearing existing mock definitions; calling activate afterwards restores interception for the same mocks. @test

Reports active state

  • isActive reflects hook status before and after deactivate/activate calls. @test

Implementation

@generates

API

export interface AllowMatcher {
  (host: string): boolean;
}

export type AllowRule = string | RegExp | AllowMatcher;

export interface NetworkGuardOptions {
  allow?: AllowRule | AllowRule[];
}

export interface NetworkGuard {
  block(allow?: AllowRule | AllowRule[]): void;
  allowAll(): void;
  deactivate(): void;
  activate(): void;
  isActive(): boolean;
}

export function createNetworkGuard(options?: NetworkGuardOptions): NetworkGuard;

Dependencies { .dependencies }

nock { .dependency }

HTTP interception and network gating for Node.js tests.

Install with Tessl CLI

npx tessl i tessl/npm-nock

tile.json