Skip to content

theajack/easy-test-lib

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
npm
 
 
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

🚀 A simple and small js test library

star Author

Version Downloads Size License TopLang issue Dependent

中文 | Online Use | Feedback | Gitee

1. Features

  1. Typescript writing
  2. Small size and easy to use
  3. Multi-terminal support
  4. Support asynchronous
  5. Customizable plugins
  6. Configuration file + command line operation
  7. Global installation available

2. Installation

2.1 api call

npm i easy-test-lib -D
const {startTest} = require('easy-test-lib');
startTest(config);

2.2 Configuration file call

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

2.3 Global installation and use

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>

2.4 CDN

<script src="https://cdn.jsdelivr.net/npm/easy-test-lib/easy-test-lib.min.js"></script>
<script>
    ETest.startTest({
        // ...
    })
</script>

3 Configuration

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
});

4 Plugins

4.1 Built-in plugins

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'}`);
    }
});

4.2 Custom plugin

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;

5 API

5.1 startTest

See above 3

5.2 isValueEqual

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));

5.3 defaultPlugin

Default plugin

5.4 ts interface

  1. ITestConfigItem
  2. ITestPlugin
  3. IStartTest
  4. IIsValueEqual
  5. IMergedArgs