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 mayoiTesttouch mayoiTest/index.jstouch mayoiTest/testSynchronous.jstouch mayoiTest/testAsynchronous.js
index.js
const mayoi = ;mayoi;
testSynchronous.js
const assert = ;moduleexportstests ={assert;}{assert;};
testAsynchronous.js
const assert = ;moduleexportstests ={let asyncFuncrion = {;};assert;}{let asyncFuncrion = {;};assert;};
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 = ;mayoi;
startFunction & endFunction & startEach & endEach
hook them in the test file testSynchronous.js
const assert = ;let cyan = "\x1b[36m%s\x1b[0m";moduleexports {console;}moduleexports {console;}moduleexportstests ={assert;}{assert;};moduleexports {console;}moduleexports {console;}
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 {return Math > 05;}
So I implement the test by mock the built-in random function.
const assert = ;const mayoi = ;const randomTF = ;moduleexportstests ={Mathrandom = mayoimock;assert;Mathrandom = mayoimock;}{Mathrandom = mayoimock;assert;Mathrandom = mayoimock;};
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