unit test

This commit is contained in:
Flam3rboy 2021-08-26 00:22:26 +02:00
parent ad05822bbf
commit 41dd600c06
10 changed files with 1025 additions and 9188 deletions

View File

@ -8,10 +8,19 @@
"sourceMaps": true, "sourceMaps": true,
"type": "node", "type": "node",
"request": "launch", "request": "launch",
"name": "Launch Server", "name": "Launch Util",
"program": "${workspaceFolder}/dist/index.js", "program": "${workspaceFolder}/dist/index.js",
"preLaunchTask": "tsc: build - tsconfig.json", "preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": ["${workspaceFolder}/dist/**/*.js"] "outFiles": ["${workspaceFolder}/dist/**/*.js"]
},
{
"name": "Debug Jest Tests",
"type": "node",
"request": "launch",
"runtimeArgs": ["--inspect-brk", "${workspaceRoot}/node_modules/jest/bin/jest.js", "--runInBand"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
} }
] ]
} }

10159
util/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -6,8 +6,9 @@
"types": "dist/index.d.ts", "types": "dist/index.d.ts",
"scripts": { "scripts": {
"start": "npm run build && node dist/", "start": "npm run build && node dist/",
"patch": "patch-package",
"test": "npm run build && jest", "test": "npm run build && jest",
"postinstall": "npm run build", "postinstall": "npm run patch && npm run build",
"build": "npx tsc -b .", "build": "npx tsc -b .",
"generate:schema": "npx typescript-json-schema tsconfig.json '*' -o src/entities/schema.json" "generate:schema": "npx typescript-json-schema tsconfig.json '*' -o src/entities/schema.json"
}, },
@ -46,12 +47,12 @@
"jsonwebtoken": "^8.5.1", "jsonwebtoken": "^8.5.1",
"missing-native-js-functions": "^1.2.10", "missing-native-js-functions": "^1.2.10",
"node-fetch": "^2.6.1", "node-fetch": "^2.6.1",
"patch-package": "^6.4.7",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"sqlite3": "^5.0.2", "sqlite3": "^5.0.2",
"ts-transform-json-schema": "^2.0.3",
"typeorm": "^0.2.37", "typeorm": "^0.2.37",
"typescript": "^4.3.5", "typescript": "^4.3.5",
"typescript-json-schema": "github:fosscord/typescript-json-schema" "typescript-json-schema": "^0.50.1"
}, },
"jest": { "jest": {
"setupFilesAfterEnv": [ "setupFilesAfterEnv": [

View File

@ -8,6 +8,7 @@ const ajv = new Ajv({
removeAdditional: "all", removeAdditional: "all",
useDefaults: true, useDefaults: true,
coerceTypes: true, coerceTypes: true,
// @ts-ignore
validateFormats: false, validateFormats: false,
allowUnionTypes: true, allowUnionTypes: true,
}); });
@ -23,12 +24,18 @@ export class BaseClass extends BaseEntity {
this.assign(props); this.assign(props);
if (!this.construct.schema) this.construct.schema = { ...schema, $ref: `#/definitions/${this.construct.name}` }; if (!this.construct.schema) this.construct.schema = { ...schema, $ref: `#/definitions/${this.construct.name}` };
this.id = this.opts.id || Snowflake.generate();
} }
get construct(): any { get construct(): any {
return this.constructor; return this.constructor;
} }
get metadata() {
return this.construct.getRepository().metadata;
}
assign(props: any) { assign(props: any) {
if (!props || typeof props !== "object") return; if (!props || typeof props !== "object") return;
@ -39,8 +46,6 @@ export class BaseClass extends BaseEntity {
Object.defineProperty(this, key, { value: props[key] }); Object.defineProperty(this, key, { value: props[key] });
} }
this.id = this.opts.id || Snowflake.generate();
} }
@BeforeUpdate() @BeforeUpdate()
@ -48,10 +53,7 @@ export class BaseClass extends BaseEntity {
validate() { validate() {
const valid = ajv.validate(this.construct.schema, this.toJSON()); const valid = ajv.validate(this.construct.schema, this.toJSON());
if (!valid) throw ajv.errors; if (!valid) throw ajv.errors;
} return this;
get metadata() {
return this.construct.getRepository().metadata;
} }
toJSON(): any { toJSON(): any {

View File

@ -1,4 +1,4 @@
import { Column, Entity, JoinColumn, OneToMany, OneToOne } from "typeorm"; import { Column, Entity, JoinColumn, OneToMany } from "typeorm";
import { BaseClass } from "./BaseClass"; import { BaseClass } from "./BaseClass";
import { BitField } from "../util/BitField"; import { BitField } from "../util/BitField";
import { Relationship } from "./Relationship"; import { Relationship } from "./Relationship";

View File

@ -1,10 +1,9 @@
import "reflect-metadata"; import "reflect-metadata";
// export * as Constants from "../util/Constants"; // export * as Constants from "../util/Constants";
export * from "./util/index";
export * from "./interfaces/index"; export * from "./interfaces/index";
export * from "./entities/index"; export * from "./entities/index";
export * from "./util/index";
import "./test";
// import Config from "../util/Config"; // import Config from "../util/Config";
// import db, { MongooseCache, toObject } from "./util/Database"; // import db, { MongooseCache, toObject } from "./util/Database";

View File

@ -10,6 +10,7 @@ var promise: Promise<any>;
export function initDatabase() { export function initDatabase() {
if (promise) return promise; // prevent initalizing multiple times if (promise) return promise; // prevent initalizing multiple times
console.log("[Database] connecting ...");
// @ts-ignore // @ts-ignore
promise = createConnection({ promise = createConnection({
type: "sqlite", type: "sqlite",
@ -19,7 +20,7 @@ export function initDatabase() {
logging: false, logging: false,
}); });
promise.then(() => console.log("[Database] connected"));
return promise; return promise;
} }
initDatabase();

View File

@ -9,7 +9,7 @@ export function checkToken(token: string, jwtSecret: string): Promise<any> {
jwt.verify(token, jwtSecret, JWTOptions, async (err, decoded: any) => { jwt.verify(token, jwtSecret, JWTOptions, async (err, decoded: any) => {
if (err || !decoded) return rej("Invalid Token"); if (err || !decoded) return rej("Invalid Token");
const user = await User.findOneOrFail( const user = await User.findOne(
{ id: decoded.id }, { id: decoded.id },
{ select: ["user_data", "bot", "disabled", "deleted"] } { select: ["user_data", "bot", "disabled", "deleted"] }
); );

View File

@ -1,7 +1,8 @@
export * from "./Database";
export * from "./Regex"; export * from "./Regex";
export * from "./String"; export * from "./String";
export * from "./BitField"; export * from "./BitField";
export * from "./Database";
export * from "./Intents"; export * from "./Intents";
export * from "./MessageFlags"; export * from "./MessageFlags";
export * from "./Permissions"; export * from "./Permissions";

View File

@ -3,8 +3,7 @@ const { User } = require("../dist/entities/User");
beforeAll(async () => { beforeAll(async () => {
await initDatabase(); await initDatabase();
new User().validate();
new User().validate(); // initalize schema validator
}); });
describe("Validate model class properties", () => { describe("Validate model class properties", () => {
@ -24,4 +23,8 @@ describe("Validate model class properties", () => {
const user = new User({ opts: { id: 0 } }); const user = new User({ opts: { id: 0 } });
expect(user.opts.id).not.toBe(0); expect(user.opts.id).not.toBe(0);
}); });
test("test", () => {
expect(1).toBe(1);
});
}); });