Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[test] Implemented tests for `errs` covering all methods

  • Loading branch information...
commit 39767ab203501dcbdc4b72b1638c60c23d950380 1 parent 6fe25c7
@indexzero authored
View
2  lib/errs.js
@@ -109,7 +109,7 @@ exports.handle = function (error, callback) {
return callback(error);
}
- var emitter = new EventEmitter();
+ var emitter = new events.EventEmitter();
process.nextTick(function () { emitter.emit('error', error); });
return emitter;
};
View
86 test/errs-test.js
@@ -0,0 +1,86 @@
+/*
+ * errs-test.js: Tests for the `errs` module.
+ *
+ * (C) 2012, Nodejitsu Inc.
+ * MIT LICENSE
+ *
+ */
+
+var assert = require('assert'),
+ vows = require('vows'),
+ errs = require('../lib/errs'),
+ fixtures = require('./fixtures'),
+ macros = require('./macros');
+
+var opts = [{
+ foo: 'bar',
+ status: 404,
+ whatever: 'some other property'
+}, {
+ testing: true,
+ 'some-string': 'is-a-value',
+ message: 'This is an error. There are many like it.'
+}, {
+ 'a-function': 'that returns an object',
+ should: true,
+ have: 4,
+ properties: 'yes'
+}]
+
+vows.describe('errs').addBatch({
+ "Using errs module": {
+ "the register() method": {
+ "should register the prototype": function () {
+ errs.register('named', fixtures.NamedError);
+ assert.equal(errs.registered['named'], fixtures.NamedError);
+ }
+ },
+ "the create() method with": {
+ "a string": macros.create.string('An error as a string'),
+ "no parameters": macros.create.string('An error as a string'),
+ "an object": {
+ "that has no message": macros.create.object(opts[0]),
+ "that has a message": macros.create.object(opts[1])
+ },
+ "an error": macros.create.err(new Error('An instance of an error')),
+ "a function": macros.create.fn(function () {
+ return opts[2];
+ }),
+ "a registered type": {
+ "that exists": macros.create.registered('named', fixtures.NamedError, opts[1]),
+ "that doesnt exist": macros.create.registered('bad', null, opts[1])
+ }
+ },
+ "the handle() method": {
+ "with a callback": {
+ topic: function () {
+ var err = this.err = errs.create('Some async error');
+ errs.handle(err, this.callback.bind(this, null));
+ },
+ "should invoke the callback with the error": function (_, err) {
+ assert.equal(err, this.err);
+ }
+ },
+ "with no callback": {
+ topic: function () {
+ var err = this.err = errs.create('Some emitted error'),
+ emitter = errs.handle(err);
+
+ emitter.once('error', this.callback.bind(this, null))
+ },
+ "should invoke the callback with the error": function (_, err) {
+ assert.equal(err, this.err);
+ }
+ }
+ }
+ }
+}).addBatch({
+ "Using errs module": {
+ "the unregister() method": {
+ "should unregister the prototype": function () {
+ errs.unregister('named');
+ assert.isTrue(!errs.registered['named']);
+ }
+ }
+ }
+}).export(module);
View
18 test/fixtures.js
@@ -0,0 +1,18 @@
+/*
+ * fixtures.js: Test fixtures for the `errs` module.
+ *
+ * (C) 2012, Nodejitsu Inc.
+ * MIT LICENSE
+ *
+ */
+
+var util = require('util');
+
+var fixtures = exports;
+
+fixtures.NamedError = function (msg) {
+ this.message = msg;
+ this.named = true;
+};
+
+util.inherits(fixtures.NamedError, Error);
View
79 test/macros.js
@@ -0,0 +1,79 @@
+/*
+ * macros.js: Test macros for the `errs` module.
+ *
+ * (C) 2012, Nodejitsu Inc.
+ * MIT LICENSE
+ *
+ */
+
+var assert = require('assert'),
+ errs = require('../lib/errs');
+
+var macros = exports;
+
+//
+// Macros for `errs.create(type, opts)`.
+//
+macros.create = {};
+
+macros.create.string = function (msg) {
+ return {
+ topic: errs.create(msg),
+ "should create an error with the correct message": function (err) {
+ assert.instanceOf(err, Error);
+ assert.equal(msg, err.message);
+ }
+ };
+};
+
+macros.create.object = function (obj) {
+ return {
+ topic: errs.create(obj),
+ "should create an error with the specified properties": function (err) {
+ assert.instanceOf(err, Error);
+ assert.equal(err.message, obj.message || 'Unspecified error');
+ Object.keys(obj).forEach(function (key) {
+ assert.equal(err[key], obj[key]);
+ });
+ }
+ };
+};
+
+macros.create.err = function (inst) {
+ return {
+ topic: errs.create(inst),
+ "should return the error unmodified": function (err) {
+ assert.equal(err, inst);
+ }
+ };
+};
+
+macros.create.fn = function (fn) {
+ var obj = fn();
+
+ return {
+ topic: errs.create(fn),
+ "should create an error with the specified properties": function (err) {
+ assert.instanceOf(err, Error);
+ assert.equal(err.message, obj.message || 'Unspecified error');
+ Object.keys(obj).forEach(function (key) {
+ assert.equal(err[key], obj[key]);
+ });
+ }
+ };
+};
+
+macros.create.registered = function (type, proto, obj) {
+ return {
+ topic: function () {
+ return errs.create(type, obj);
+ },
+ "should create an error of the correct type": function (err) {
+ assert.instanceOf(err, proto || Error);
+ assert.equal(err.message, obj.message || 'Unspecified error');
+ Object.keys(obj).forEach(function (key) {
+ assert.equal(err[key], obj[key]);
+ });
+ }
+ };
+}
Please sign in to comment.
Something went wrong with that request. Please try again.