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

mayoi

v1.0.6

Published

a tiny nodeJS unittest framework like true JS

Downloads

5

Readme

mayoi

a tiny nodeJS unittest framework like true JS

Screenshot

Introduction

mayoi is a small unittest framework that depend on only async. It just provide two compoment:

  • a function for search your test file and run it
  • a Mock Object for mock & unmock function that your test depend on

Installation

use npm install

npm install mayoi

Usage - setup

create folder for test file

mkdir mayoiTest
touch mayoiTest/index.js
touch mayoiTest/testSynchronous.js
touch mayoiTest/testAsynchronous.js

index.js

const mayoi = require("mayoi");

mayoi.run({
    root: "mayoiTest",
    ignore: "index.js"
});

testSynchronous.js

const assert = require("assert");

module.exports.tests = [
    function test_1() {
        assert.equal("testString1", "testString2");
    },
    function test_2() {
        assert.equal(200, 200);
    }
];

testAsynchronous.js

const assert = require('assert');

module.exports.tests = [
    async function test_1() {
        let asyncFuncrion = new Promise(function (resolve, reject) {
            setTimeout(() => { resolve("Promise1"); }, 1000);
        });
        assert.equal("Promise1", await asyncFuncrion);
    },
    async function test_2() {
        let asyncFuncrion = new Promise(function (resolve, reject) {
            setTimeout(() => { resolve("Promise2"); }, 500);
        });
        assert.equal("Promise2", await asyncFuncrion);
    }
];

run test

node mayoiTest/index.js

you can also add test script in package.json like

...
"scripts": {
    "test": "node mayoiTest/index.js"
},
...

then you will see

Usage - hook function on test

there are six type hook function

| Name | Features | | --- | --- | | initStartFunction | run at whole test start | | startFunction | run at test file start | | startEach | run at each test function start | | initEndFunction | run at whole test end | | endFunction | run at test file end | | endEach | run at each test function end |

initStartFunction & initEndFunction

hook them when you run mayoi

index.js

const mayoi = require("mayoi");

mayoi.run({
    root: "mayoiTest",
    ignore: "index.js",
    startFunction: () => console.log("\trun initStartFunction"),
    endFunction: () => console.log("\trun finalEndFunction")
});

startFunction & endFunction & startEach & endEach

hook them in the test file testSynchronous.js

const assert = require("assert");

let cyan = "\x1b[36m%s\x1b[0m";

module.exports.startEach = function() {
    console.log("run mock testStartEachFunction");
}
module.exports.startFunction = function() {
    console.log(cyan, "run mock testFileStartFunction");
}
module.exports.tests = [
    function test_1() {
        assert.equal("test_1", "test_1");
    },
    function test_2() {
        assert.equal(200, 200);
    }
];
module.exports.endFunction = function() {
    console.log(cyan, "run mock testFileEndFunction");
}
module.exports.endEach = function() {
    console.log("run mock testEndEachFunction");
}

then you will see output like screenshot

Usage - mock function & object

mayoi provide a mock tool for user, because of the tool is implement by save real object in a map then direct cover fake function on it variable, to get real function back from the map you need to unmock it after you test complete

for example I have a function that return true and false both 50% probability should be tested.

module.exports = function () {
    return Math.random() > 0.5;
}

So I implement the test by mock the built-in random function.

const assert = require("assert");
const mayoi = require("mayoi");
const randomTF = require("../randomTF.js");

module.exports.tests = [
    function test_random1() {
        Math.random = mayoi.mock.mock(Math.random, () => 0.2);
        assert.equal(randomTF(), false);
        Math.random = mayoi.mock.unmock(Math.random);
    },
    function test_random2() {
        Math.random = mayoi.mock.mock(Math.random, () => 0.8);
        assert.equal(randomTF(), true);
        Math.random = mayoi.mock.unmock(Math.random);
    }
];

make sure the format is

realFunction = mayoi.mock.mock(realFunction, fakeFunction);
realFunction = mayoi.mock.unmock(realFunction);

because JS object is pass by sharing, the new fakeFunction should use return to repatriate it

Developer - Get Source Code

to get source code just clone from github

Developer - Tests

mayoi is a unittest framework, so it tests only use built-in assert function to test. run the test/test.js, if there's on error output, represent the tests are all pass.

node test/test.js