supertest-graphql
Version:
Extends supertest to test a GraphQL endpoint
103 lines (102 loc) • 4.25 kB
JavaScript
;
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 });
exports.asserNoError = exports.wrapAssertFn = exports.BlockingQueue = exports.getOperationName = void 0;
const getOperationName = (document) => {
var _a;
let operationName = undefined;
const operationDefinitions = document.definitions.filter((definition) => definition.kind === "OperationDefinition");
if (operationDefinitions.length === 1) {
operationName = (_a = operationDefinitions[0].name) === null || _a === void 0 ? void 0 : _a.value;
}
return operationName;
};
exports.getOperationName = getOperationName;
// inspired from https://github.com/davidje13/superwstest/blob/main/src/BlockingQueue.mjs
class BlockingQueue {
constructor() {
this.pendingPush = [];
this.pendingPop = [];
}
push(item) {
if (this.pendingPop.length > 0) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- it's verified with above condition
const firstPendingPop = this.pendingPop.shift();
if (typeof firstPendingPop.tm === "number") {
clearTimeout(firstPendingPop.tm);
}
firstPendingPop.resolve(item);
}
else {
this.pendingPush.push(item);
}
}
pop(timeout) {
return __awaiter(this, void 0, void 0, function* () {
if (this.pendingPush.length > 0) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- it's verified with above condition
return this.pendingPush.shift();
}
return new Promise((resolve, reject) => {
const newPop = { resolve, tm: null };
this.pendingPop.push(newPop);
if (timeout !== undefined) {
newPop.tm = setTimeout(() => {
this.pendingPop = this.pendingPop.filter((pop) => pop !== newPop);
reject(new Error(`Timeout after ${timeout}ms`));
}, timeout);
}
});
});
}
flush() {
const flushed = [...this.pendingPush];
this.pendingPush = [];
return flushed;
}
get length() {
return this.pendingPush.length;
}
}
exports.BlockingQueue = BlockingQueue;
/**
* Wraps an assert function into another.
* The wrapper function edit the stack trace of any assertion error, prepending a more useful stack to it.
*
* Borrowed from supertest
*/
function wrapAssertFn(assertFn) {
var _a;
const savedStack = ((_a = new Error().stack) === null || _a === void 0 ? void 0 : _a.split("\n").slice(3)) || [];
return (res) => __awaiter(this, void 0, void 0, function* () {
let badStack;
const err = yield assertFn(res);
if (err instanceof Error && err.stack) {
badStack = err.stack.replace(err.message, "").split("\n").slice(1);
err.stack = [err.toString()]
.concat(savedStack)
.concat("----")
.concat(badStack)
.join("\n");
}
return err;
});
}
exports.wrapAssertFn = wrapAssertFn;
const asserNoError = ({ errors }) => {
if (errors && Array.isArray(errors) && errors.length > 0) {
const errorSummary = errors
.map((e) => e.message)
.join(",");
return new Error(`expected no errors but got ${errors.length} error(s) in GraphQL response: ${errorSummary}`);
}
};
exports.asserNoError = asserNoError;