Add tests
This commit is contained in:
parent
6469de499f
commit
e77ee6bbdd
14
jest.config.js
Normal file
14
jest.config.js
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
module.exports = {
|
||||||
|
transform: {
|
||||||
|
"^.+\\.ts$": "ts-jest"
|
||||||
|
},
|
||||||
|
moduleFileExtensions: [
|
||||||
|
'js',
|
||||||
|
'ts',
|
||||||
|
'd.ts'
|
||||||
|
],
|
||||||
|
testMatch: [
|
||||||
|
'**/test/**/*.test.ts'
|
||||||
|
],
|
||||||
|
testEnvironment: 'node'
|
||||||
|
};
|
@ -12,8 +12,9 @@
|
|||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"test": "jest --verbose --runInBand",
|
||||||
"build": "rm -r dist && tsc && cp package.json dist/ && mkdir dist/types && cp src/types/* dist/types/",
|
"build": "rm -r dist && tsc && cp package.json dist/ && mkdir dist/types && cp src/types/* dist/types/",
|
||||||
"publish_to_local": "yarn build && cd dist && yarn publish"
|
"publish_to_local": "yarn test && yarn build && cd dist && yarn publish"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/config": "^0.0.36",
|
"@types/config": "^0.0.36",
|
||||||
@ -22,6 +23,7 @@
|
|||||||
"@types/cookie": "^0.3.3",
|
"@types/cookie": "^0.3.3",
|
||||||
"@types/cookie-parser": "^1.4.2",
|
"@types/cookie-parser": "^1.4.2",
|
||||||
"@types/express": "^4.17.6",
|
"@types/express": "^4.17.6",
|
||||||
|
"@types/jest": "^25.2.1",
|
||||||
"@types/mjml": "^4.0.4",
|
"@types/mjml": "^4.0.4",
|
||||||
"@types/mysql": "^2.15.10",
|
"@types/mysql": "^2.15.10",
|
||||||
"@types/nodemailer": "^6.4.0",
|
"@types/nodemailer": "^6.4.0",
|
||||||
@ -29,6 +31,8 @@
|
|||||||
"@types/on-finished": "^2.3.1",
|
"@types/on-finished": "^2.3.1",
|
||||||
"@types/uuid": "^7.0.2",
|
"@types/uuid": "^7.0.2",
|
||||||
"@types/ws": "^7.2.4",
|
"@types/ws": "^7.2.4",
|
||||||
|
"jest": "^25.4.0",
|
||||||
|
"ts-jest": "^25.4.0",
|
||||||
"typescript": "^3.8.3"
|
"typescript": "^3.8.3"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
64
test/Model.test.ts
Normal file
64
test/Model.test.ts
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
import registerMigrations from "./_registerMigrations";
|
||||||
|
import Model from "../src/db/Model";
|
||||||
|
import MysqlConnectionManager from "../src/db/MysqlConnectionManager";
|
||||||
|
import Validator from "../src/db/Validator";
|
||||||
|
|
||||||
|
class FakeDummyModel extends Model {
|
||||||
|
public name?: string;
|
||||||
|
public date?: Date;
|
||||||
|
public date_default?: Date;
|
||||||
|
|
||||||
|
protected defineProperties(): void {
|
||||||
|
this.defineProperty<string>('name', new Validator().acceptUndefined().between(3, 256));
|
||||||
|
this.defineProperty<Date>('date', new Validator());
|
||||||
|
this.defineProperty<Date>('date_default', new Validator());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
beforeAll(async (done) => {
|
||||||
|
registerMigrations();
|
||||||
|
await MysqlConnectionManager.prepare();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async (done) => {
|
||||||
|
await MysqlConnectionManager.endPool();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Model', () => {
|
||||||
|
it('should have a proper table name', async () => {
|
||||||
|
expect(FakeDummyModel.table).toBe('fake_dummy_models');
|
||||||
|
expect(new FakeDummyModel({}).table).toBe('fake_dummy_models');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should insert and retrieve properly', async () => {
|
||||||
|
await MysqlConnectionManager.query(`DROP TABLE IF EXISTS ${FakeDummyModel.table}`);
|
||||||
|
await MysqlConnectionManager.query(`CREATE TABLE ${FakeDummyModel.table}(
|
||||||
|
id INT NOT NULL AUTO_INCREMENT,
|
||||||
|
name VARCHAR(256),
|
||||||
|
date DATETIME,
|
||||||
|
date_default DATETIME DEFAULT NOW(),
|
||||||
|
PRIMARY KEY(id)
|
||||||
|
)`);
|
||||||
|
|
||||||
|
const date = new Date(569985);
|
||||||
|
let instance: FakeDummyModel | null = new FakeDummyModel({
|
||||||
|
name: 'name1',
|
||||||
|
date: date,
|
||||||
|
});
|
||||||
|
|
||||||
|
await instance.save();
|
||||||
|
expect(instance.id).toBe(1);
|
||||||
|
expect(instance.name).toBe('name1');
|
||||||
|
expect(instance.date?.getTime()).toBeCloseTo(date.getTime(), -4);
|
||||||
|
expect(instance.date_default).toBeDefined();
|
||||||
|
|
||||||
|
instance = await FakeDummyModel.getById(1);
|
||||||
|
expect(instance).toBeDefined();
|
||||||
|
expect(instance!.id).toBe(1);
|
||||||
|
expect(instance!.name).toBe('name1');
|
||||||
|
expect(instance!.date?.getTime()).toBeCloseTo(date.getTime(), -4);
|
||||||
|
expect(instance!.date_default).toBeDefined();
|
||||||
|
}, 15000);
|
||||||
|
});
|
15
test/_registerMigrations.ts
Normal file
15
test/_registerMigrations.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import CreateMigrationsTable from "../src/migrations/CreateMigrationsTable";
|
||||||
|
import CreateLogsTable from "../src/migrations/CreateLogsTable";
|
||||||
|
import MysqlConnectionManager from "../src/db/MysqlConnectionManager";
|
||||||
|
|
||||||
|
let migrationsRegistered = false;
|
||||||
|
|
||||||
|
export default function registerMigrations() {
|
||||||
|
if (!migrationsRegistered) {
|
||||||
|
migrationsRegistered = true;
|
||||||
|
[
|
||||||
|
CreateMigrationsTable,
|
||||||
|
CreateLogsTable,
|
||||||
|
].forEach(m => MysqlConnectionManager.registerMigration(v => new m(v)));
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "ES2020",
|
"target": "ES6",
|
||||||
"module": "CommonJS",
|
"module": "CommonJS",
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
"stripInternal": true,
|
"stripInternal": true,
|
||||||
@ -29,6 +29,6 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
"src"
|
"src/**/*"
|
||||||
]
|
]
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user