supertest-graphql
Version:
Extends supertest to test a GraphQL endpoint
141 lines (140 loc) • 5.02 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const graphql_1 = require("graphql");
const utils_1 = require("./utils");
class SuperTestGraphQL {
constructor(_supertest) {
this._supertest = _supertest;
this._path = "/graphql";
this._asserts = [];
}
/**
* 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, variables) {
return this.operation(query, variables);
}
/**
* 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, variables) {
return this.operation(mutation, variables);
}
/**
* 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, variables) {
if (typeof operation !== "string") {
this._operationName = (0, utils_1.getOperationName)(operation);
}
this._query = typeof operation === "string" ? operation : (0, graphql_1.print)(operation);
this._variables = variables;
return this;
}
/**
* Set variables.
* @param - variables
*/
variables(variables) {
this._variables = variables;
return this;
}
/**
* Set the GraphQL endpoint path.
*
* @default "/graphql"
*/
path(path) {
this._path = path;
return this;
}
auth(...args) {
this._supertest.auth(...args);
return this;
}
set(...args) {
this._supertest.set(...args);
return this;
}
/**
* Assert that there is no errors (`.errors` field) in response returned from the GraphQL API.
*/
expectNoErrors() {
this._asserts.push((0, utils_1.wrapAssertFn)(utils_1.asserNoError));
return this;
}
/**
* Access to underlying supertest instance.
*/
supertest() {
return this._supertest;
}
assert(result) {
return __awaiter(this, void 0, void 0, function* () {
for (const assertFn of this._asserts) {
const maybeError = yield assertFn(result);
if (maybeError instanceof Error)
throw maybeError;
}
});
}
end() {
return __awaiter(this, void 0, void 0, function* () {
if (this._query === undefined)
throw new Error("You should call `query` or `mutate`");
const payload = {
query: this._query,
};
if (this._operationName) {
payload.operationName = this._operationName;
}
if (this._variables) {
payload.variables = this._variables;
}
const response = yield this._supertest
.post(this._path)
.accept("application/json")
.send(payload);
if (typeof response.body !== "object") {
throw new Error(`Received a non valid body ${response.body}`);
}
const result = Object.assign(Object.assign({}, response.body), { response });
yield this.assert(result);
return Object.assign(Object.assign({}, response.body), { response });
});
}
then(onfulfilled, onrejected) {
return __awaiter(this, void 0, void 0, function* () {
try {
if (this._query === undefined)
throw new Error("You should call `query` or `mutate`");
const res = yield this.end();
if (onfulfilled)
return onfulfilled(res);
// @ts-expect-error no idea why
return res;
}
catch (e) {
if (onrejected)
return onrejected(e);
throw new Error("No rejection");
}
});
}
}
exports.default = SuperTestGraphQL;