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

jive-testing-framework

v0.0.4

Published

For testing nodejs applications

Downloads

8

Readme

Summary

Testing nodeJS applications -- for example, those written ontop of the Jive SDK -- has become easier than ever, using the jive testing framework (https://github.com/jivesoftware/jive-testing-framework). The framework:

  • is a convenient wrapper for the excellent Mocha library, and enables you to inject useful framework and custom utilities into the testing context.
  • includes a super useful "server simulator" that you can instantiate and control from within your testing context. Yep, you can dynamically spawn servers up, program their intended responses, and even ship code to these servers to further customize their behavior. This makes the framework especially useful for testing applications that need to mock the interaction of external systems (eg. Jive, salesforce, or whatever).
  • is a test coverage measurer/visualizing tool. Stay tuned on how to do this in the near future.

Including the framework

To begin testing your application, make sure you include the testing framework in your package.json:

{
    …  
    "dependencies": {  
        …  
        "jive-testing-framework" : "git+ssh://[email protected]/jivesoftware/jive-testing-framework.git",  
        …  
    }  
}

The framework is not yet published to NPM, so it must be pulled down by nodes directly from the public github repository.

Setting up your test runner

Next you have to create a runner. I've called my example one run.js, and it should be executable by node (eg. node run.js should work).

var testUtils = require('jive-testing-framework/testUtils');
var jive = require('jive-sdk');
var jiveMongo = require('../');


var makeRunner = function() {
    return testUtils.makeRunner( {
        'eventHandlers' : {
            'onTestStart' : function(test) {
                test['ctx']['persistence'] = new jiveMongo({
                    'databaseUrl' : 'mongoTestDB'
                });
            },
            'onTestEnd' : function(test) {
                test['ctx']['persistence'].destroy();
            }
        }
    });
};

makeRunner().runTests(
    {
        'context' : {
            'testUtils' : testUtils,
            'jive' : jive,
            'jiveMongo' : jiveMongo
        },
        'rootSuiteName' : 'jive',
        'runMode' : 'test',
        'testcases' : process.cwd()  + '/library',
        'timeout' : 5000
    }
).then( function(allClear) {
    if ( allClear ) {
        process.exit(0);
    } else {
        process.exit(-1);
    }
});

In this simple runner, I've created a makeRunner function that creates a test runner by invoking the makeRunner function from the framework testUtils library, specifying test life cycle event handler overrides (the base suite has no-ops). In my example, on the start of every test I inject into the test context an instance of mongodb client. I can inject anything I want into the context, and it will be available from within the test as this['']. Then on each test teardown, I destroy the mongodb client.

Any resources that should always be present will be injected into the testing context via the 'context' parameter to runTests. In my example, I injected an instance of the framework testUtils, the jive-sdk, and the jive mongo driver into the context.

The 'test cases' parameter to runTests() is the location of the testcase library, described below, and the 'rootSuiteName' describes which suite within that library (and any subsuites) should be run. The 'timeout' parameter controls how much time Mocha should give to an individual test before that test is counted as failed. The 'runMode' parameter determines whether or not we are running the testing framework in test mode, or in coverage mode.

Setting up the testcase library

In your project, set up a testcase folder hierarchy. Its root should be equal to the ''testcases' parameter of context attribute fed to runTests. Here is what mine looks like:

\projectroot
       \test  
            \library  
                 \mongo  
                      \testcases  
                 \othermonogotests  
                      \testcases

The test runner will recursively execute any Mocha tests found in the \testcases folders inside your library root.

Here is one such file, called tests.js, located under \library\mongo\testcases\tests.js:

var assert = require('assert');
var test = require('../basePersistenceTest');  
  
describe('jive', function () {  
  
    describe ('#persistence.mongo', function () {  
  
        it('save', function (done) {  
            var jive = this['jive'];  
            var testUtils = this['testUtils'];  
            var persistence = this['persistence'];  
  
            test.testSave(testUtils, persistence).then(  
                function() {  
                    setTimeout( function() {  
                        done();  
                    }, 1000);  
                },  
  
                function(e) {  
                    assert.fail(e);  
                }  
            ).finally( function() {  
                    return persistence.close();  
                });  
        })  
});

In this example, I pull out the jive, persistence, and testUtils resources injected into the test context, then provide them as parameters into a custom testing utility called 'basePersistenceTest' that exists in a folder just above the one housing my currently executing test. Note that I could have also just used those resources inline in my test.

Simulating a remote server

Here is a fun example of simulating Jive, and controlling what it will return.

var assert = require('assert');
  
describe('jive', function () {  
  
    describe ('server', function () {  
  
    it('test', function (done) {  
        var jive = this['jive'];  
        var testUtils = this['testUtils'];  
  
        var server;  
        testUtils.createServer( {  
            'port' : 5556,  
            'routes' : [  
                {  
                    'method'        : 'get',  
                    'statusCode'    : '200',  
                    'path'          : '/hi',  
                    'body'          : 'hiya',  
                    'handler' : function() {  
                        // optional  
                        // this is going to run on the remote server!  
                        // mutates the response for /hi -- making that endpoint reject now that its  
                        // been called once  
                        delete app.routes.get;  
                        self.setEndpoint(  
                            'get',  
                            '/hi',  
                            '400',  
                            JSON.stringify({ 'expired' : 'now'})  
                        );  
                    }  
                }  
            ]  
        }).then( function(_server) {  
                server = _server;  
                return jive.util.buildRequest( 'http://localhost:5556/hi').then( function(response) {  
                    // first call should be success  
                    console.log(response);  
                    assert.ok(response);  
                    assert.equal( response.entity.body, 'hiya');  
                }).then( function() {  
                    // second call should fail  
                    return jive.util.buildRequest( 'http://localhost:5556/hi').then(  
                        function(response) {  
                            // expected error  
                            assert.fail(response);  
                        },  
                        function(err) {  
                            assert.ok(err);  
                            assert.equal( err['statusCode'], 400);  
                        }  
                    );  
  
                });  
            }).catch(function(e) {  
                assert.fail(e);  
            }).then( function() {  
                done();  
            }).finally( function() {  
                if ( server ) {  
                    return server.stop();  
                }  
            });  
     });  
  
});

More fun things you can do

As part of your run.js you can inject some of the other amazingly useful utilities such as mockery and simon, for noninvasive mocks of whole libraries and individual functions. But hopefully what I have presented here will make unit and functional testing easier for nodeJS apps.