ไธญๆ | Online Use | Feedback | Gitee
- Typescript writing
- Small size and easy to use
- Multi-terminal support
- Support asynchronous
- Customizable plugins
- Configuration file + command line operation
- Global installation available
npm i easy-test-lib -D
const {startTest} = require('easy-test-lib');
startTest(config);
package.json added
...
"scripts": {
"test": "etest <config file>"
},
...
The configuration file defaults to the easy.test.js
file in the root directory, which can be configured freely
Root directory execution
npm run test
npm i easy-test-lib -g
The configuration file is consistent with the rules in 2.2
Run the following command line in the project directory
etest <config file>
<script src="https://cdn.jsdelivr.net/npm/easy-test-lib/easy-test-lib.min.js"></script>
<script>
ETest.startTest({
// ...
})
</script>
const {startTest} = require('easy-test-lib');
function add (x, y) {
return x + y;
}
startTest({
args: {// optional parameters
// Used to pass in some public apis, which will be passed into test cases
},
cases: [// Test case configuration, it is recommended to split files
{
name:'Test add function', // optional
disabled: false, // optional Whether to disable the current use case
args: {// optional
// The api of the current test case
},
test (mergedArgs) {
// mergedArg is a combination of public arg and use case arg, in addition to mergedArg, there are two attributes: $global and $local
// this refers to the current test case
return add(1, 1);
},
expect: 2,
// plugin: ITestPlugin, // Plug-in used by the current test case optional
}
],
onTestComplete (result) {// All tests are completed callback optional
// result data structure is as follows
/*
passed: boolean;
results: [
{
passed: boolean;
result: any;
expect?: any;
name?: string;
index: number;
time: number;
}
];
time: number;
*/
},
onTestSingle (result) {// Single test case completion callback optional
// result data structure is as follows
/*
passed: boolean;
result: any;
expect?: any;
name?: string;
index: number;
time: number;
*/
},
// plugin: ITestPlugin, // global plugin optional
});
easy-test-lib has a built-in default plugin (defaultPlugin) 1.0.1 and later versions. The asynchronous plugin function is merged into the default plugin
The following is a test case using asynchronous
function timeout (time) {
return new Promise(resolve => {
setTimeout(() => {
resolve(true);
}, time);
});
}
const asyncCase = {
args: {aa: 22},
name:'Test async',
async test (args: any) { // Or return a Promise object
await timeout(2000);
console.log(args, this.args);
return [
11
];
},
expect: [
11
]
};
module.exports = asyncCase;
Run test cases
const {startTest} = require('easy-test-lib');
const testAsync = require('./test-async');
startTest({
cases: [
testAsync
],
onTestComplete (result) {
console.log(`Total time (${result.time}) ms; result: ${result.passed?'Passed':'Failed'}`);
console.log(result);
},
onTestSingle (result) {
console.log(`${result.index}: Time-consuming (${result.time}) ms; Result: ${result.passed?'Passed':'Failed'}`);
}
});
easy-test-lib supports custom plugins, which are handed over to the developer to customize the test calculation process. A simple custom plugin template is as follows
const plugin: ITestPlugin = (item, mergedArgs) => {
// do something ...
return {
result: {},
expect: {},
passed: true,
};
};
export default plugin;
See above 3
Determine whether two objects are equal, support reference types
The reference type will traverse whether all the attribute values are equal
const {isValueEqual} = require('easy-test-lib');
console.log(isValueEqual(1, 1));
Default plugin
- ITestConfigItem
- ITestPlugin
- IStartTest
- IIsValueEqual
- IMergedArgs