From 2e670c8935ddf6ad093afc23d2d427c632070238 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Thu, 26 Aug 2021 02:07:16 +0200 Subject: :bug: fix unit tests --- util/tests/setupJest.js | 19 +++++++++++++++++++ util/tests/validate.test.js | 25 +++++++++++++------------ 2 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 util/tests/setupJest.js (limited to 'util/tests') diff --git a/util/tests/setupJest.js b/util/tests/setupJest.js new file mode 100644 index 00000000..551d2be5 --- /dev/null +++ b/util/tests/setupJest.js @@ -0,0 +1,19 @@ +const { performance } = require("perf_hooks"); + +global.expect.extend({ + toBeFasterThan: async (func, target) => { + const start = performance.now(); + var error; + try { + await func(); + } catch (e) { + error = e.toString(); + } + const time = performance.now() - start; + + return { + pass: time < target && !error, + message: () => error || `${func.name} took ${time}ms of maximum ${target}`, + }; + }, +}); diff --git a/util/tests/validate.test.js b/util/tests/validate.test.js index d36da1ed..c885a167 100644 --- a/util/tests/validate.test.js +++ b/util/tests/validate.test.js @@ -1,30 +1,31 @@ const { initDatabase } = require("../dist/util/Database"); const { User } = require("../dist/entities/User"); +jest.setTimeout(10000); -beforeAll(async () => { - await initDatabase(); - new User().validate(); +beforeAll((done) => { + initDatabase().then(() => { + new User().validate(); // warm up schema/model + done(); + }); }); describe("Validate model class properties", () => { - describe("validation should be faster than 20ms", () => { - expect(() => new User().validate()).toBeFasterThan(20); - }); - describe("User", () => { - test("object instead of string", () => { + test("object instead of string", async () => { expect(() => { new User({ username: {} }).validate(); }).toThrow(); }); }); + test("validation should be faster than 20ms", () => { + expect(() => { + new User().validate(); + }).toBeFasterThan(20); + }); + test("should not set opts", () => { const user = new User({ opts: { id: 0 } }); expect(user.opts.id).not.toBe(0); }); - - test("test", () => { - expect(1).toBe(1); - }); }); -- cgit 1.5.1