Skip to content

Commit

Permalink
add jest for unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanwerfling committed Aug 29, 2024
1 parent 4aefaf1 commit 2e84f56
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 21 deletions.
6 changes: 6 additions & 0 deletions backend/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"presets": [
"@babel/preset-env",
"@babel/preset-typescript"
]
}
6 changes: 4 additions & 2 deletions backend/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"env": {
"amd": true,
"es6": true,
"node": true
"node": true,
"jest": true
},
"extends": [
"eslint:all"
Expand All @@ -13,7 +14,8 @@
},
"plugins": [
"@typescript-eslint",
"prefer-arrow"
"prefer-arrow",
"jest"
],
"rules": {
// typescript rules
Expand Down
20 changes: 20 additions & 0 deletions backend/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { JestConfigWithTsJest } from 'ts-jest';

const config: JestConfigWithTsJest = {
verbose: true,
transform: {
'^.+\\.ts?$': [
'ts-jest',
{
useESM: true,
},
],
},
extensionsToTreatAsEsm: ['.ts'],
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts']
};

export default config;
1 change: 1 addition & 0 deletions backend/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO Env
4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "dist/main.js",
"type": "module",
"scripts": {
"test": "node --experimental-vm-modules ../node_modules/.bin/jest",
"npm-check-updates": "npm-check-updates",
"tsc": "tsc",
"build": "npm run tsc"
Expand Down Expand Up @@ -37,6 +38,7 @@
"@typescript-eslint/eslint-plugin": "^7.4.0",
"@typescript-eslint/parser": "^7.4.0",
"eslint": "^8.46.0",
"eslint-plugin-jest": "^28.8.0",
"eslint-plugin-prefer-arrow": "^1.2.3",
"typescript": "^4.9.5"
},
Expand All @@ -54,8 +56,8 @@
"express-session": "^1.17.3",
"fast-xml-parser": "^4.2.7",
"flyingfish_core": "file:../core",
"flyingfish_schemas": "file:../schemas",
"flyingfish_plugin_letsencrypt": "file:../plugins/letsencrypt",
"flyingfish_schemas": "file:../schemas",
"got": "^12.6.1",
"helmet": "^7.1.0",
"moment": "^2.29.4",
Expand Down
72 changes: 57 additions & 15 deletions backend/src/inc/Provider/IpLocate/IpLocateIo.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,66 @@
import {Logger} from 'flyingfish_core';
import got from 'got';
import {ExtractSchemaResultType, Vts} from 'vts';

export const SchemaIpLocateData = Vts.object({
id: Vts.or([
Vts.string(),
Vts.null()
]),
country: Vts.or([
Vts.string(),
Vts.null()
]),
country_code: Vts.or([
Vts.string(),
Vts.null()
]),
city: Vts.or([
Vts.string(),
Vts.null()
]),
continent: Vts.or([
Vts.string(),
Vts.null()
]),
latitude: Vts.or([
Vts.string(),
Vts.null()
]),
longitude: Vts.or([
Vts.string(),
Vts.null()
]),
time_zone: Vts.or([
Vts.string(),
Vts.null()
]),
postal_code: Vts.or([
Vts.string(),
Vts.null()
]),
org: Vts.or([
Vts.string(),
Vts.null()
]),
asn: Vts.or([
Vts.string(),
Vts.null()
]),
subdivision: Vts.or([
Vts.string(),
Vts.null()
]),
subdivision2: Vts.or([
Vts.string(),
Vts.null()
])
});

/**
* IpLocateData
*/
export type IpLocateData = {
ip: string|null;
country: string|null;
country_code: string|null;
city: string|null;
continent: string|null;
latitude: string|null;
longitude: string|null;
time_zone: string|null;
postal_code: string|null;
org: string|null;
asn: string|null;
subdivision: string|null;
subdivision2: string|null;
};
export type IpLocateData = ExtractSchemaResultType<typeof SchemaIpLocateData>;

/**
* IpLocateIo
Expand Down
36 changes: 36 additions & 0 deletions backend/src/types/ExpectSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {Schema, SchemaErrors} from 'vts';

interface CustomMatcherResult {
pass: boolean;
message: () => string;
}

export const SchemaMatchers = {
toValidateSchema: (received: any, schema: Schema<any>): CustomMatcherResult | Promise<CustomMatcherResult> => {
const errors: SchemaErrors = [];

if (schema.validate(received, errors)) {
return {
message: () => 'expected object schema is not validate',
pass: false,
};
}

return {
message: () => 'expected object schema is validate',
pass: true,
};
}
};

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace jest {

interface Matchers<R> {
toValidateSchema(schema: Schema<any>): R;
}
}
}

export {};
21 changes: 21 additions & 0 deletions backend/tests/Provider/IpLocate/IpLocateIo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {IpLocateData, IpLocateIo, SchemaIpLocateData} from '../../../src/inc/Provider/IpLocate/IpLocateIo';
import {SchemaMatchers} from '../../../src/types/ExpectSchema';

expect.extend(SchemaMatchers);

describe('testing ip locate IO by ip 8.8.8.8', () => {
const ip = '8.8.8.8';
let result: IpLocateData|null = null;

beforeAll(async() => {
result = await IpLocateIo.location(ip);
});

test('test result is not empty', async() => {
expect(result).toBeTruthy();
});

test('test result type by schema: SchemaIpLocateData', async() => {
expect(result).toValidateSchema(SchemaIpLocateData);
});
});
4 changes: 2 additions & 2 deletions backend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"sourceMap": true,
"strict": true,
"target": "ES2022",
"rootDir": "src"
"rootDir": "src",
},
"include": [
"./src/**/*.ts"
"./src/**/*.ts",
]
}
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,23 @@
"sshserver",
"frontend",
"ddnsserver",
"plugins"
"plugins",
"vpn"
],
"scripts": {
"npm-check-updates": "npm-check-updates"
},
"dependencies": {
"husky": "^9.0.11"
},
"devDependencies": {
"@babel/core": "^7.25.2",
"@babel/preset-env": "^7.25.4",
"@babel/preset-typescript": "^7.24.7",
"@types/jest": "^29.5.12",
"babel-jest": "^29.7.0",
"jest": "^29.7.0",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2"
}
}

0 comments on commit 2e84f56

Please sign in to comment.