@tresdoce-nestjs-toolkit/test-utils
Version:
Tresdoce NestJS Toolkit - Utilities para testing
83 lines (82 loc) • 2.92 kB
TypeScript
import { Interceptor, Options, RequestHeaderMatcher } from 'nock';
/**
* Supported HTTP methods for the mock.
*/
type HttpMethod = 'get' | 'post' | 'put' | 'head' | 'patch' | 'delete' | 'options';
/**
* Structure for expected query parameters.
*/
type QueryParams = {
[key: string]: string | number | boolean | string[] | number[] | boolean[] | null | undefined;
};
/**
* Types for matching request headers. Can be a direct string match or a custom matcher function.
*/
type RequestHeaderMatcherType = RequestHeaderMatcher | string;
/**
* Configuration options for creating a nock mock.
*
* @property {string} url - The complete URL to intercept.
* @property {HttpMethod} method - The HTTP method to intercept.
* @property {number} statusCode - The HTTP status code to respond with.
* @property {string | Record<string, unknown> | Buffer | (() => Record<string, unknown>)} responseBody - The response body. Can be a string, object, Buffer, or function returning an object.
* @property {Options & { reqheaders?: Record<string, RequestHeaderMatcherType> }} [options] - Optional nock configuration options.
* @property {string | Record<string, unknown> | Buffer} [reqBody] - Optional expected request body.
* @property {QueryParams} [queryParams] - Optional expected query parameters.
*/
interface CreateMock {
url: string;
method: HttpMethod;
statusCode: number;
responseBody: string | Record<string, unknown> | Buffer | (() => Record<string, unknown>);
options?: Options & {
reqheaders?: Record<string, RequestHeaderMatcherType>;
};
reqBody?: string | Record<string, unknown> | Buffer;
queryParams?: QueryParams;
}
/**
* Creates a nock mock based on provided options.
*
* @param _options - Configuration options for the mock.
* @returns A configured nock interceptor.
* @example
*
* // Using a JSON object as responseBody
* const mock1 = createMock({
* url: 'http://example.com/api/data',
* method: 'get',
* statusCode: 200,
* responseBody: { success: true }
* });
*
* // Using a string as responseBody
* const mock2 = createMock({
* url: 'http://example.com/api/message',
* method: 'get',
* statusCode: 200,
* responseBody: 'Success message'
* });
*
* // Using a Buffer as responseBody
* const mock3 = createMock({
* url: 'http://example.com/api/file',
* method: 'get',
* statusCode: 200,
* responseBody: Buffer.from('Some binary data')
* });
*
* // Using a function returning a JSON object as responseBody
* const mock4 = createMock({
* url: 'http://example.com/api/dynamicData',
* method: 'get',
* statusCode: 200,
* responseBody: () => JSON.parse(fs.readFileSync('path/to/fixture.json', 'utf8'))
* });
*/
export declare const createMock: (_options: CreateMock) => Interceptor;
/**
* Cleans all the mocks created by nock.
*/
export declare const cleanAllMock: () => void;
export {};