@eggjs/supertest
Version:
SuperAgent driven library for testing HTTP servers
97 lines (96 loc) • 3.41 kB
TypeScript
import type { Server } from 'node:net';
import { Request, type Response } from 'superagent';
import { AssertError } from './error/AssertError.js';
export type TestApplication = Server | string;
export type AssertFunction = (res: Response) => AssertError | void;
export type CallbackFunction = (err: AssertError | Error | null, res: Response) => void;
export type ResponseError = Error & {
syscall?: string;
code?: string;
status?: number;
};
export interface ExpectHeader {
name: string;
value: string | number | RegExp;
}
export declare class Test extends Request {
app: TestApplication;
_server: Server;
_asserts: AssertFunction[];
/**
* Initialize a new `Test` with the given `app`,
* request `method` and `path`.
*/
constructor(app: TestApplication, method: string, path: string);
/**
* Returns a URL, extracted from a server.
*
* @return {String} URL address
* @private
*/
protected serverAddress(app: Server, path: string): string;
/**
* Expectations:
*
* ```js
* .expect(200)
* .expect(200, fn)
* .expect(200, body)
* .expect('Some body')
* .expect('Some body', fn)
* .expect(['json array body', { key: 'val' }])
* .expect('Content-Type', 'application/json')
* .expect('Content-Type', 'application/json', fn)
* .expect(fn)
* .expect([200, 404])
* ```
*
* @return {Test} The current Test instance for chaining.
*/
expect(a: number | string | RegExp | object | AssertFunction, b?: string | number | RegExp | CallbackFunction, c?: CallbackFunction): Test;
/**
* UnExpectations:
*
* .unexpectHeader('Content-Type')
* .unexpectHeader('Content-Type', fn)
*/
unexpectHeader(name: string, fn?: CallbackFunction): this;
/**
* Expectations:
*
* .expectHeader('Content-Type')
* .expectHeader('Content-Type', fn)
*/
expectHeader(name: string, fn?: CallbackFunction): this;
_unexpectHeader(name: string, res: Response): AssertError | undefined;
_expectHeader(name: string, res: Response): AssertError | undefined;
/**
* Defer invoking superagent's `.end()` until
* the server is listening.
*/
end(fn: CallbackFunction): this;
/**
* Perform assertions and invoke `fn(err, res)`.
*/
assert(resError: ResponseError | null, res: Response, fn: CallbackFunction): void;
/**
* Perform assertions on a response body and return an Error upon failure.
*/
_assertBody(body: RegExp | string | number | object | null | undefined, res: Response): AssertError | undefined;
/**
* Perform assertions on a response header and return an Error upon failure.
*/
_assertHeader(header: ExpectHeader, res: Response): AssertError | undefined;
/**
* Perform assertions on the response status and return an Error upon failure.
*/
_assertStatus(status: number, res: Response): AssertError | undefined;
/**
* Perform assertions on the response status and return an Error upon failure.
*/
_assertStatusArray(statusArray: number[], res: Response): AssertError | undefined;
/**
* Performs an assertion by calling a function and return an Error upon failure.
*/
_assertFunction(fn: AssertFunction, res: Response): Error | undefined;
}