Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed error messages for new'd objects & errors

  • Loading branch information...
commit 1f2443920bcfa3883efcd96974ab754b69d0a7a9 1 parent 122f4b9
Steve Mason authored
1  .gitignore
... ... @@ -1 +1,2 @@
1 1 node_modules
  2 +*.sublime-workspace
8 expectations.sublime-project
... ... @@ -0,0 +1,8 @@
  1 +{
  2 + "folders":
  3 + [
  4 + {
  5 + "path": "/home/stevem/Development/expectations"
  6 + }
  7 + ]
  8 +}
19 index.js
@@ -23,12 +23,12 @@
23 23 })(this, function(root, AssertionError) {
24 24 'use strict';
25 25 var assertions = {
26   - pass: function(message){
27   - },
28   - fail: function(message){
29   - throw new AssertionError({message: message});
30   - }
31   - };
  26 + pass: function(message){
  27 + },
  28 + fail: function(message){
  29 + throw new AssertionError({message: message});
  30 + }
  31 + };
32 32
33 33 function formatValue(value, ignoreUndefined, stack){
34 34 if(typeof value === 'undefined'){
@@ -55,6 +55,13 @@
55 55
56 56 stack = stack || [];
57 57 if(typeof value === 'object' && stack.indexOf(value) === -1 && stack.length < 5){
  58 + if(value.toString() !== '[object Object]'){
  59 + if(value instanceof Error){
  60 + return '[Error: ' + value.toString() + ']';
  61 + }
  62 + return '[' + value.toString() + ']';
  63 + }
  64 +
58 65 return '{' + Object.keys(value).map(function(key){
59 66 return ['"', key, '": ', formatValue(value[key], false, stack.concat(value))].join('');
60 67 }).join(', ') + '}';
22 test/expect.tests.js
@@ -194,6 +194,28 @@
194 194 }
195 195 }
196 196 });
  197 + it('can generate correct message for Function with custom toString()', function(){
  198 + function Obj(){}
  199 + Obj.prototype.toString = function(){
  200 + return "testing";
  201 + };
  202 + try{
  203 + expect(new Obj()).not.toBeDefined();
  204 + }catch(err){
  205 + if(err.message !== 'expected [testing] not to be defined'){
  206 + throw new Error('Expected error message is not correct: ' + err.message);
  207 + }
  208 + }
  209 + });
  210 + it('can generate correct message for Errors', function(){
  211 + try{
  212 + expect(new Error('text')).not.toBeDefined();
  213 + }catch(err){
  214 + if(err.message !== 'expected [Error: text] not to be defined'){
  215 + throw new Error('Expected error message is not correct: ' + err.message);
  216 + }
  217 + }
  218 + });
197 219 it('can generate correct message for objects with circular references', function(){
198 220 try{
199 221 var obj = {abc: 'def'};

0 comments on commit 1f24439

Please sign in to comment.
Something went wrong with that request. Please try again.