Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update utils.betterErrors so that it behaves correctly when there are…

… no expected or actual values.
  • Loading branch information...
commit ccf0273ef9058be0606fcf58dad4674878e7fcbc 1 parent 92d7bd1
Allan Boyd allanmboyd authored
Showing with 76 additions and 9 deletions.
  1. +2 −8 lib/utils.js
  2. +3 −1 package.json
  3. +71 −0 test/test-bettererrors.js
10 lib/utils.js
View
@@ -180,16 +180,10 @@ exports.httputil = function (cgi, envReady) {
*/
exports.betterErrors = function (assertion) {
- if (!assertion.error) return;
+ if (!assertion.error) return assertion;
var e = assertion.error;
- // deepEqual error message is a bit sucky, lets improve it!
- // e.actual and e.expected could be null or undefined, so
- // using getOwnPropertyDescriptor to see if they exist:
- if (Object.getOwnPropertyDescriptor(e, 'actual') &&
- Object.getOwnPropertyDescriptor(e, 'expected')) {
-
- // alexgorbatchev 2010-10-22 :: Added a bit of depth to inspection
+ if (e.actual && e.expected) {
var actual = util.inspect(e.actual, false, 10).replace(/\n$/, '');
var expected = util.inspect(e.expected, false, 10).replace(/\n$/, '');
var multiline = (
4 package.json
View
@@ -49,7 +49,9 @@
, "url" : "http://github.com/caolan/nodeunit.git"
}
, "devDependencies":
- { "uglify-js": ">=1.1.0" }
+ { "uglify-js": ">=1.1.0"
+ , "should" : ">=0.4.2"
+ }
, "bugs" : { "url" : "http://github.com/caolan/nodeunit/issues" }
, "licenses" :
[ { "type" : "MIT"
71 test/test-bettererrors.js
View
@@ -0,0 +1,71 @@
+/*
+ * Test utils.betterErrors. utils.betterErrors should provide sensible error messages even when the error does not
+ * contain expected, actual or operator.
+ */
+var assert = require("../lib/assert");
+var should = require("should");
+var types = require("../lib/types");
+var util = require('util');
+var utils = require("../lib/utils");
+
+function betterErrorStringFromError(error) {
+ var assertion = types.assertion({error: error});
+ var better = utils.betterErrors(assertion);
+ return better.error.stack.toString();
+}
+
+function performBasicChecks(betterErrorString) {
+ betterErrorString.should.include("AssertionError");
+ betterErrorString.should.include("test-bettererrors");
+ betterErrorString.should.not.include("undefined");
+}
+
+/**
+ * Control test. Provide an AssertionError that contains actual, expected operator values.
+ * @param test the test object from nodeunit
+ */
+exports.testEqual = function (test) {
+ try {
+ assert.equal(true, false);
+ } catch (error) {
+ var betterErrorString = betterErrorStringFromError(error);
+ performBasicChecks(betterErrorString);
+ betterErrorString.should.include("true");
+ betterErrorString.should.include("false");
+ betterErrorString.should.include("==");
+ test.done();
+ }
+};
+
+/**
+ * Test an AssertionError that does not contain actual, expected or operator values.
+ * @param test the test object from nodeunit
+ */
+exports.testAssertThrows = function (test) {
+ try {
+ assert.throws(function () {
+ });
+ } catch (error) {
+ var betterErrorString = betterErrorStringFromError(error);
+ performBasicChecks(betterErrorString);
+ test.done();
+ }
+};
+
+/**
+ * Test with an error that is not an AssertionError.
+ * @param test the test object from nodeunit
+ */
+exports.testNonAssertionError = function (test) {
+ try {
+ throw new Error("test error");
+ } catch (error) {
+ var betterErrorString = betterErrorStringFromError(error);
+ betterErrorString.should.not.include("AssertionError");
+ betterErrorString.should.include("Error");
+ betterErrorString.should.include("test error");
+ betterErrorString.should.include("test-bettererrors");
+ betterErrorString.should.not.include("undefined");
+ test.done();
+ }
+};
Please sign in to comment.
Something went wrong with that request. Please try again.