summary refs log tree commit diff
path: root/api/tests
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-21 23:13:31 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-21 23:13:31 +0200
commitb3f8df560af34ba6904821eea684be8ccef14c9d (patch)
tree6e426481cf46dbb84aa626e46064c4ca44b92ac1 /api/tests
parent:sparkles: generate openapi documentation (diff)
downloadserver-b3f8df560af34ba6904821eea684be8ccef14c9d.tar.xz
:sparkles: unit tests expect event
Diffstat (limited to 'api/tests')
-rw-r--r--api/tests/routes.test.ts31
1 files changed, 30 insertions, 1 deletions
diff --git a/api/tests/routes.test.ts b/api/tests/routes.test.ts
index a9c75df1..fcaa3124 100644
--- a/api/tests/routes.test.ts
+++ b/api/tests/routes.test.ts
@@ -7,7 +7,7 @@ import fs from "fs";
 import Ajv from "ajv";
 import addFormats from "ajv-formats";
 import fetch from "node-fetch";
-import { User } from "@fosscord/util";
+import { Event, User, events } from "@fosscord/util";
 
 const SchemaPath = join(__dirname, "..", "assets", "schemas.json");
 const schemas = JSON.parse(fs.readFileSync(SchemaPath, { encoding: "utf8" }));
@@ -58,6 +58,12 @@ beforeAll(async (done) => {
 	}
 });
 
+const emit = events.emit;
+events.emit = (event: string | symbol, ...args: any[]) => {
+	events.emit("event", args[0]);
+	return emit(event, ...args);
+};
+
 describe("Automatic unit tests with route description middleware", () => {
 	const routes = getRouteDescriptions();
 
@@ -77,6 +83,23 @@ describe("Automatic unit tests with route description middleware", () => {
 			}
 
 			var body = "";
+			let eventEmitted = Promise.resolve();
+
+			if (route.test.event) {
+				if (!Array.isArray(route.test.event)) route.test.event = [route.test.event];
+
+				eventEmitted = new Promise((resolve, reject) => {
+					const timeout = setTimeout(() => reject, 1000);
+					const received = [];
+
+					events.on("event", (event: Event) => {
+						if (!route.test.event.includes(event.event)) return;
+
+						received.push(event.event);
+						if (received.length === route.test.event.length) resolve();
+					});
+				});
+			}
 
 			try {
 				const response = await fetch(`http://localhost:3001/api${urlPath}`, {
@@ -101,6 +124,12 @@ describe("Automatic unit tests with route description middleware", () => {
 				return done(error);
 			}
 
+			try {
+				await eventEmitted;
+			} catch (error) {
+				return done(new Error(`Event ${route.test.event} was not emitted`));
+			}
+
 			return done();
 		});
 	});