Skip to content

Commit 417bee5

Browse files
committed
feat: 1.0.2
1 parent dcccb0e commit 417bee5

12 files changed

+86
-32
lines changed

helper/version.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@
2929
### 1.0.1
3030

3131
1. 删除 asyncPlugin, 合并到defaultPlugin
32-
2. 增加 diaabled 参数,可以动态控制禁用某些测试用例
32+
2. 增加 disabled 参数,可以动态控制禁用某些测试用例
33+
34+
### 1.0.2
35+
36+
1. 修复测试用例耗时不准的bug
37+
2. 修复disabled参数导致测试无法complete的bug

npm/easy-test-lib.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

npm/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "easy-test-lib",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "A simple and easy-to-use testing lib",
55
"main": "easy-test-lib.min.js",
66
"bin": {

npm/type.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export interface ITestPluginReturn {
4141
passed: boolean;
4242
result: any;
4343
expect?: any;
44+
disabled?: boolean;
4445
}
4546

4647
export interface IOnTestSingleOption extends ITestPluginReturn {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "easy-test-lib",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "A simple and easy-to-use testing lib",
55
"scripts": {
66
"dev": "webpack-dev-server --open --config webpack-config/dev.js",

public/cases/test-args.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ const config: ITestConfigItem[] = [{
2121
name: '测试this',
2222
args: {caseArg: {a: 1}},
2323
test () {
24-
return [this.name, this.args.caseArg];
24+
// return [this.name, this.args.caseArg];
25+
// console.log(this.expect);
26+
return this.expect;
2527
},
26-
expect: ['测试this', {a: 1}]
28+
// expect: ['测试this', {a: 1}]
29+
expect: window.Symbol(111)
2730
}];
2831

2932
export default config;

public/cases/test-default.ts

+10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ const defaultCase: ITestConfigItem[] = [{
2121
expect: [
2222
11
2323
]
24+
}, {
25+
name: '测试Symbol',
26+
args: {caseArg: {a: 1}},
27+
test () {
28+
// return [this.name, this.args.caseArg];
29+
// console.log('*****************', this.name, this.expect);
30+
return this.expect;
31+
},
32+
// expect: ['测试this', {a: 1}]
33+
expect: window.Symbol(111)
2434
}];
2535

2636
export default defaultCase;

public/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ startTest({
1818
...testDefault,
1919
testAdd,
2020
testDom,
21+
testAsync,
2122
...testObject,
2223
...testArgs,
23-
testAsync,
2424
],
2525
onTestComplete (result) {
2626
console.log(`总耗时(${result.time})ms; 结果:${result.passed ? '通过' : '失败'}`);
2727
console.log(result);
2828
},
2929
onTestSingle (result) {
30+
// console.log(result.result);
3031
console.log(`${result.name}[${result.index}]: 耗时(${result.time})ms; 结果:${result.passed ? '通过' : '失败'}`);
3132
if (!result.passed) {
3233
console.warn(result);

src/default-plugin/index.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,25 @@ import {
44
} from '../type';
55
import {isValueEqual} from '../util';
66

7-
const plugin: ITestPlugin = async (item, mergedArgs) => {
8-
const {test, expect} = item;
9-
let result = test.call(item, mergedArgs);
10-
if (result instanceof Promise) {
11-
result = await result;
12-
}
7+
function buildResult (result: any, expect: any) {
138
return {
149
result,
1510
expect,
1611
passed: isValueEqual(result, expect),
1712
};
13+
}
14+
15+
const plugin: ITestPlugin = (item, mergedArgs) => {
16+
const {test, expect} = item;
17+
const result = test.call(item, mergedArgs);
18+
if (result instanceof Promise) {
19+
return new Promise((resolve) => {
20+
result.then(data => {
21+
resolve(buildResult(data, expect));
22+
});
23+
});
24+
}
25+
return buildResult(result, expect);
1826
};
1927

2028
export default plugin;

src/index.ts

+43-18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ITestPlugin,
55
ITestConfigItem,
66
ITestPluginConfigItem,
7+
ITestPluginReturn,
78
} from './type';
89

910
import defaultTestPlugin from './default-plugin';
@@ -20,30 +21,54 @@ export const startTest:IStartTest = ({
2021
const onTestProcess = createTestProcess(onTestSingle, onTestComplete);
2122
const length = cases.length;
2223
let testedNum = 0;
23-
cases.forEach(async (item, index) => {
24-
if (item.disabled) return;
25-
const mergedArgs = mergeArgs(args, item.args);
26-
if (typeof item.test !== 'function') {
27-
item.test = () => item.test; // 支持test传入非函数
28-
} else {
29-
item.test = item.test.bind(item);
30-
}
31-
if (typeof item.expect === 'function') { // 支持expect传入函数
32-
item.expect = item.expect.call(item, mergedArgs);
33-
}
34-
const startTime = new Date().getTime();
35-
let result = pickPlugin(item, plugin)(item as ITestPluginConfigItem, mergedArgs);
36-
if (result instanceof Promise) { // 兼容 async plugin
37-
result = await result;
38-
}
24+
25+
const trigTestProcess = ({
26+
index, time, result, name = ''
27+
}: {
28+
index: number, time: number, result: ITestPluginReturn, name?: string,
29+
}) => {
3930
testedNum ++;
4031
const singleOption: IOnTestSingleOption = {
4132
...result,
4233
index,
43-
time: countTime(startTime),
34+
time,
4435
};
45-
if (item.name) {singleOption.name = item.name;}
36+
if (name) singleOption.name = name;
4637
onTestProcess(singleOption, testedNum === length);
38+
};
39+
40+
cases.forEach(async (item, index) => {
41+
let time: number, result: ITestPluginReturn | Promise<ITestPluginReturn>;
42+
if (item.disabled) {
43+
time = 0;
44+
result = {
45+
passed: true,
46+
disabled: true,
47+
result: '',
48+
};
49+
} else {
50+
const startTime = Date.now();
51+
const mergedArgs = mergeArgs(args, item.args);
52+
if (typeof item.test !== 'function') {
53+
item.test = () => item.test; // 支持test传入非函数
54+
} else {
55+
item.test = item.test.bind(item);
56+
}
57+
if (typeof item.expect === 'function') { // 支持expect传入函数
58+
item.expect = item.expect.call(item, mergedArgs);
59+
}
60+
result = pickPlugin(item, plugin)(item as ITestPluginConfigItem, mergedArgs);
61+
if (result instanceof Promise) { // 兼容 async plugin
62+
result = await result;
63+
}
64+
time = countTime(startTime);
65+
}
66+
trigTestProcess({
67+
name: item.name,
68+
index,
69+
time,
70+
result,
71+
});
4772
});
4873
};
4974

src/type.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export interface ITestPluginReturn {
4141
passed: boolean;
4242
result: any;
4343
expect?: any;
44+
disabled?: boolean;
4445
}
4546

4647
export interface IOnTestSingleOption extends ITestPluginReturn {

src/util.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function objectEqualBaseSingle (resultItem: any, expectItem: any) {
7272
}
7373

7474
export function countTime (startTime: number): number {
75-
return new Date().getTime() - startTime;
75+
return Date.now() - startTime;
7676
}
7777

7878
export function mergeArgs (global: any, local: any) {

0 commit comments

Comments
 (0)