npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

easy-test-lib

v1.0.4

Published

A simple and easy-to-use testing lib

Downloads

34

Readme

🚀 A simple and small js test library

中文 | 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