supertest-graphql
Version:
Extends supertest to test a GraphQL endpoint
75 lines (74 loc) • 2.89 kB
TypeScript
import { DocumentNode, ExecutionResult } from "graphql";
import { SuperAgentTest, Response } from "supertest";
import { Variables } from "./types";
export declare type SuperTestExecutionResult<TData> = ExecutionResult<TData> & {
response: Response;
};
export default class SuperTestGraphQL<TData, TVariables extends Variables> implements PromiseLike<SuperTestExecutionResult<TData>> {
private _supertest;
private _query?;
private _operationName?;
private _variables?;
private _path;
private _asserts;
constructor(_supertest: SuperAgentTest);
/**
* Send a GraphQL Query Document to the GraphQL server for execution.
* @param query - the query to execute as string or `DocumentNode`
* @param variables - the variables for this query
*/
query(query: DocumentNode | string, variables?: TVariables): this;
/**
* Send a GraphQL Query Document to the GraphQL server for execution.
* @param mutation - the mutation to execute as string or `DocumentNode`
* @param variables - the variables for this mutation
*/
mutate(mutation: DocumentNode | string, variables?: TVariables): this;
/**
* Send a GraphQL Query Document to the GraphQL server for execution.
* @param operation - the operation to execute as string or `DocumentNode`
* @param variables - the variables for this operation
*/
operation(operation: DocumentNode | string, variables?: TVariables): this;
/**
* Set variables.
* @param - variables
*/
variables(variables: TVariables): this;
/**
* Set the GraphQL endpoint path.
*
* @default "/graphql"
*/
path(path: string): this;
/**
* Set authentication parameters for the request.
*
* @see [supragent.auth](https://visionmedia.github.io/superagent/#authentication)
*/
auth(user: string, pass: string, options?: {
type: "basic" | "auto";
}): this;
auth(token: string, options: {
type: "bearer";
}): this;
/**
* Set headers for the request.
*
* @see [supragent.set](https://visionmedia.github.io/superagent/#setting-header-fields)
*/
set(field: object): this;
set(field: string, val: string): this;
set(field: "Cookie", val: string[]): this;
/**
* Assert that there is no errors (`.errors` field) in response returned from the GraphQL API.
*/
expectNoErrors(): this;
/**
* Access to underlying supertest instance.
*/
supertest(): SuperAgentTest;
private assert;
end(): Promise<SuperTestExecutionResult<TData>>;
then<TResult1 = SuperTestExecutionResult<TData>, TResult2 = never>(onfulfilled?: ((value: SuperTestExecutionResult<TData>) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
}