ivitest

2.0.3 • Public • Published

iviTest

NPM version Stories in Ready

NPM

Simple REST API unit test, based from vowsjs and request.
Use chaijs to find BDD & TDD documentation.

Simple to use

just place your code in test folder inside ivitest main folder, or you can create custom folder anywhere.

    {
        request     : {}, // GLOBAL
        response    : {}, // GLOBAL
        "a context" : {  // LOCAL
            request     : {},
            response    : {},
            callback    : function(err,res,body) {},
            before      : function(data,done) {},
            after       : function(data,done) {}
        }
    }

Request

    {
        url     : "http://google.com",
        method  : "GET", // POST, GET, PUT, & etc
        headers : {
            'Content-Type': 'text/plain'
        }
    }

Response

Check data type

    // sample response
    // {
    //      data : [],
    //      list : [],
    //      body : {},
    //      foo  : {},
    //      "check the child" : {
    //          name    : "",
    //          address : "",
    //          years   : 2014,
    //          age     : 26,
    //      }
    // }
    {
        array   : ["data","list"],
        object  : ["body","foo"],
        "check the child"  : {
            string  : ["name","address"],
            number  : ["years","age"],
        }
    }

Check object inside array

    //  sample response
    //  [
    //      {
    //          "name"      : "yuda",
    //          "adress"    : "surabaya",
    //      }
    //  ]
    {
        each   : ["name","address"] // each response has property name and address
    }

callback

assert, should or expect can directly call from this function

    // sample response
    // {
    //      data : [],
    //      list : [],
    //      foo  : {},
    //      "check the child" : {
    //          name    : "",
    //          address : "",
    //          years   : 2014,
    //          age     : 26,
    //      }
    // }
    callback: function(err,response,body) {
        body.should.have.property("data").be.a("array");
        expect(body).to.have.property("list").to.be.a("array");
        assert((typeof body.foo)=="object")
    }

if you want to use first response as second request

    var google = {
        "request1" : {
            request     : { 
                method  : "GET",
                url     : "http://google.com",
            },
            response    : {
                statusCode  : 200
            },
            callback    : function(err,res,body) {
                return {
                    body : {
                        status : res.statusCode
                    }
                }
            }
        },
        "request2" : {
            request     : {
                method  : "POST",
                url     : "http://custom.com/getstatus",
                body    : {
                    detail : "status from google"
                }
            },
            response    : {
                statusCode  : 200
            }
        }
    }
 
    module.exports = google;

Before & After

    var mongoose = require("mongoose");
    mongoose.connect("mongodb://localhost/mydb");
    var user = mongoose.model("users",{},"users);
    var google = {
        "request1" : {
            request     : { 
                method  : "GET",
                url     : "http://google.com",
            },
            response    : {
                statusCode  : 200
            },
            callback    : function(err,res,body) {
            },
            before : function(data,done) {
                var su = new user({name:"yuda"});
                su.save(function(err) {
                    data.name = "yuda";
                    done();
                })
            },
            after : function(data,done) {
                su.remove({name:data.name},function() {
                    done();
                })
            }
        }
    }
 
    module.exports = google;

then execute them : ivitest
if you want to run test in other folder : ivitest -t /foldername
if you want to know all file to run : ivitest -l
if you want to execute just one file ( not all in one hit ) : ivitest -c 1
( number 1 can change with other number or string file name )

to see help : ivitest -h

Readme

Keywords

Package Sidebar

Install

npm i ivitest

Weekly Downloads

22

Version

2.0.3

License

none

Last publish

Collaborators

  • minesv