diff --git a/index.js b/index.js index 9f668d42..35957f25 100644 --- a/index.js +++ b/index.js @@ -37,26 +37,28 @@ queryResult = { // - within 'pg-promise'. // // promiseLib: null -// - Overrides the promise library instance to be used -// by the library. +// - Overrides the promise library instance used by +// the library. // } module.exports = function (options) { if (npm) { throw new Error('Cannot initialize the library more than once.'); } else { - npm = { - pg: require('pg') - }; + var promiseLib; if (options && options.promiseLib) { if (typeof(options.promiseLib) === 'function') { - npm.promise = options.promiseLib; + promiseLib = options.promiseLib; } else { throw new Error('Invalid or unsupported promise library override.'); } } else { - npm.promise = require('promise'); + promiseLib = require('promise'); } + npm = { + pg: require('pg'), + promise: promiseLib + }; } var lib = function (cn) { diff --git a/test/indexSpec.js b/test/indexSpec.js index bbf19968..5f010c37 100644 --- a/test/indexSpec.js +++ b/test/indexSpec.js @@ -1,23 +1,34 @@ -var pgpLib = require('../index') +var pgpLib = require('../index'); +var pgp = pgpLib(); // initializing the library; describe("Library entry object", function () { + it("must be a function", function () { expect(typeof(pgpLib)).toBe('function'); }); -}); -var pgp = pgpLib(); // initializing the library; +}); describe("Library initialization object", function () { + + it("can be created only once", function () { + expect(function(){ + pgpLib(); + }).toThrow('Cannot initialize the library more than once.'); + }); + it("must be a function", function () { expect(typeof(pgp)).toBe('function'); }); + it("must have property 'pg'", function () { expect(typeof(pgp.pg)).toBe('object'); }); + it("must have function 'end'", function () { expect(typeof(pgp.end)).toBe('function'); }); + it("must have valid property 'as'", function () { expect(typeof(pgp.as)).toBe('object'); expect(typeof(pgp.as.text)).toBe('function');