增加测试代码

This commit is contained in:
CounterFire2023 2024-04-17 14:03:48 +08:00
parent f7b28b80b8
commit 9889c60903
5 changed files with 1830 additions and 28 deletions

5
jest.config.js Normal file
View File

@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
}

View File

@ -12,7 +12,8 @@
"lint": "eslint --ext .ts src/**",
"format": "eslint --ext .ts src/** --fix",
"initdata": "ts-node src/initdata.ts",
"test": "ts-node -r tsconfig-paths/register src/test.ts"
"test:watch": "jest --watch",
"test": "jest"
},
"author": "zhl",
"license": "ISC",
@ -37,7 +38,9 @@
"zutils": "link:packages/zutils"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@types/dotenv": "^8.2.0",
"@types/jest": "^29.5.12",
"@types/node": "16",
"@types/node-schedule": "^2.1.0",
"@types/redis": "^2.8.28",
@ -46,7 +49,9 @@
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"jest": "^29.7.0",
"prettier": "^3.2.3",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.1",
"ts-node-dev": "^2.0.0",
"tsconfig-paths": "^3.9.0",

View File

@ -1,3 +1,5 @@
import { BASE52_ALPHABET } from './Constants'
export const isObjectIdString = (str: string) => {
return /^[0-9a-fA-F]{24}$/.test(str)
}
@ -5,13 +7,14 @@ export const isObjectIdString = (str: string) => {
// check if a string is a valid share code
// alphabet:'3fBCM8j17XNA9xYun4wmLWep2oHFlhPcgyEJskqOz6GK0UtV5ZRaDSvrTbidQI'
export const isValidShareCode = (str: string) => {
return /^[3fBCM8j17XNA9xYun4wmLWep2oHFlhPcgyEJskqOz6GK0UtV5ZRaDSvrTbidQI]{10}$/.test(str)
let reg = new RegExp(`^[${BASE52_ALPHABET}]{10}$`)
return reg.test(str)
}
export const isValidVoucherCode = (str: string) => {
return /^[3fBCM8j17XNA9xYun4wmLWep2oHFlhPcgyEJskqOz6GK0UtV5ZRaDSvrTbidQI]{12}$/.test(str)
let reg = new RegExp(`^[${BASE52_ALPHABET}]{12}$`)
return reg.test(str)
}
export const formatNumShow = (num: number) => {
if (num >= 10) {
return Math.round(num) + ''

29
test/utils.test.ts Normal file
View File

@ -0,0 +1,29 @@
import { describe, expect } from '@jest/globals'
import { isValidVoucherCode, isValidShareCode } from '../src/common/Utils'
describe('Common Utils Tests', () => {
it('should return true for voucher code', async () => {
// create a mock for the User class
const code = 'testeBZ2leXC'
let result = isValidVoucherCode(code)
expect(result).toBe(true)
})
it('should return false for voucher code', async () => {
// create a mock for the User class
const code = 'testeBZ2le'
let result = isValidVoucherCode(code)
expect(result).toBe(false)
})
it('should return true for share code', async () => {
// create a mock for the User class
const code = 'testeBZ2le'
let result = isValidShareCode(code)
expect(result).toBe(true)
})
it('should return false for share code', async () => {
// create a mock for the User class
const code = 'testeBZ2leXC'
let result = isValidShareCode(code)
expect(result).toBe(false)
})
})

1808
yarn.lock

File diff suppressed because it is too large Load Diff