Skip to content

Commit

Permalink
feat: 补充单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
D-xuanmo committed Jul 8, 2023
1 parent 363cfa9 commit 8987d70
Show file tree
Hide file tree
Showing 20 changed files with 491 additions and 9 deletions.
20 changes: 15 additions & 5 deletions .github/workflows/node.js.yml → .github/workflows/build.yml
Expand Up @@ -7,7 +7,7 @@ on:
- '.github/workflows/node.js.yml'

jobs:
publish-npm:
build:
runs-on: macos-latest

# 任务的步骤
Expand All @@ -23,8 +23,18 @@ jobs:

- run: pnpm i
- run: pnpm build
- run: npm publish --access public

env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
test:
needs: build
runs-on: ubuntu-latest
steps:
- run: pnpm test

publish:
needs: build
runs-on: ubuntu-latest
steps:
- run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -21,3 +21,5 @@ dist
*.njsproj
*.sln
*.sw?

test/__snapshots__
6 changes: 4 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "@xuanmo/validator",
"version": "0.0.14",
"version": "0.0.15",
"description": "用最少的代码,解决繁琐的事情",
"private": false,
"main": "dist/validator.cjs.js",
Expand All @@ -13,7 +13,8 @@
"build": "cross-env NODE_ENV=prod npm run clear && rollup -c --bundleConfigAsCjs",
"clear": "rimraf dist && rimraf rules",
"lint": "eslint . --fix",
"test": "vitest"
"test": "vitest",
"test:ui": "vitest --ui"
},
"keywords": [
"validator",
Expand All @@ -40,6 +41,7 @@
"@rollup/plugin-typescript": "^11.0.0",
"@typescript-eslint/eslint-plugin": "^5.56.0",
"@typescript-eslint/parser": "^5.56.0",
"@vitest/ui": "^0.33.0",
"cross-env": "^7.0.3",
"eslint": "^8.36.0",
"husky": "^8.0.3",
Expand Down
61 changes: 59 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions test/alpha.test.js
@@ -0,0 +1,35 @@
import { expect, test } from 'vitest'
import { validator } from '../dist/validator.esm'
import zhCN from '../locale/zh-CN.json'

validator.localize(zhCN)

test('alpha', async () => {
// 校验通过
await expect(validator.validate([
{
dataKey: 'alpha1',
value: 'abc',
rules: 'alpha'
}
])).resolves.toBe(true)

// 校验未通过
await expect(validator.validate([
{
dataKey: 'alpha2',
value: '1',
rules: 'alpha'
},
{
dataKey: 'alpha3',
value: 'a1',
rules: 'alpha'
},
{
dataKey: 'alpha4',
value: 'a_1',
rules: 'alpha'
},
])).rejects.toMatchSnapshot()
})
35 changes: 35 additions & 0 deletions test/alpha_num.test.js
@@ -0,0 +1,35 @@
import { expect, test } from 'vitest'
import { validator } from '../dist/validator.esm'
import zhCN from '../locale/zh-CN.json'

validator.localize(zhCN)

test('alpha_num', async () => {
// 校验通过
await expect(validator.validate([
{
dataKey: 'alpha_num1',
value: 'abc',
rules: 'alpha_num'
},
{
dataKey: 'alpha_num2',
value: 'abc1',
rules: 'alpha_num'
},
])).resolves.toBe(true)

// 校验未通过
await expect(validator.validate([
{
dataKey: 'alpha_num3',
value: 'abc_1',
rules: 'alpha_num'
},
{
dataKey: 'alpha_num4',
value: '错误',
rules: 'alpha_num'
},
])).rejects.toMatchSnapshot()
})
35 changes: 35 additions & 0 deletions test/alpha_spaces.test.js
@@ -0,0 +1,35 @@
import { expect, test } from 'vitest'
import { validator } from '../dist/validator.esm'
import zhCN from '../locale/zh-CN.json'

validator.localize(zhCN)

test('alpha_spaces', async () => {
// 校验通过
await expect(validator.validate([
{
dataKey: 'alpha_spaces1',
value: 'abc',
rules: 'alpha_spaces'
},
{
dataKey: 'alpha_spaces2',
value: 'abc d',
rules: 'alpha_spaces'
},
])).resolves.toBe(true)

// 校验未通过
await expect(validator.validate([
{
dataKey: 'alpha_spaces3',
value: 'abc_1',
rules: 'alpha_spaces'
},
{
dataKey: 'alpha_spaces4',
value: '错误',
rules: 'alpha_spaces'
},
])).rejects.toMatchSnapshot()
})
28 changes: 28 additions & 0 deletions test/between.test.js
@@ -0,0 +1,28 @@
import { expect, test } from 'vitest'
import { validator } from '../dist/validator.esm'
import zhCN from '../locale/zh-CN.json'

validator.localize(zhCN)

test('between', async () => {
await expect(validator.validate([
{
dataKey: 'between1',
value: 22,
rules: 'between:0,22'
}
])).resolves.toBe(true)

await expect(validator.validate([
{
dataKey: 'between2',
value: '3q',
rules: 'between:2,2'
},
{
dataKey: 'between3',
value: 100,
rules: 'between:2,20'
},
])).rejects.toMatchSnapshot()
})
33 changes: 33 additions & 0 deletions test/confirmed.test.js
@@ -0,0 +1,33 @@
import { expect, test } from 'vitest'
import { validator } from '../dist/validator.esm'
import zhCN from '../locale/zh-CN.json'

validator.localize(zhCN)

test('confirmed', async () => {
// 校验通过
await expect(validator.validate([
{
dataKey: 'confirmed1',
value: '123456'
},
{
dataKey: 'confirmed2',
value: '123456',
rules: 'confirmed:confirmed1'
},
])).resolves.toBe(true)

// 校验未通过
await expect(validator.validate([
{
dataKey: 'confirmed3',
value: '123456'
},
{
dataKey: 'confirmed4',
value: '1',
rules: 'confirmed:confirmed3'
},
])).rejects.toMatchSnapshot()
})
23 changes: 23 additions & 0 deletions test/email.test.js
@@ -0,0 +1,23 @@
import { expect, test } from 'vitest'
import { validator } from '../dist/validator.esm'
import zhCN from '../locale/zh-CN.json'

validator.localize(zhCN)

test('email', async () => {
await expect(validator.validate([
{
dataKey: 'email1',
value: 'me@xuanmo.xin',
rules: 'email'
}
])).resolves.toBe(true)

await expect(validator.validate([
{
dataKey: 'email2',
value: 'me',
rules: 'email'
}
])).rejects.toMatchSnapshot()
})

0 comments on commit 8987d70

Please sign in to comment.