Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

Add cross-frame compatible Error checking for fail

Johnathon Sanders3fcca2e358fb
diff --git a/lib/runner.js b/lib/runner.js
index 3199dc92..23ddaf23 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -202,7 +202,7 @@ Runner.prototype.fail = function(test, err) {
++this.failures;
test.state = 'failed';
- if (!(err instanceof Error)) {
+ if (!(err instanceof Error || err && typeof err.message == 'string')) {
err = new Error('the ' + type(err) + ' ' + stringify(err) + ' was thrown, throw an Error :)');
}
diff --git a/test/runner.js b/test/runner.js
index a2ce5125..08a5ce3e 100644
--- a/test/runner.js
+++ b/test/runner.js
@@ -229,7 +229,7 @@ describe('Runner', function(){
runner.fail(test, err);
})
- it('should emit a the error when failed with an Error', function(done){
+ it('should emit a the error when failed with an Error instance', function(done){
var test = {}, err = new Error('an error message');
runner.on('fail', function(test, err){
err.message.should.equal('an error message');
@@ -238,6 +238,15 @@ describe('Runner', function(){
runner.fail(test, err);
})
+ it('should emit the error when failed with an Error-like object', function(done){
+ var test = {}, err = {message: 'an error message'};
+ runner.on('fail', function(test, err){
+ err.message.should.equal('an error message');
+ done();
+ });
+ runner.fail(test, err);
+ })
+
it('should emit a helpful message when failed with an Object', function(done){
var test = {}, err = { x: 1 };
runner.on('fail', function(test, err){