Add log class and add some functins
All checks were successful
continuous-integration/drone/push Build is passing

Add jest for test units
Updated the package and lib
This commit is contained in:
Sambo Chea 2021-03-10 20:23:49 +07:00
parent df0b744058
commit 9e9cbe1162
7 changed files with 118 additions and 26 deletions

20
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,20 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["${relativeFile}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
}
}
]
}

6
jest.config.js Normal file
View File

@ -0,0 +1,6 @@
module.exports = {
transform: { '^.+\\.ts?$': 'ts-jest' },
testEnvironment: 'node',
testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']
};

View File

@ -9,17 +9,22 @@
"private": false, "private": false,
"dependencies": {}, "dependencies": {},
"devDependencies": { "devDependencies": {
"@types/jest": "^26.0.20",
"@typescript-eslint/eslint-plugin": "^4.17.0", "@typescript-eslint/eslint-plugin": "^4.17.0",
"@typescript-eslint/parser": "^4.17.0", "@typescript-eslint/parser": "^4.17.0",
"eslint": "^7.21.0", "eslint": "^7.21.0",
"eslint-config-prettier": "^8.1.0", "eslint-config-prettier": "^8.1.0",
"eslint-plugin-prettier": "^3.3.1", "eslint-plugin-prettier": "^3.3.1",
"jest": "^26.6.3",
"prettier": "^2.2.1", "prettier": "^2.2.1",
"ts-jest": "^26.5.3",
"typescript": "^4.2.3" "typescript": "^4.2.3"
}, },
"scripts": { "scripts": {
"build": "tsc -p tsconfig.json", "build": "tsc -p tsconfig.json",
"lint": "eslint . --ext .ts", "lint": "eslint . --ext .ts",
"prettier": "prettier --config .prettierrc 'src/**/*.ts' --write" "prettier": "prettier --config .prettierrc 'src/**/*.ts' --write",
"test": "jest",
"coverage": "jest --coverage"
} }
} }

31
src/log/Log.ts Normal file
View File

@ -0,0 +1,31 @@
import { Loggable } from "./interfaces";
class Log {
static log(loggable: Loggable): void {
switch (loggable.type) {
case 'error':
this.error(loggable.from, loggable.data)
break;
case 'warn':
this.warn(loggable.from, loggable.data)
break;
default:
this.info(loggable.from, loggable.data)
break;
}
}
static info(...data: any[]): void {
console.log(data);
}
static error(...data: any[]): void {
console.error(data)
}
static warn(...data: any[]): void {
console.warn(data)
}
}
export default Log;

7
src/log/interfaces.ts Normal file
View File

@ -0,0 +1,7 @@
type LogType = 'info' | 'error' | 'warn'
export interface Loggable {
type: LogType;
from?: string;
data?: any;
}

23
tests/log.test.ts Normal file
View File

@ -0,0 +1,23 @@
import Log from '../src/log/Log';
describe('loggin into console', function () {
it('info', function () {
Log.info('I am an info!');
});
it('warn', function () {
Log.info('I am a warn!');
});
it('error', function () {
Log.info('I am an error!');
});
it('loggable', function () {
Log.log({
type: 'info',
from: 'log.test.ts',
data: 'I am loggable called'
});
});
});

View File

@ -11,11 +11,11 @@
"module": "commonjs", "module": "commonjs",
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true, "noEmit": true,
"declaration": true, "declaration": true,
"outDir": "dist", "outDir": "dist",
"rootDir": "src" "rootDir": "src",
}, },
"include": ["src/**/*"] "include": ["src/**/*"],
"exclude": ["node_modules"]
} }