route middleware test option

This commit is contained in:
Flam3rboy 2021-09-18 11:56:06 +02:00
parent a7bf295591
commit a3484e64e4
4 changed files with 12 additions and 13 deletions

View File

@ -2,6 +2,7 @@ import { traverseDirectory } from "lambert-server";
import path from "path"; import path from "path";
import express from "express"; import express from "express";
import * as RouteUtility from "../dist/util/route"; import * as RouteUtility from "../dist/util/route";
import { RouteOptions } from "../dist/util/route";
const Router = express.Router; const Router = express.Router;
const routes = new Map<string, RouteUtility.RouteOptions>(); const routes = new Map<string, RouteUtility.RouteOptions>();
@ -12,9 +13,10 @@ const methods = ["get", "post", "put", "delete", "patch"];
function registerPath(file, method, prefix, path, ...args) { function registerPath(file, method, prefix, path, ...args) {
const urlPath = prefix + path; const urlPath = prefix + path;
const sourceFile = file.replace("/dist/", "/src/").replace(".js", ".ts"); const sourceFile = file.replace("/dist/", "/src/").replace(".js", ".ts");
const opts = args.find((x) => typeof x === "object"); const opts: RouteOptions = args.find((x) => typeof x === "object");
if (opts) { if (opts) {
routes.set(urlPath + "|" + method, opts); routes.set(urlPath + "|" + method, opts); // @ts-ignore
opts.file = sourceFile;
// console.log(method, urlPath, opts); // console.log(method, urlPath, opts);
} else { } else {
console.log(`${sourceFile}\nrouter.${method}("${path}") is missing the "route()" description middleware\n`, args); console.log(`${sourceFile}\nrouter.${method}("${path}") is missing the "route()" description middleware\n`, args);

View File

@ -11,7 +11,7 @@ export interface UserProfileResponse {
premium_since?: Date; premium_since?: Date;
} }
router.get("/", route({ response: { body: "UserProfileResponse" } }), async (req: Request, res: Response) => { router.get("/", route({ test: { response: { body: "UserProfileResponse" } } }), async (req: Request, res: Response) => {
if (req.params.id === "@me") req.params.id = req.user_id; if (req.params.id === "@me") req.params.id = req.user_id;
const user = await User.getPublicUser(req.params.id, { relations: ["connected_accounts"] }); const user = await User.getPublicUser(req.params.id, { relations: ["connected_accounts"] });

View File

@ -33,11 +33,11 @@ export type RouteResponse = { status?: number; body?: `${string}Response`; heade
export interface RouteOptions { export interface RouteOptions {
permission?: PermissionResolvable; permission?: PermissionResolvable;
body?: `${string}Schema`; // typescript interface name body?: `${string}Schema`; // typescript interface name
response?: RouteResponse; test?: {
example?: { response?: RouteResponse;
body?: any; body?: any;
path?: string; path?: string;
event?: EventData; event?: EventData | EventData[];
headers?: Record<string, string>; headers?: Record<string, string>;
}; };
} }

View File

@ -3,13 +3,13 @@
import getRouteDescriptions from "../jest/getRouteDescriptions"; import getRouteDescriptions from "../jest/getRouteDescriptions";
import supertest, { Response } from "supertest"; import supertest, { Response } from "supertest";
import path from "path"; import { join } from "path";
import fs from "fs"; import fs from "fs";
import Ajv from "ajv"; import Ajv from "ajv";
import addFormats from "ajv-formats"; import addFormats from "ajv-formats";
const request = supertest("http://localhost:3001/api"); const request = supertest("http://localhost:3001/api");
const SchemaPath = path.join(__dirname, "..", "assets", "responses.json"); const SchemaPath = join(__dirname, "..", "assets", "responses.json");
const schemas = JSON.parse(fs.readFileSync(SchemaPath, { encoding: "utf8" })); const schemas = JSON.parse(fs.readFileSync(SchemaPath, { encoding: "utf8" }));
export const ajv = new Ajv({ export const ajv = new Ajv({
allErrors: true, allErrors: true,
@ -27,13 +27,10 @@ describe("Automatic unit tests with route description middleware", () => {
routes.forEach((route, pathAndMethod) => { routes.forEach((route, pathAndMethod) => {
const [path, method] = pathAndMethod.split("|"); const [path, method] = pathAndMethod.split("|");
test(path, (done) => { test(path, (done) => {
if (!route.example) { if (!route.example) {
console.log(`Route ${path} is missing the example property`); console.log(`${(route as any).file}\nrouter.${method} is missing the test property`);
return done();
}
if (!route.response) {
console.log(`Route ${path} is missing the response property`);
return done(); return done();
} }
const urlPath = path || route.example?.path; const urlPath = path || route.example?.path;