Skip to content

Commit 605728f

Browse files
committedJun 16, 2015
Merge pull request #31 from cjihrig/30
Handle multi-line errors
2 parents 03346a1 + 53bf6f1 commit 605728f

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed
 

‎lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ internals.type = function (value) {
341341
internals.at = function (error) {
342342

343343
error = error || new Error();
344-
var at = error.stack.split('\n').slice(1).filter(internals.filterLocal)[0].match(/^\s*at \(?(.+)\:(\d+)\:(\d+)\)?$/);
344+
var at = error.stack.replace(error.toString(), '').split('\n').slice(1).filter(internals.filterLocal)[0].match(/^\s*at \(?(.+)\:(\d+)\:(\d+)\)?$/);
345345
return {
346346
filename: at[1],
347347
line: at[2],

‎test/index.js

+24-7
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ describe('expect()', function () {
143143
Code.expect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]).to.be.a.string();
144144
}
145145
catch (err) {
146-
Code.settings.truncateMessages = origTruncate;
147146
exception = err;
148147
}
149148

149+
Code.settings.truncateMessages = origTruncate;
150150
Hoek.assert(exception.message === 'Expected [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 ] to be a string but got \'array\'', exception);
151151
done();
152152
});
@@ -174,14 +174,31 @@ describe('expect()', function () {
174174
Code.expect({ a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10 }).to.be.a.string();
175175
}
176176
catch (err) {
177-
Code.settings.truncateMessages = origTruncate;
178177
exception = err;
179178
}
180179

180+
Code.settings.truncateMessages = origTruncate;
181181
Hoek.assert(exception.message === 'Expected { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10 } to be a string but got \'object\'', exception);
182182
done();
183183
});
184184

185+
it('handles multi-line error message', function (done) {
186+
187+
var exception = false;
188+
var origTruncate = Code.settings.truncateMessages;
189+
try {
190+
Code.settings.truncateMessages = false;
191+
Code.expect({ a: 1, b: '12345678901234567890123456789012345678901234567890' }).to.be.a.string();
192+
}
193+
catch (err) {
194+
exception = err;
195+
}
196+
197+
Code.settings.truncateMessages = origTruncate;
198+
Hoek.assert(exception.message === 'Expected { a: 1,\n b: \'12345678901234567890123456789012345678901234567890\' } to be a string but got \'object\'', exception);
199+
done();
200+
});
201+
185202
it('asserts on invalid condition (long object values, display truncated)', function (done) {
186203

187204
var exception = false;
@@ -205,10 +222,10 @@ describe('expect()', function () {
205222
Code.expect({ a: 12345678901234567890, b: 12345678901234567890 }).to.be.a.string();
206223
}
207224
catch (err) {
208-
Code.settings.truncateMessages = origTruncate;
209225
exception = err;
210226
}
211227

228+
Code.settings.truncateMessages = origTruncate;
212229
Hoek.assert(exception.message === 'Expected { a: 12345678901234567000, b: 12345678901234567000 } to be a string but got \'object\'', exception);
213230
done();
214231
});
@@ -236,10 +253,10 @@ describe('expect()', function () {
236253
Code.expect('{ a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10 }').to.be.a.number();
237254
}
238255
catch (err) {
239-
Code.settings.truncateMessages = origTruncate;
240256
exception = err;
241257
}
242258

259+
Code.settings.truncateMessages = origTruncate;
243260
Hoek.assert(exception.message === 'Expected \'{ a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10 }\' to be a number but got \'string\'', exception);
244261
done();
245262
});
@@ -1139,9 +1156,9 @@ describe('expect()', function () {
11391156
var exception = false;
11401157
try {
11411158
Code.expect(['abc']).to.deep.equal(['abc']);
1142-
Code.expect({a: 1}).to.deep.equal({a: 1});
1143-
Code.expect({}).to.not.deep.equal({a: 1});
1144-
Code.expect({a: 1}).to.not.deep.equal({});
1159+
Code.expect({ a: 1 }).to.deep.equal({ a: 1 });
1160+
Code.expect({}).to.not.deep.equal({ a: 1 });
1161+
Code.expect({ a: 1 }).to.not.deep.equal({});
11451162
Code.expect(Object.create(null)).to.not.deep.equal({});
11461163
Code.expect(Object.create(null)).to.deep.equal({}, { prototype: false });
11471164
}

0 commit comments

Comments
 (0)
Please sign in to comment.