From f7fc2861866bdf9130ea36f2dc88e2a3f31faf92 Mon Sep 17 00:00:00 2001 From: Rob Brackett Date: Sat, 25 May 2013 19:21:19 -0700 Subject: [PATCH] Pass iframe errors up to the parent error handler (if there is one). --- app/assets/javascripts/konacha/iframe.js | 7 +++++++ spec/dummy/spec/javascripts/failing_spec.js | 9 +++++++++ spec/runner_spec.rb | 15 +++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/app/assets/javascripts/konacha/iframe.js b/app/assets/javascripts/konacha/iframe.js index 99c9643..54a0aaf 100644 --- a/app/assets/javascripts/konacha/iframe.js +++ b/app/assets/javascripts/konacha/iframe.js @@ -5,6 +5,13 @@ window.Konacha = { } }; +// Push errors to parent iframe. +window.onerror = function() { + if (parent.onerror) { + return parent.onerror.apply(parent, arguments); + } +}; + window.Mocha = Object.create(parent.Mocha); window.mocha = Object.create(parent.mocha); diff --git a/spec/dummy/spec/javascripts/failing_spec.js b/spec/dummy/spec/javascripts/failing_spec.js index ed8678d..07a4dc9 100644 --- a/spec/dummy/spec/javascripts/failing_spec.js +++ b/spec/dummy/spec/javascripts/failing_spec.js @@ -8,4 +8,13 @@ describe("failure", function(){ // throwing real Error objects. throw new Error("this one errors out"); }); + + it("errors asynchronously", function(done) { + setTimeout(function() { + (2 + 2).should.equal(5); + }, 0); + setTimeout(function() { + done(); + }, 10); + }); }); diff --git a/spec/runner_spec.rb b/spec/runner_spec.rb index 7f8cdab..21eb9f1 100644 --- a/spec/runner_spec.rb +++ b/spec/runner_spec.rb @@ -89,6 +89,20 @@ 'error' => {'message' => 'this one errors out', 'name' => 'Error'}}} end + let(:error_async) do + {'event' => 'fail', + 'type' => 'test', + 'data' => { + 'title' => 'errors asynchronously', + 'fullTitle' => 'failure errors asynchronously', + 'parentFullTitle' => 'failure', + 'status' => 'failed', + 'path' => 'failing_spec.js', + # Accept anything for 'message' since async errors have URLs, which + # vary on every run, and line #, which may change in Chai releases. + 'error' => {'message' => anything(), 'name' => 'Error'}}} + end + let(:pass) do {'event' => 'pass', 'type' => 'test', @@ -122,6 +136,7 @@ subject.reporter.should_receive(:process_mocha_event).with(test) subject.reporter.should_receive(:process_mocha_event).with(failure) subject.reporter.should_receive(:process_mocha_event).with(error) + subject.reporter.should_receive(:process_mocha_event).with(error_async) subject.reporter.should_receive(:process_mocha_event).with(pass) subject.reporter.should_receive(:process_mocha_event).with(pending) subject.reporter.should_receive(:process_mocha_event).with(end_event)