@lytjs/test-utils 4.2.0 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +33 -23
- package/README.md +0 -201
- package/dist/index.cjs +0 -7
- package/dist/index.mjs +0 -7
- package/dist/types/index.d.ts +0 -135
- package/dist/types/index.d.ts.map +0 -1
package/package.json
CHANGED
|
@@ -1,43 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lytjs/test-utils",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "6.0.0",
|
|
4
|
+
"description": "LytJS testing utilities for unit and integration testing",
|
|
5
|
+
"type": "module",
|
|
5
6
|
"main": "./dist/index.cjs",
|
|
6
7
|
"module": "./dist/index.mjs",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
7
9
|
"exports": {
|
|
8
10
|
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
9
12
|
"import": "./dist/index.mjs",
|
|
10
|
-
"require": "./dist/index.cjs"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
},
|
|
15
|
+
"./package.json": "./package.json"
|
|
13
16
|
},
|
|
14
|
-
"sideEffects": false,
|
|
15
17
|
"files": [
|
|
16
18
|
"dist"
|
|
17
19
|
],
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "echo 'Skipping test-utils build for now'",
|
|
23
|
+
"dev": "tsup --watch",
|
|
24
|
+
"test": "vitest run",
|
|
25
|
+
"test:watch": "vitest",
|
|
26
|
+
"test:coverage": "vitest run --coverage",
|
|
27
|
+
"type-check": "tsc --noEmit",
|
|
28
|
+
"lint": "eslint \"src/**/*.ts\"",
|
|
29
|
+
"clean": "rm -rf dist"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@lytjs/core": "^6.0.0",
|
|
33
|
+
"@lytjs/reactivity": "^6.0.0",
|
|
34
|
+
"@lytjs/component": "^6.0.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"tsup": "^8.0.0",
|
|
38
|
+
"typescript": "^5.4.0",
|
|
39
|
+
"vitest": "^3.0.0"
|
|
40
|
+
},
|
|
18
41
|
"license": "MIT",
|
|
19
|
-
"author": "lytjs",
|
|
20
42
|
"repository": {
|
|
21
43
|
"type": "git",
|
|
22
|
-
"url": "https://gitee.com/lytjs/lytjs"
|
|
44
|
+
"url": "https://gitee.com/lytjs/lytjs.git",
|
|
45
|
+
"directory": "packages/tools/packages/test-utils"
|
|
23
46
|
},
|
|
24
|
-
"homepage": "https://gitee.com/lytjs/lytjs",
|
|
25
47
|
"keywords": [
|
|
26
|
-
"lyt",
|
|
27
48
|
"lytjs",
|
|
28
|
-
"javascript",
|
|
29
|
-
"framework",
|
|
30
|
-
"frontend",
|
|
31
|
-
"vue-like",
|
|
32
|
-
"lightweight",
|
|
33
|
-
"zero-dependency",
|
|
34
49
|
"test",
|
|
35
50
|
"testing",
|
|
36
|
-
"utils"
|
|
37
|
-
|
|
38
|
-
"测试"
|
|
39
|
-
],
|
|
40
|
-
"publishConfig": {
|
|
41
|
-
"access": "public"
|
|
42
|
-
}
|
|
51
|
+
"utils"
|
|
52
|
+
]
|
|
43
53
|
}
|
package/README.md
DELETED
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
# @lytjs/test-utils
|
|
2
|
-
|
|
3
|
-
Lyt.js 测试工具库 - 提供测试支持的工具函数和组件。
|
|
4
|
-
|
|
5
|
-
## 安装
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install @lytjs/test-utils --save-dev
|
|
9
|
-
|
|
10
|
-
# 或使用 pnpm
|
|
11
|
-
pnpm add @lytjs/test-utils -D
|
|
12
|
-
```
|
|
13
|
-
|
|
14
|
-
## 特性
|
|
15
|
-
|
|
16
|
-
- 📦 组件挂载工具
|
|
17
|
-
- 🔍 元素查询
|
|
18
|
-
- 🎯 事件触发
|
|
19
|
-
- 💾 状态模拟
|
|
20
|
-
- 🚀 零运行时依赖
|
|
21
|
-
|
|
22
|
-
## 快速开始
|
|
23
|
-
|
|
24
|
-
```javascript
|
|
25
|
-
import { mount } from '@lytjs/test-utils';
|
|
26
|
-
import { defineComponent, ref } from '@lytjs/core';
|
|
27
|
-
|
|
28
|
-
const Counter = defineComponent({
|
|
29
|
-
setup() {
|
|
30
|
-
const count = ref(0);
|
|
31
|
-
const increment = () => count.value++;
|
|
32
|
-
return { count, increment };
|
|
33
|
-
},
|
|
34
|
-
template: `
|
|
35
|
-
<div>
|
|
36
|
-
<p>Count: {{ count }}</p>
|
|
37
|
-
<button @click="increment">Increment</button>
|
|
38
|
-
</div>
|
|
39
|
-
`
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
test('increments counter', () => {
|
|
43
|
-
const wrapper = mount(Counter);
|
|
44
|
-
const button = wrapper.find('button');
|
|
45
|
-
const p = wrapper.find('p');
|
|
46
|
-
|
|
47
|
-
expect(p.text()).toBe('Count: 0');
|
|
48
|
-
button.trigger('click');
|
|
49
|
-
expect(p.text()).toBe('Count: 1');
|
|
50
|
-
});
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
## API 参考
|
|
54
|
-
|
|
55
|
-
### mount
|
|
56
|
-
|
|
57
|
-
挂载组件进行测试
|
|
58
|
-
|
|
59
|
-
```javascript
|
|
60
|
-
import { mount } from '@lytjs/test-utils';
|
|
61
|
-
|
|
62
|
-
const wrapper = mount(Component, {
|
|
63
|
-
props: {},
|
|
64
|
-
slots: {},
|
|
65
|
-
attachTo: document.body
|
|
66
|
-
});
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
### shallowMount
|
|
70
|
-
|
|
71
|
-
浅层挂载组件,不渲染子组件
|
|
72
|
-
|
|
73
|
-
```javascript
|
|
74
|
-
import { shallowMount } from '@lytjs/test-utils';
|
|
75
|
-
|
|
76
|
-
const wrapper = shallowMount(Component);
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
### createTestingApp
|
|
80
|
-
|
|
81
|
-
创建测试用的应用实例
|
|
82
|
-
|
|
83
|
-
```javascript
|
|
84
|
-
import { createTestingApp } from '@lytjs/test-utils';
|
|
85
|
-
|
|
86
|
-
const app = createTestingApp(Component);
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
### Wrapper API
|
|
90
|
-
|
|
91
|
-
| 方法 | 说明 |
|
|
92
|
-
|------|------|
|
|
93
|
-
| `find(selector)` | 查找元素 |
|
|
94
|
-
| `findAll(selector)` | 查找所有元素 |
|
|
95
|
-
| `trigger(event)` | 触发事件 |
|
|
96
|
-
| `text()` | 获取文本内容 |
|
|
97
|
-
| `html()` | 获取 HTML |
|
|
98
|
-
| `isVisible()` | 检查是否可见 |
|
|
99
|
-
| `isEnabled()` | 检查是否启用 |
|
|
100
|
-
| `attributes()` | 获取属性 |
|
|
101
|
-
| `classes()` | 获取类名 |
|
|
102
|
-
| `setProps(props)` | 设置组件属性 |
|
|
103
|
-
| `vm` | 访问组件实例 |
|
|
104
|
-
|
|
105
|
-
## 示例
|
|
106
|
-
|
|
107
|
-
### 测试组件属性
|
|
108
|
-
|
|
109
|
-
```javascript
|
|
110
|
-
import { mount } from '@lytjs/test-utils';
|
|
111
|
-
import { defineComponent } from '@lytjs/core';
|
|
112
|
-
|
|
113
|
-
const Greeting = defineComponent({
|
|
114
|
-
props: ['name'],
|
|
115
|
-
template: '<p>Hello, {{ name }}!</p>'
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
test('renders with name', () => {
|
|
119
|
-
const wrapper = mount(Greeting, {
|
|
120
|
-
props: { name: 'Alice' }
|
|
121
|
-
});
|
|
122
|
-
expect(wrapper.text()).toBe('Hello, Alice!');
|
|
123
|
-
});
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
### 测试事件
|
|
127
|
-
|
|
128
|
-
```javascript
|
|
129
|
-
import { mount } from '@lytjs/test-utils';
|
|
130
|
-
import { defineComponent } from '@lytjs/core';
|
|
131
|
-
|
|
132
|
-
const Button = defineComponent({
|
|
133
|
-
emits: ['click'],
|
|
134
|
-
template: '<button @click="$emit('click')">Click</button>'
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
test('emits click event', () => {
|
|
138
|
-
const wrapper = mount(Button);
|
|
139
|
-
const button = wrapper.find('button');
|
|
140
|
-
button.trigger('click');
|
|
141
|
-
expect(wrapper.emitted('click')).toBeTruthy();
|
|
142
|
-
});
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
### 测试插槽
|
|
146
|
-
|
|
147
|
-
```javascript
|
|
148
|
-
import { mount } from '@lytjs/test-utils';
|
|
149
|
-
import { defineComponent } from '@lytjs/core';
|
|
150
|
-
|
|
151
|
-
const Card = defineComponent({
|
|
152
|
-
template: '<div><slot /></div>'
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
test('renders slot content', () => {
|
|
156
|
-
const wrapper = mount(Card, {
|
|
157
|
-
slots: {
|
|
158
|
-
default: '<p>Hello</p>'
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
expect(wrapper.text()).toBe('Hello');
|
|
162
|
-
});
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
### 测试响应式数据
|
|
166
|
-
|
|
167
|
-
```javascript
|
|
168
|
-
import { mount } from '@lytjs/test-utils';
|
|
169
|
-
import { defineComponent, ref } from '@lytjs/core';
|
|
170
|
-
|
|
171
|
-
const Form = defineComponent({
|
|
172
|
-
setup() {
|
|
173
|
-
const input = ref('');
|
|
174
|
-
return { input };
|
|
175
|
-
},
|
|
176
|
-
template: '<input v-model="input" />'
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
test('updates input value', async () => {
|
|
180
|
-
const wrapper = mount(Form);
|
|
181
|
-
const input = wrapper.find('input');
|
|
182
|
-
input.setValue('Hello');
|
|
183
|
-
expect(wrapper.vm.input).toBe('Hello');
|
|
184
|
-
});
|
|
185
|
-
```
|
|
186
|
-
|
|
187
|
-
## 性能
|
|
188
|
-
|
|
189
|
-
- 轻量级测试工具
|
|
190
|
-
- 零运行时依赖
|
|
191
|
-
- 快速的组件挂载
|
|
192
|
-
- 高效的测试执行
|
|
193
|
-
|
|
194
|
-
## 兼容性
|
|
195
|
-
|
|
196
|
-
- Node.js >= 18.0.0
|
|
197
|
-
- 支持主流测试框架(Jest、Vitest)
|
|
198
|
-
|
|
199
|
-
## License
|
|
200
|
-
|
|
201
|
-
MIT
|
package/dist/index.cjs
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
var j=Object.create;var $=Object.defineProperty;var A=Object.getOwnPropertyDescriptor;var B=Object.getOwnPropertyNames;var R=Object.getPrototypeOf,x=Object.prototype.hasOwnProperty;var L=(s,t)=>{for(var e in t)$(s,e,{get:t[e],enumerable:!0})},_=(s,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of B(t))!x.call(s,i)&&i!==e&&$(s,i,{get:()=>t[i],enumerable:!(r=A(t,i))||r.enumerable});return s};var T=(s,t,e)=>(e=s!=null?j(R(s)):{},_(t||!s||!s.__esModule?$(e,"default",{value:s,enumerable:!0}):e,s)),C=s=>_($({},"__esModule",{value:!0}),s);var D={};L(D,{Assertion:()=>p,afterEach:()=>G,beforeEach:()=>U,deepEqual:()=>d,describe:()=>N,expect:()=>J,it:()=>k,runAll:()=>W,skip:()=>I,test:()=>q,waitFor:()=>K});module.exports=C(D);var S=T(require("fs")),F=T(require("path")),w=[];var a=null;function N(s,t){let e={name:s,tests:[],beforeEachFn:[],afterEachFn:[]};w.push(e);let r=a;a=e;try{t()}finally{a=r}}function k(s,t){if(!a)throw new Error(`it() \u5FC5\u987B\u5728 describe() \u5185\u90E8\u8C03\u7528: "${s}"`);a.tests.push({name:s,fn:t})}function q(s,t){k(s,t)}function I(s,t){if(!a)throw new Error(`skip() \u5FC5\u987B\u5728 describe() \u5185\u90E8\u8C03\u7528: "${s}"`);a.tests.push({name:s,fn:()=>{throw new g(s)}})}var g=class extends Error{constructor(t){super(`SKIP: ${t}`),this.name="SkipError"}};function U(s){if(!a)throw new Error("beforeEach() \u5FC5\u987B\u5728 describe() \u5185\u90E8\u8C03\u7528");a.beforeEachFn.push(s)}function G(s){if(!a)throw new Error("afterEach() \u5FC5\u987B\u5728 describe() \u5185\u90E8\u8C03\u7528");a.afterEachFn.push(s)}var p=class{constructor(t){this.negated=!1;this.actual=t}get not(){return this.negated=!this.negated,this}toBe(t){let e=this.negated?!Object.is(this.actual,t):Object.is(this.actual,t);this._assert(e,`\u671F\u671B ${this._fmt(this.actual)} ${this.negated?"\u4E0D":""}\u7B49\u4E8E ${this._fmt(t)}`)}toEqual(t){let e=this.negated?!d(this.actual,t):d(this.actual,t);this._assert(e,`\u671F\u671B ${this._fmt(this.actual)} ${this.negated?"\u4E0D":""}\u6DF1\u5EA6\u7B49\u4E8E ${this._fmt(t)}`)}toBeTruthy(){let t=this.negated?!this.actual:!!this.actual;this._assert(t,`\u671F\u671B ${this._fmt(this.actual)} ${this.negated?"\u4E0D":""}\u4E3A\u771F\u503C`)}toBeFalsy(){let t=this.negated?!!this.actual:!this.actual;this._assert(t,`\u671F\u671B ${this._fmt(this.actual)} ${this.negated?"\u4E0D":""}\u4E3A\u5047\u503C`)}toBeNull(){let t=this.negated?this.actual!==null:this.actual===null;this._assert(t,`\u671F\u671B ${this._fmt(this.actual)} ${this.negated?"\u4E0D":""}\u4E3A null`)}toBeUndefined(){let t=this.negated?this.actual!==void 0:this.actual===void 0;this._assert(t,`\u671F\u671B ${this._fmt(this.actual)} ${this.negated?"\u4E0D":""}\u4E3A undefined`)}toBeDefined(){let t=this.negated?this.actual===void 0:this.actual!==void 0;this._assert(t,`\u671F\u671B\u503C ${this.negated?"\u4E0D":""}\u5DF2\u5B9A\u4E49`)}toThrow(t){let e=!1,r="";try{if(typeof this.actual=="function")this.actual();else throw new Error("toThrow() \u7684\u5B9E\u9645\u503C\u5FC5\u987B\u662F\u51FD\u6570")}catch(u){e=!0,r=u.message||String(u)}let i=this.negated?!e:e;i&&e&&t&&!this.negated&&(i=r.includes(t)),this._assert(i,`\u671F\u671B\u51FD\u6570${this.negated?"\u4E0D":""}\u629B\u51FA\u5F02\u5E38${t?` (\u5305\u542B "${t}")`:""}`)}toContain(t){let e;typeof this.actual=="string"?e=this.actual.includes(t):Array.isArray(this.actual)?e=this.actual.some(r=>d(r,t)):e=!1,this.negated&&(e=!e),this._assert(e,`\u671F\u671B ${this._fmt(this.actual)} ${this.negated?"\u4E0D":""}\u5305\u542B ${this._fmt(t)}`)}toBeGreaterThan(t){let e=this.negated?!(this.actual>t):this.actual>t;this._assert(e,`\u671F\u671B ${this._fmt(this.actual)} ${this.negated?"\u4E0D":""}\u5927\u4E8E ${t}`)}toBeLessThan(t){let e=this.negated?!(this.actual<t):this.actual<t;this._assert(e,`\u671F\u671B ${this._fmt(this.actual)} ${this.negated?"\u4E0D":""}\u5C0F\u4E8E ${t}`)}toBeGreaterThanOrEqual(t){let e=this.negated?!(this.actual>=t):this.actual>=t;this._assert(e,`\u671F\u671B ${this._fmt(this.actual)} ${this.negated?"\u4E0D":""}\u5927\u4E8E\u7B49\u4E8E ${t}`)}toBeLessThanOrEqual(t){let e=this.negated?!(this.actual<=t):this.actual<=t;this._assert(e,`\u671F\u671B ${this._fmt(this.actual)} ${this.negated?"\u4E0D":""}\u5C0F\u4E8E\u7B49\u4E8E ${t}`)}toHaveLength(t){var i;let e=(i=this.actual)==null?void 0:i.length,r=this.negated?e!==t:e===t;this._assert(r,`\u671F\u671B\u957F\u5EA6\u4E3A ${t}\uFF0C\u5B9E\u9645\u4E3A ${e}`)}_assert(t,e){if(!t)throw new v(e)}_fmt(t){if(t===null)return"null";if(t===void 0)return"undefined";if(typeof t=="string")return`"${t}"`;if(typeof t=="function")return"[Function]";if(Array.isArray(t))try{return JSON.stringify(t)}catch(e){return"[Array]"}if(typeof t=="object")try{return JSON.stringify(t)}catch(e){return"[Object]"}return String(t)}},v=class extends Error{constructor(t){super(t),this.name="AssertionError"}};function J(s){return new p(s)}function d(s,t){if(Object.is(s,t))return!0;if(s===null||t===null||typeof s!=typeof t)return!1;if(typeof s=="object"){let e=Object.keys(s),r=Object.keys(t);if(e.length!==r.length)return!1;for(let i of e)if(!Object.prototype.hasOwnProperty.call(t,i)||!d(s[i],t[i]))return!1;return!0}return!1}function K(s){return new Promise(t=>setTimeout(t,s))}var n={reset:"\x1B[0m",green:"\x1B[32m",red:"\x1B[31m",yellow:"\x1B[33m",cyan:"\x1B[36m",gray:"\x1B[90m",bold:"\x1B[1m"};async function W(){let s=[],t=0,e=0,r=0,i=0,u=[];console.log(`
|
|
2
|
-
${n.bold}${n.cyan}=== Lyt.js \u6D4B\u8BD5\u8FD0\u884C\u5668 ===${n.reset}
|
|
3
|
-
`);for(let l of w){console.log(`${n.bold}${l.name}${n.reset}`);for(let h of l.tests){t++;let O=performance.now();if(l.beforeEachFn)for(let o of l.beforeEachFn)try{o()}catch(b){console.warn(` ${n.yellow}[WARN] beforeEach \u51FA\u9519: ${b.message}${n.reset}`)}let f="passed",c;try{let o=h.fn();o instanceof Promise&&await o}catch(o){o instanceof g?(f="skipped",i++):(f="failed",r++,c=o,u.push(`Suite: ${l.name}, Test: ${h.name}, Error: ${(c==null?void 0:c.message)||"Unknown error"}`))}let m=performance.now()-O;if(l.afterEachFn)for(let o of l.afterEachFn)try{o()}catch(b){console.warn(` ${n.yellow}[WARN] afterEach \u51FA\u9519: ${b.message}${n.reset}`)}f==="passed"&&e++;let P={name:h.name,suite:l.name,status:f,error:c,duration:m};s.push(P);let y=m<1?"":` ${n.gray}(${m.toFixed(1)}ms)${n.reset}`;f==="passed"?console.log(` ${n.green}[PASS]${n.reset} ${h.name}${y}`):f==="skipped"?console.log(` ${n.yellow}[SKIP]${n.reset} ${h.name}${y}`):(console.log(` ${n.red}[FAIL]${n.reset} ${h.name}${y}`),c&&console.log(` ${n.red}${c.message}${n.reset}`))}console.log("")}let E=F.join(process.cwd(),"failed-tests.log");return S.writeFileSync(E,u.join(`
|
|
4
|
-
`),"utf8"),console.log(`\u5931\u8D25\u7684\u6D4B\u8BD5\u5DF2\u4FDD\u5B58\u5230 ${E}, \u5171 ${u.length} \u4E2A\u5931\u8D25
|
|
5
|
-
`),console.log(`${n.bold}=== \u6D4B\u8BD5\u7ED3\u679C ===${n.reset}`),console.log(` \u603B\u8BA1: ${t}`),console.log(` ${n.green}\u901A\u8FC7: ${e}${n.reset}`),r>0&&console.log(` ${n.red}\u5931\u8D25: ${r}${n.reset}`),i>0&&console.log(` ${n.yellow}\u8DF3\u8FC7: ${i}${n.reset}`),console.log(""),console.log(r===0?`${n.green}${n.bold}\u6240\u6709\u6D4B\u8BD5\u901A\u8FC7!${n.reset}
|
|
6
|
-
`:`${n.red}${n.bold}\u5B58\u5728\u5931\u8D25\u7684\u6D4B\u8BD5!${n.reset}
|
|
7
|
-
`),w.length=0,{total:t,passed:e,failed:r,skipped:i,results:s}}0&&(module.exports={Assertion,afterEach,beforeEach,deepEqual,describe,expect,it,runAll,skip,test,waitFor});
|
package/dist/index.mjs
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import*as E from"fs";import*as _ from"path";var y=[];var a=null;function k(n,t){let e={name:n,tests:[],beforeEachFn:[],afterEachFn:[]};y.push(e);let i=a;a=e;try{t()}finally{a=i}}function F(n,t){if(!a)throw new Error(`it() \u5FC5\u987B\u5728 describe() \u5185\u90E8\u8C03\u7528: "${n}"`);a.tests.push({name:n,fn:t})}function O(n,t){F(n,t)}function P(n,t){if(!a)throw new Error(`skip() \u5FC5\u987B\u5728 describe() \u5185\u90E8\u8C03\u7528: "${n}"`);a.tests.push({name:n,fn:()=>{throw new $(n)}})}var $=class extends Error{constructor(t){super(`SKIP: ${t}`),this.name="SkipError"}};function j(n){if(!a)throw new Error("beforeEach() \u5FC5\u987B\u5728 describe() \u5185\u90E8\u8C03\u7528");a.beforeEachFn.push(n)}function A(n){if(!a)throw new Error("afterEach() \u5FC5\u987B\u5728 describe() \u5185\u90E8\u8C03\u7528");a.afterEachFn.push(n)}var b=class{constructor(t){this.negated=!1;this.actual=t}get not(){return this.negated=!this.negated,this}toBe(t){let e=this.negated?!Object.is(this.actual,t):Object.is(this.actual,t);this._assert(e,`\u671F\u671B ${this._fmt(this.actual)} ${this.negated?"\u4E0D":""}\u7B49\u4E8E ${this._fmt(t)}`)}toEqual(t){let e=this.negated?!d(this.actual,t):d(this.actual,t);this._assert(e,`\u671F\u671B ${this._fmt(this.actual)} ${this.negated?"\u4E0D":""}\u6DF1\u5EA6\u7B49\u4E8E ${this._fmt(t)}`)}toBeTruthy(){let t=this.negated?!this.actual:!!this.actual;this._assert(t,`\u671F\u671B ${this._fmt(this.actual)} ${this.negated?"\u4E0D":""}\u4E3A\u771F\u503C`)}toBeFalsy(){let t=this.negated?!!this.actual:!this.actual;this._assert(t,`\u671F\u671B ${this._fmt(this.actual)} ${this.negated?"\u4E0D":""}\u4E3A\u5047\u503C`)}toBeNull(){let t=this.negated?this.actual!==null:this.actual===null;this._assert(t,`\u671F\u671B ${this._fmt(this.actual)} ${this.negated?"\u4E0D":""}\u4E3A null`)}toBeUndefined(){let t=this.negated?this.actual!==void 0:this.actual===void 0;this._assert(t,`\u671F\u671B ${this._fmt(this.actual)} ${this.negated?"\u4E0D":""}\u4E3A undefined`)}toBeDefined(){let t=this.negated?this.actual===void 0:this.actual!==void 0;this._assert(t,`\u671F\u671B\u503C ${this.negated?"\u4E0D":""}\u5DF2\u5B9A\u4E49`)}toThrow(t){let e=!1,i="";try{if(typeof this.actual=="function")this.actual();else throw new Error("toThrow() \u7684\u5B9E\u9645\u503C\u5FC5\u987B\u662F\u51FD\u6570")}catch(u){e=!0,i=u.message||String(u)}let r=this.negated?!e:e;r&&e&&t&&!this.negated&&(r=i.includes(t)),this._assert(r,`\u671F\u671B\u51FD\u6570${this.negated?"\u4E0D":""}\u629B\u51FA\u5F02\u5E38${t?` (\u5305\u542B "${t}")`:""}`)}toContain(t){let e;typeof this.actual=="string"?e=this.actual.includes(t):Array.isArray(this.actual)?e=this.actual.some(i=>d(i,t)):e=!1,this.negated&&(e=!e),this._assert(e,`\u671F\u671B ${this._fmt(this.actual)} ${this.negated?"\u4E0D":""}\u5305\u542B ${this._fmt(t)}`)}toBeGreaterThan(t){let e=this.negated?!(this.actual>t):this.actual>t;this._assert(e,`\u671F\u671B ${this._fmt(this.actual)} ${this.negated?"\u4E0D":""}\u5927\u4E8E ${t}`)}toBeLessThan(t){let e=this.negated?!(this.actual<t):this.actual<t;this._assert(e,`\u671F\u671B ${this._fmt(this.actual)} ${this.negated?"\u4E0D":""}\u5C0F\u4E8E ${t}`)}toBeGreaterThanOrEqual(t){let e=this.negated?!(this.actual>=t):this.actual>=t;this._assert(e,`\u671F\u671B ${this._fmt(this.actual)} ${this.negated?"\u4E0D":""}\u5927\u4E8E\u7B49\u4E8E ${t}`)}toBeLessThanOrEqual(t){let e=this.negated?!(this.actual<=t):this.actual<=t;this._assert(e,`\u671F\u671B ${this._fmt(this.actual)} ${this.negated?"\u4E0D":""}\u5C0F\u4E8E\u7B49\u4E8E ${t}`)}toHaveLength(t){var r;let e=(r=this.actual)==null?void 0:r.length,i=this.negated?e!==t:e===t;this._assert(i,`\u671F\u671B\u957F\u5EA6\u4E3A ${t}\uFF0C\u5B9E\u9645\u4E3A ${e}`)}_assert(t,e){if(!t)throw new w(e)}_fmt(t){if(t===null)return"null";if(t===void 0)return"undefined";if(typeof t=="string")return`"${t}"`;if(typeof t=="function")return"[Function]";if(Array.isArray(t))try{return JSON.stringify(t)}catch(e){return"[Array]"}if(typeof t=="object")try{return JSON.stringify(t)}catch(e){return"[Object]"}return String(t)}},w=class extends Error{constructor(t){super(t),this.name="AssertionError"}};function B(n){return new b(n)}function d(n,t){if(Object.is(n,t))return!0;if(n===null||t===null||typeof n!=typeof t)return!1;if(typeof n=="object"){let e=Object.keys(n),i=Object.keys(t);if(e.length!==i.length)return!1;for(let r of e)if(!Object.prototype.hasOwnProperty.call(t,r)||!d(n[r],t[r]))return!1;return!0}return!1}function R(n){return new Promise(t=>setTimeout(t,n))}var s={reset:"\x1B[0m",green:"\x1B[32m",red:"\x1B[31m",yellow:"\x1B[33m",cyan:"\x1B[36m",gray:"\x1B[90m",bold:"\x1B[1m"};async function x(){let n=[],t=0,e=0,i=0,r=0,u=[];console.log(`
|
|
2
|
-
${s.bold}${s.cyan}=== Lyt.js \u6D4B\u8BD5\u8FD0\u884C\u5668 ===${s.reset}
|
|
3
|
-
`);for(let l of y){console.log(`${s.bold}${l.name}${s.reset}`);for(let h of l.tests){t++;let T=performance.now();if(l.beforeEachFn)for(let o of l.beforeEachFn)try{o()}catch(m){console.warn(` ${s.yellow}[WARN] beforeEach \u51FA\u9519: ${m.message}${s.reset}`)}let f="passed",c;try{let o=h.fn();o instanceof Promise&&await o}catch(o){o instanceof $?(f="skipped",r++):(f="failed",i++,c=o,u.push(`Suite: ${l.name}, Test: ${h.name}, Error: ${(c==null?void 0:c.message)||"Unknown error"}`))}let g=performance.now()-T;if(l.afterEachFn)for(let o of l.afterEachFn)try{o()}catch(m){console.warn(` ${s.yellow}[WARN] afterEach \u51FA\u9519: ${m.message}${s.reset}`)}f==="passed"&&e++;let S={name:h.name,suite:l.name,status:f,error:c,duration:g};n.push(S);let p=g<1?"":` ${s.gray}(${g.toFixed(1)}ms)${s.reset}`;f==="passed"?console.log(` ${s.green}[PASS]${s.reset} ${h.name}${p}`):f==="skipped"?console.log(` ${s.yellow}[SKIP]${s.reset} ${h.name}${p}`):(console.log(` ${s.red}[FAIL]${s.reset} ${h.name}${p}`),c&&console.log(` ${s.red}${c.message}${s.reset}`))}console.log("")}let v=_.join(process.cwd(),"failed-tests.log");return E.writeFileSync(v,u.join(`
|
|
4
|
-
`),"utf8"),console.log(`\u5931\u8D25\u7684\u6D4B\u8BD5\u5DF2\u4FDD\u5B58\u5230 ${v}, \u5171 ${u.length} \u4E2A\u5931\u8D25
|
|
5
|
-
`),console.log(`${s.bold}=== \u6D4B\u8BD5\u7ED3\u679C ===${s.reset}`),console.log(` \u603B\u8BA1: ${t}`),console.log(` ${s.green}\u901A\u8FC7: ${e}${s.reset}`),i>0&&console.log(` ${s.red}\u5931\u8D25: ${i}${s.reset}`),r>0&&console.log(` ${s.yellow}\u8DF3\u8FC7: ${r}${s.reset}`),console.log(""),console.log(i===0?`${s.green}${s.bold}\u6240\u6709\u6D4B\u8BD5\u901A\u8FC7!${s.reset}
|
|
6
|
-
`:`${s.red}${s.bold}\u5B58\u5728\u5931\u8D25\u7684\u6D4B\u8BD5!${s.reset}
|
|
7
|
-
`),y.length=0,{total:t,passed:e,failed:i,skipped:r,results:n}}export{b as Assertion,A as afterEach,j as beforeEach,d as deepEqual,k as describe,B as expect,F as it,x as runAll,P as skip,O as test,R as waitFor};
|
package/dist/types/index.d.ts
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
interface TestCase {
|
|
2
|
-
name: string;
|
|
3
|
-
fn: () => void | Promise<void>;
|
|
4
|
-
}
|
|
5
|
-
interface TestResult {
|
|
6
|
-
name: string;
|
|
7
|
-
suite: string;
|
|
8
|
-
status: 'passed' | 'failed' | 'skipped';
|
|
9
|
-
error?: Error;
|
|
10
|
-
duration: number;
|
|
11
|
-
}
|
|
12
|
-
interface TestSuite {
|
|
13
|
-
name: string;
|
|
14
|
-
tests: TestCase[];
|
|
15
|
-
beforeEachFn?: (() => void)[];
|
|
16
|
-
afterEachFn?: (() => void)[];
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* 定义测试套件
|
|
20
|
-
*
|
|
21
|
-
* @param name 测试套件名称
|
|
22
|
-
* @param fn 定义函数,内部使用 it/test 注册测试用例
|
|
23
|
-
*/
|
|
24
|
-
declare function describe(name: string, fn: () => void): void;
|
|
25
|
-
/**
|
|
26
|
-
* 注册测试用例
|
|
27
|
-
*
|
|
28
|
-
* @param name 测试名称
|
|
29
|
-
* @param fn 测试函数
|
|
30
|
-
*/
|
|
31
|
-
declare function it(name: string, fn: () => void | Promise<void>): void;
|
|
32
|
-
/** it 的别名 */
|
|
33
|
-
declare function test(name: string, fn: () => void | Promise<void>): void;
|
|
34
|
-
/**
|
|
35
|
-
* 跳过测试用例
|
|
36
|
-
*
|
|
37
|
-
* @param name 测试名称
|
|
38
|
-
* @param fn 测试函数(不会执行)
|
|
39
|
-
*/
|
|
40
|
-
declare function skip(name: string, fn: () => void | Promise<void>): void;
|
|
41
|
-
/**
|
|
42
|
-
* 注册前置钩子
|
|
43
|
-
*
|
|
44
|
-
* @param fn 每个测试用例执行前调用的函数
|
|
45
|
-
*/
|
|
46
|
-
declare function beforeEach(fn: () => void): void;
|
|
47
|
-
/**
|
|
48
|
-
* 注册后置钩子
|
|
49
|
-
*
|
|
50
|
-
* @param fn 每个测试用例执行后调用的函数
|
|
51
|
-
*/
|
|
52
|
-
declare function afterEach(fn: () => void): void;
|
|
53
|
-
/**
|
|
54
|
-
* Assertion 断言类
|
|
55
|
-
*
|
|
56
|
-
* 支持链式调用和 .not 取反。
|
|
57
|
-
*/
|
|
58
|
-
declare class Assertion {
|
|
59
|
-
private actual;
|
|
60
|
-
private negated;
|
|
61
|
-
constructor(actual: any);
|
|
62
|
-
/** 取反后续断言 */
|
|
63
|
-
get not(): Assertion;
|
|
64
|
-
/** 断言严格相等 (===) */
|
|
65
|
-
toBe(expected: any): void;
|
|
66
|
-
/** 断言深度相等 */
|
|
67
|
-
toEqual(expected: any): void;
|
|
68
|
-
/** 断言为真值 */
|
|
69
|
-
toBeTruthy(): void;
|
|
70
|
-
/** 断言为假值 */
|
|
71
|
-
toBeFalsy(): void;
|
|
72
|
-
/** 断言为 null */
|
|
73
|
-
toBeNull(): void;
|
|
74
|
-
/** 断言为 undefined */
|
|
75
|
-
toBeUndefined(): void;
|
|
76
|
-
/** 断言不为 undefined(即已定义) */
|
|
77
|
-
toBeDefined(): void;
|
|
78
|
-
/** 断言函数抛出异常 */
|
|
79
|
-
toThrow(message?: string): void;
|
|
80
|
-
/** 断言数组/字符串包含指定项 */
|
|
81
|
-
toContain(item: any): void;
|
|
82
|
-
/** 断言数值大于 n */
|
|
83
|
-
toBeGreaterThan(n: number): void;
|
|
84
|
-
/** 断言数值小于 n */
|
|
85
|
-
toBeLessThan(n: number): void;
|
|
86
|
-
/** 断言数值大于等于 n */
|
|
87
|
-
toBeGreaterThanOrEqual(n: number): void;
|
|
88
|
-
/** 断言数值小于等于 n */
|
|
89
|
-
toBeLessThanOrEqual(n: number): void;
|
|
90
|
-
/** 断言数组/字符串长度为 n */
|
|
91
|
-
toHaveLength(n: number): void;
|
|
92
|
-
/** 内部断言方法 */
|
|
93
|
-
private _assert;
|
|
94
|
-
/** 格式化值用于错误消息 */
|
|
95
|
-
private _fmt;
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* 创建断言
|
|
99
|
-
*
|
|
100
|
-
* @param value 要断言的值
|
|
101
|
-
* @returns Assertion 实例
|
|
102
|
-
*/
|
|
103
|
-
declare function expect(value: any): Assertion;
|
|
104
|
-
/**
|
|
105
|
-
* 深度比较两个值
|
|
106
|
-
*
|
|
107
|
-
* @param a 第一个值
|
|
108
|
-
* @param b 第二个值
|
|
109
|
-
* @returns 是否深度相等
|
|
110
|
-
*/
|
|
111
|
-
declare function deepEqual(a: any, b: any): boolean;
|
|
112
|
-
/**
|
|
113
|
-
* 等待指定毫秒数
|
|
114
|
-
*
|
|
115
|
-
* @param ms 等待时间(毫秒)
|
|
116
|
-
* @returns Promise
|
|
117
|
-
*/
|
|
118
|
-
declare function waitFor(ms: number): Promise<void>;
|
|
119
|
-
/**
|
|
120
|
-
* 运行所有已注册的测试套件
|
|
121
|
-
*
|
|
122
|
-
* 按注册顺序执行所有 describe 中的测试用例,
|
|
123
|
-
* 收集结果并输出格式化报告。
|
|
124
|
-
*
|
|
125
|
-
* @returns 测试结果汇总
|
|
126
|
-
*/
|
|
127
|
-
declare function runAll(): Promise<{
|
|
128
|
-
total: number;
|
|
129
|
-
passed: number;
|
|
130
|
-
failed: number;
|
|
131
|
-
skipped: number;
|
|
132
|
-
results: TestResult[];
|
|
133
|
-
}>;
|
|
134
|
-
export { describe, it, test, skip, beforeEach, afterEach, expect, Assertion, deepEqual, waitFor, runAll, type TestCase, type TestResult, type TestSuite, };
|
|
135
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"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"}
|