Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

geddy-unit-test-utilities

franksrevenge34MITdeprecated1.1.0

This package has been deprecated

Unit test helper utilities for Geddy

geddy, test, unit, development

readme

Geddy Unit Test Utilities

A support library that helps with unit testing Geddy applications.

Installation

> npm install geddy-unit-test-utilities

Features

  • Write tests that hit controllers and actions just like a user request would
  • Test framework agnostic -- use with Mocha, Geddy's jake test, or any other test framework
  • Test templates and views from command line
  • Sticky sessions -- test chains of requests rather than just one
  • Sticky data -- pass data from one test to another

Compatibility

  • 1.1.x works with Geddy 13+
  • 1.0.x works with Geddy 0.11.x

Examples

Process request using config/router.js routing configuration

var assert                    = require( 'assert' ),
    GeddyUnitTestUtilities    = require( 'geddy-unit-test-utilities' );

var tests = {

    'test routing' : function( next )
    {
        GeddyUnitTestUtilities.router.route(
                {
                    // request URL, required
                    url        : '/path/to/my/action?id=1234',

                    // optional request method, defaults to GET
                    method    : 'POST',

                    // optional request parameters passed to the controller
                    params : {
                        name    : 'John',
                        phone    : '12345678'
                    },

                    // optional request headers
                    headers : {
                        accept    : 'application/json'
                    }
                },
                function( response )
                {
                    // specify what happens when request has been processed
                    console.log( response.getStatusCode() );
                    console.log( response.getHeader( 'content-type' ) );
                    console.log( response.getContent() );

                    assert.equals( response.getStatusCode(), 200 );

                    next();
                }
            );
    }
};

module.exports = tests;

Run specific action from specific controller

var assert                    = require( 'assert' ),
    GeddyUnitTestUtilities    = require( 'geddy-unit-test-utilities' );

var tests = {

    'test controller action' : function( next )
    {
        // specify controller
        var ctl = GeddyUnitTestUtilities.controller.create( 'MyTestController' );

        // optional -- specify parameters passed to the controller
        ctl.setParams(
                {
                    id        : 1234,
                    name    : 'John',
                    phone    : '12345678'
                }
            );

        // optional -- specify request headers
        ctl.setHeaders(
                {
                    accept    : 'application/json'
                }
            );

        // optional -- set request URL
        ctl.setUrl( '/hello/world' );

        // optional -- set request method
        ctl.setMethod( 'POST' );

        // specify what happens when request has been processed
        ctl.response.onComplete(
                function( response )
                {
                    console.log( response.getStatusCode() );
                    console.log( response.getHeader( 'content-type' ) );
                    console.log( response.getContent() );

                    assert.equals( response.getStatusCode(), 200 );

                    next();
                }
            );

        // run action
        ctl.unit.runAction( 'myAction' );
    }
};

module.exports = tests;

Sticky sessions

In order to maintain a single session across multiple queries, specify the following in the before() and after() functions of your test. Note that the mock queries do not respect cookie expirations or paths; all cookies returned are passed to the next request as-is.

If session.end() is not called in the after() function, the sessions will stick across test modules.

var assert                    = require( 'assert' ),
    GeddyUnitTestUtilities    = require( 'geddy-unit-test-utilities' );

var tests = {

    'before' : function( next )
    {
        // enable sticky session
        GeddyUnitTestUtilities.session.start( next );
    },

    'after' : function( next )
    {
        // disable sticky session
        GeddyUnitTestUtilities.session.end();
        next();
    }

    // ... ( your tests go here )
};

module.exports = tests;

Cookies from previous request (a.k.a. cookies that will be passed to the next request) are accessible via GeddyUnitTestUtilities.session.cookieStore.

console.log( GeddyUnitTestUtilities.session.cookieStore.get( 'sid' ) );

GeddyUnitTestUtilities.session.cookieStore.set( 'sid', 'Hello world' );

Sticky data

Test utilies provide a way to store data across tests and test modules.

var assert                    = require( 'assert' ),
    GeddyUnitTestUtilities    = require( 'geddy-unit-test-utilities' );

var tests = {

    'test first' : function( next )
    {
        // store sticky data
        GeddyUnitTestUtilities.global.set( 'myVariable', 'Hello world' );
        next();
    },

    'test second' : function( next )
    {
        // retrieve sticky data
        console.log( GeddyUnitTestUtilities.global.get( 'myVariable' ) );
        next();
    }

    'after' : function( next )
    {
        // empty sticky data
        GeddyUnitTestUtilities.global.reset();
        next();
    }

};

module.exports = tests;

Helper functions

GeddyUnitTestUtilities.helper.getBasicAuthStr( username, password )

Returns a string which can be used as basic HTTP authentication in the Authorization HTTP request header.

Notes

Version 1.1.x is compatible with Geddy 13+.

Version 1.0.x is compatible with Geddy 0.11.x.

If you wish to use templates during testing with Geddy 0.11.x, a small change to original Geddy source code is required, available here.

changelog

Changelog

1.1.0

  • Gained support for Geddy v13
  • Dropped support for Geddy v0.11

1.0.5

  • Added helper function GeddyUnitTestUtilities.helper.getBasicAuthStr() to deal with basic HTTP authentication
  • Sessions no longer fail to initialize if a sticky session hasn't been started

1.0.4

  • Support for sticky data
  • Confirmed compatibility with third party test frameworks
  • Updated readme
  • Added license (MIT)

1.0.2

  • Support for controller.setHeaders()

1.0.1

  • Extracts parameters correctly from router rules to controller.params
  • controller.setParams() now merges new parameters with old ones instead of clearing the old ones entirely
  • More code documentation
  • .editorconfig

1.0.0

  • Supports i18n
  • Supports templates
  • controller.params automatically contains all parameters specified in the URL query string
  • Remote IP address may be set with controller.setRemoteIp()

0.1.1

  • Fixed typos

0.1.0

  • Supports sessions
  • Supports router
  • Supports sticky sessions
  • Supports methods
  • Supports URLs
  • Friendlier way to set parameters
  • Cookie manipulation

0.0.2

  • Friendlier "run auction" interface

0.0.1

  • Initial release