nock

  • Version 14.0.4
  • Published
  • 184 kB
  • 3 dependencies
  • MIT license

Install

npm i nock
yarn add nock
pnpm add nock

Overview

HTTP server mocking and expectations library for Node.js

Index

Variables

variable back

let back: Back;

    variable emitter

    let emitter: NodeJS.EventEmitter;

      variable recorder

      let recorder: Recorder;

        Functions

        function abortPendingRequests

        abortPendingRequests: () => void;

          function activate

          activate: () => void;

            function activeMocks

            activeMocks: () => string[];

              function cleanAll

              cleanAll: () => void;

                function define

                define: (defs: Definition[]) => Scope[];

                  function disableNetConnect

                  disableNetConnect: () => void;

                    function enableNetConnect

                    enableNetConnect: (
                    matcher?: string | RegExp | ((host: string) => boolean)
                    ) => void;

                      function isActive

                      isActive: () => boolean;

                        function isDone

                        isDone: () => boolean;

                          function load

                          load: (path: string) => Scope[];

                            function loadDefs

                            loadDefs: (path: string) => Definition[];

                              function nock

                              nock: typeof nock;

                                function pendingMocks

                                pendingMocks: () => string[];

                                  function removeInterceptor

                                  removeInterceptor: (interceptor: Interceptor | ReqOptions) => boolean;

                                    function restore

                                    restore: () => void;

                                      Interfaces

                                      interface Back

                                      interface Back {}

                                        property currentMode

                                        currentMode: BackMode;

                                          property fixtures

                                          fixtures: string;

                                            method setMode

                                            setMode: (mode: BackMode) => void;

                                              call signature

                                              (fixtureName: string, nockedFn: (nockDone: () => void) => void): void;

                                                call signature

                                                (
                                                fixtureName: string,
                                                options: BackOptions,
                                                nockedFn: (nockDone: () => void) => void
                                                ): void;

                                                  call signature

                                                  (fixtureName: string, options?: BackOptions): Promise<{
                                                  nockDone: () => void;
                                                  context: BackContext;
                                                  }>;

                                                    interface BackContext

                                                    interface BackContext {}

                                                      property isLoaded

                                                      isLoaded: boolean;

                                                        property query

                                                        query: InterceptorSurface[];

                                                          property scopes

                                                          scopes: Scope[];

                                                            method assertScopesFinished

                                                            assertScopesFinished: () => void;

                                                              interface BackOptions

                                                              interface BackOptions {}

                                                                property after

                                                                after?: (scope: Scope) => void;

                                                                  property afterRecord

                                                                  afterRecord?: (defs: Definition[]) => Definition[] | string;

                                                                    property before

                                                                    before?: (def: Definition) => void;

                                                                      property recorder

                                                                      recorder?: RecorderOptions;

                                                                        interface DataMatcherArray

                                                                        interface DataMatcherArray extends ReadonlyArray<DataMatcher> {}

                                                                          interface DataMatcherMap

                                                                          interface DataMatcherMap {}

                                                                            index signature

                                                                            [key: string]: DataMatcher;

                                                                              interface Definition

                                                                              interface Definition {}

                                                                                property body

                                                                                body?: RequestBodyMatcher;

                                                                                  property headers

                                                                                  headers?: ReplyHeaders;

                                                                                    property method

                                                                                    method?: string;

                                                                                      property options

                                                                                      options?: Options;

                                                                                        property path

                                                                                        path: string | RegExp;

                                                                                          property port

                                                                                          port?: number | string;

                                                                                            property reqheaders

                                                                                            reqheaders?: Record<string, RequestHeaderMatcher>;

                                                                                              property response

                                                                                              response?: ReplyBody;

                                                                                                property scope

                                                                                                scope: string | RegExp;

                                                                                                  property status

                                                                                                  status?: number;

                                                                                                    interface Interceptor

                                                                                                    interface Interceptor {}

                                                                                                      method basicAuth

                                                                                                      basicAuth: (options: { user: string; pass?: string }) => this;

                                                                                                        method delay

                                                                                                        delay: {
                                                                                                        (opts: number): this;
                                                                                                        (opts: { head?: number; body?: number }): this;
                                                                                                        (opts: number | { head?: number; body?: number }): this;
                                                                                                        };
                                                                                                        • Deprecated

                                                                                                          use delay(number) instead

                                                                                                        method delayBody

                                                                                                        delayBody: (timeMs: number) => this;
                                                                                                        • Deprecated

                                                                                                          use delay function instead

                                                                                                        method delayConnection

                                                                                                        delayConnection: (timeMs: number) => this;
                                                                                                        • Deprecated

                                                                                                          use delay function instead

                                                                                                        method matchHeader

                                                                                                        matchHeader: (name: string, value: RequestHeaderMatcher) => this;

                                                                                                          method once

                                                                                                          once: () => this;

                                                                                                            method optionally

                                                                                                            optionally: (flag?: boolean) => this;

                                                                                                              method query

                                                                                                              query: (matcher: any) => this;

                                                                                                                method reply

                                                                                                                reply: {
                                                                                                                (
                                                                                                                replyFnWithCallback: (
                                                                                                                this: ReplyFnContext,
                                                                                                                uri: string,
                                                                                                                body: Body,
                                                                                                                callback: (
                                                                                                                err: NodeJS.ErrnoException | null,
                                                                                                                result: ReplyFnResult
                                                                                                                ) => void
                                                                                                                ) => void
                                                                                                                ): Scope;
                                                                                                                (
                                                                                                                replyFn: (
                                                                                                                this: ReplyFnContext,
                                                                                                                uri: string,
                                                                                                                body: Body
                                                                                                                ) => ReplyFnResult | Promise<ReplyFnResult>
                                                                                                                ): Scope;
                                                                                                                (
                                                                                                                statusCode: number,
                                                                                                                replyBodyFnWithCallback: (
                                                                                                                this: ReplyFnContext,
                                                                                                                uri: string,
                                                                                                                body: Body,
                                                                                                                callback: (err: any, result: any) => void
                                                                                                                ) => void,
                                                                                                                headers?: ReplyHeaders
                                                                                                                ): Scope;
                                                                                                                (
                                                                                                                statusCode: number,
                                                                                                                replyBodyFn: (this: ReplyFnContext, uri: string, body: Body) => any,
                                                                                                                headers?: ReplyHeaders
                                                                                                                ): Scope;
                                                                                                                (responseCode?: number, body?: Body, headers?: ReplyHeaders): Scope;
                                                                                                                };

                                                                                                                  method replyWithError

                                                                                                                  replyWithError: (errorMessage: string | object) => Scope;

                                                                                                                    method replyWithFile

                                                                                                                    replyWithFile: (
                                                                                                                    statusCode: StatusCode,
                                                                                                                    fileName: string,
                                                                                                                    headers?: ReplyHeaders
                                                                                                                    ) => Scope;

                                                                                                                      method thrice

                                                                                                                      thrice: () => this;

                                                                                                                        method times

                                                                                                                        times: (newCounter: number) => this;

                                                                                                                          method twice

                                                                                                                          twice: () => this;

                                                                                                                            interface InterceptorSurface

                                                                                                                            interface InterceptorSurface {}

                                                                                                                              property basePath

                                                                                                                              basePath: string;

                                                                                                                                property body

                                                                                                                                body: string;

                                                                                                                                  property counter

                                                                                                                                  counter: number;

                                                                                                                                    property method

                                                                                                                                    method: string;

                                                                                                                                      property optional

                                                                                                                                      optional: boolean;

                                                                                                                                        property path

                                                                                                                                        path: string;

                                                                                                                                          property queries

                                                                                                                                          queries?: string;

                                                                                                                                            property statusCode

                                                                                                                                            statusCode: number;

                                                                                                                                              property uri

                                                                                                                                              uri: string;

                                                                                                                                                interface Options

                                                                                                                                                interface Options {}

                                                                                                                                                  property allowUnmocked

                                                                                                                                                  allowUnmocked?: boolean;

                                                                                                                                                    property badheaders

                                                                                                                                                    badheaders?: string[];

                                                                                                                                                      property encodedQueryParams

                                                                                                                                                      encodedQueryParams?: boolean;

                                                                                                                                                        property filteringScope

                                                                                                                                                        filteringScope?: { (scope: string): boolean };

                                                                                                                                                          property reqheaders

                                                                                                                                                          reqheaders?: Record<string, RequestHeaderMatcher>;

                                                                                                                                                            interface Recorder

                                                                                                                                                            interface Recorder {}

                                                                                                                                                              method clear

                                                                                                                                                              clear: () => void;

                                                                                                                                                                method play

                                                                                                                                                                play: () => string[] | Definition[];

                                                                                                                                                                  method rec

                                                                                                                                                                  rec: (options?: boolean | RecorderOptions) => void;

                                                                                                                                                                    interface RecorderOptions

                                                                                                                                                                    interface RecorderOptions {}

                                                                                                                                                                      property dont_print

                                                                                                                                                                      dont_print?: boolean;

                                                                                                                                                                        property enable_reqheaders_recording

                                                                                                                                                                        enable_reqheaders_recording?: boolean;

                                                                                                                                                                          property logging

                                                                                                                                                                          logging?: (content: string) => void;

                                                                                                                                                                            property output_objects

                                                                                                                                                                            output_objects?: boolean;

                                                                                                                                                                              property use_separator

                                                                                                                                                                              use_separator?: boolean;

                                                                                                                                                                                interface ReplyFnContext

                                                                                                                                                                                interface ReplyFnContext extends Interceptor {}

                                                                                                                                                                                  property req

                                                                                                                                                                                  req: ClientRequest & {
                                                                                                                                                                                  headers: Record<string, string>;
                                                                                                                                                                                  };

                                                                                                                                                                                    interface Scope

                                                                                                                                                                                    interface Scope extends NodeJS.EventEmitter {}

                                                                                                                                                                                      property delete

                                                                                                                                                                                      delete: InterceptFunction;

                                                                                                                                                                                        property get

                                                                                                                                                                                        get: InterceptFunction;

                                                                                                                                                                                          property head

                                                                                                                                                                                          head: InterceptFunction;

                                                                                                                                                                                            property intercept

                                                                                                                                                                                            intercept: (
                                                                                                                                                                                            uri: string | RegExp | { (uri: string): boolean },
                                                                                                                                                                                            method: string,
                                                                                                                                                                                            requestBody?: RequestBodyMatcher,
                                                                                                                                                                                            options?: Options
                                                                                                                                                                                            ) => Interceptor;

                                                                                                                                                                                              property merge

                                                                                                                                                                                              merge: InterceptFunction;

                                                                                                                                                                                                property options

                                                                                                                                                                                                options: InterceptFunction;

                                                                                                                                                                                                  property patch

                                                                                                                                                                                                  patch: InterceptFunction;

                                                                                                                                                                                                    property post

                                                                                                                                                                                                    post: InterceptFunction;

                                                                                                                                                                                                      property put

                                                                                                                                                                                                      put: InterceptFunction;

                                                                                                                                                                                                        method activeMocks

                                                                                                                                                                                                        activeMocks: () => string[];

                                                                                                                                                                                                          method defaultReplyHeaders

                                                                                                                                                                                                          defaultReplyHeaders: (headers: ReplyHeaders) => this;

                                                                                                                                                                                                            method done

                                                                                                                                                                                                            done: () => void;

                                                                                                                                                                                                              method filteringPath

                                                                                                                                                                                                              filteringPath: {
                                                                                                                                                                                                              (regex: RegExp, replace: string): this;
                                                                                                                                                                                                              (fn: (path: string) => string): this;
                                                                                                                                                                                                              };

                                                                                                                                                                                                                method filteringRequestBody

                                                                                                                                                                                                                filteringRequestBody: {
                                                                                                                                                                                                                (regex: RegExp, replace: string): this;
                                                                                                                                                                                                                (fn: (body: string, recordedBody: string) => string): this;
                                                                                                                                                                                                                };

                                                                                                                                                                                                                  method isDone

                                                                                                                                                                                                                  isDone: () => boolean;

                                                                                                                                                                                                                    method matchHeader

                                                                                                                                                                                                                    matchHeader: (name: string, value: RequestHeaderMatcher) => this;

                                                                                                                                                                                                                      method pendingMocks

                                                                                                                                                                                                                      pendingMocks: () => string[];

                                                                                                                                                                                                                        method persist

                                                                                                                                                                                                                        persist: (flag?: boolean) => this;

                                                                                                                                                                                                                          method replyContentLength

                                                                                                                                                                                                                          replyContentLength: () => this;

                                                                                                                                                                                                                            method replyDate

                                                                                                                                                                                                                            replyDate: (d?: Date) => this;

                                                                                                                                                                                                                              Type Aliases

                                                                                                                                                                                                                              type BackMode

                                                                                                                                                                                                                              type BackMode = 'wild' | 'dryrun' | 'record' | 'update' | 'lockdown';

                                                                                                                                                                                                                                type Body

                                                                                                                                                                                                                                type Body = string | Record<string, any>;

                                                                                                                                                                                                                                  type DataMatcher

                                                                                                                                                                                                                                  type DataMatcher =
                                                                                                                                                                                                                                  | boolean
                                                                                                                                                                                                                                  | number
                                                                                                                                                                                                                                  | string
                                                                                                                                                                                                                                  | null
                                                                                                                                                                                                                                  | undefined
                                                                                                                                                                                                                                  | RegExp
                                                                                                                                                                                                                                  | DataMatcherArray
                                                                                                                                                                                                                                  | DataMatcherMap;

                                                                                                                                                                                                                                    type InterceptFunction

                                                                                                                                                                                                                                    type InterceptFunction = (
                                                                                                                                                                                                                                    uri: string | RegExp | { (uri: string): boolean },
                                                                                                                                                                                                                                    requestBody?: RequestBodyMatcher,
                                                                                                                                                                                                                                    interceptorOptions?: Options
                                                                                                                                                                                                                                    ) => Interceptor;

                                                                                                                                                                                                                                      type ReplyBody

                                                                                                                                                                                                                                      type ReplyBody = Body | Buffer | ReadStream;

                                                                                                                                                                                                                                        type ReplyFnResult

                                                                                                                                                                                                                                        type ReplyFnResult =
                                                                                                                                                                                                                                        | readonly [StatusCode]
                                                                                                                                                                                                                                        | readonly [StatusCode, ReplyBody]
                                                                                                                                                                                                                                        | readonly [StatusCode, ReplyBody, ReplyHeaders];

                                                                                                                                                                                                                                          type ReplyHeaderFunction

                                                                                                                                                                                                                                          type ReplyHeaderFunction = (
                                                                                                                                                                                                                                          req: ClientRequest,
                                                                                                                                                                                                                                          res: IncomingMessage,
                                                                                                                                                                                                                                          body: string | Buffer
                                                                                                                                                                                                                                          ) => string | string[];

                                                                                                                                                                                                                                            type ReplyHeaders

                                                                                                                                                                                                                                            type ReplyHeaders =
                                                                                                                                                                                                                                            | Record<string, ReplyHeaderValue>
                                                                                                                                                                                                                                            | Map<string, ReplyHeaderValue>
                                                                                                                                                                                                                                            | ReplyHeaderValue[];

                                                                                                                                                                                                                                              type ReplyHeaderValue

                                                                                                                                                                                                                                              type ReplyHeaderValue = string | string[] | ReplyHeaderFunction;

                                                                                                                                                                                                                                                type RequestBodyMatcher

                                                                                                                                                                                                                                                type RequestBodyMatcher =
                                                                                                                                                                                                                                                | string
                                                                                                                                                                                                                                                | Buffer
                                                                                                                                                                                                                                                | RegExp
                                                                                                                                                                                                                                                | DataMatcherArray
                                                                                                                                                                                                                                                | DataMatcherMap
                                                                                                                                                                                                                                                | { (body: any): boolean };

                                                                                                                                                                                                                                                  type RequestHeaderMatcher

                                                                                                                                                                                                                                                  type RequestHeaderMatcher = string | RegExp | { (fieldValue: string): boolean };

                                                                                                                                                                                                                                                    type StatusCode

                                                                                                                                                                                                                                                    type StatusCode = number;

                                                                                                                                                                                                                                                      Package Files (1)

                                                                                                                                                                                                                                                      Dependencies (3)

                                                                                                                                                                                                                                                      Dev Dependencies (25)

                                                                                                                                                                                                                                                      Peer Dependencies (0)

                                                                                                                                                                                                                                                      No peer dependencies.

                                                                                                                                                                                                                                                      Badge

                                                                                                                                                                                                                                                      To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                                                                                                                                                                                                                                      You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/nock.

                                                                                                                                                                                                                                                      • Markdown
                                                                                                                                                                                                                                                        [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/nock)
                                                                                                                                                                                                                                                      • HTML
                                                                                                                                                                                                                                                        <a href="https://www.jsdocs.io/package/nock"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>