🐛 fix unit tests
This commit is contained in:
parent
41dd600c06
commit
2e670c8935
793
util/package-lock.json
generated
793
util/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -43,7 +43,6 @@
|
|||||||
"class-validator": "^0.13.1",
|
"class-validator": "^0.13.1",
|
||||||
"dot-prop": "^6.0.1",
|
"dot-prop": "^6.0.1",
|
||||||
"env-paths": "^2.2.1",
|
"env-paths": "^2.2.1",
|
||||||
"jest-test-performance": "^1.0.1",
|
|
||||||
"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",
|
||||||
@ -56,7 +55,7 @@
|
|||||||
},
|
},
|
||||||
"jest": {
|
"jest": {
|
||||||
"setupFilesAfterEnv": [
|
"setupFilesAfterEnv": [
|
||||||
"jest-test-performance"
|
"./tests/setupJest.js"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,9 @@ export class BaseClass extends BaseEntity {
|
|||||||
super();
|
super();
|
||||||
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 = ajv.compile({ ...schema, $ref: `#/definitions/${this.construct.name}` });
|
||||||
|
}
|
||||||
|
|
||||||
this.id = this.opts.id || Snowflake.generate();
|
this.id = this.opts.id || Snowflake.generate();
|
||||||
}
|
}
|
||||||
@ -51,7 +53,7 @@ export class BaseClass extends BaseEntity {
|
|||||||
@BeforeUpdate()
|
@BeforeUpdate()
|
||||||
@BeforeInsert()
|
@BeforeInsert()
|
||||||
validate() {
|
validate() {
|
||||||
const valid = ajv.validate(this.construct.schema, this.toJSON());
|
const valid = this.construct.schema(this.toJSON());
|
||||||
if (!valid) throw ajv.errors;
|
if (!valid) throw ajv.errors;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
19
util/tests/setupJest.js
Normal file
19
util/tests/setupJest.js
Normal file
@ -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}`,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
});
|
@ -1,30 +1,31 @@
|
|||||||
const { initDatabase } = require("../dist/util/Database");
|
const { initDatabase } = require("../dist/util/Database");
|
||||||
const { User } = require("../dist/entities/User");
|
const { User } = require("../dist/entities/User");
|
||||||
|
jest.setTimeout(10000);
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll((done) => {
|
||||||
await initDatabase();
|
initDatabase().then(() => {
|
||||||
new User().validate();
|
new User().validate(); // warm up schema/model
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Validate model class properties", () => {
|
describe("Validate model class properties", () => {
|
||||||
describe("validation should be faster than 20ms", () => {
|
|
||||||
expect(() => new User().validate()).toBeFasterThan(20);
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("User", () => {
|
describe("User", () => {
|
||||||
test("object instead of string", () => {
|
test("object instead of string", async () => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
new User({ username: {} }).validate();
|
new User({ username: {} }).validate();
|
||||||
}).toThrow();
|
}).toThrow();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("validation should be faster than 20ms", () => {
|
||||||
|
expect(() => {
|
||||||
|
new User().validate();
|
||||||
|
}).toBeFasterThan(20);
|
||||||
|
});
|
||||||
|
|
||||||
test("should not set opts", () => {
|
test("should not set opts", () => {
|
||||||
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);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user