diff --git a/lib/mocha-phantomjs.coffee b/lib/mocha-phantomjs.coffee index 27643f3..6362249 100644 --- a/lib/mocha-phantomjs.coffee +++ b/lib/mocha-phantomjs.coffee @@ -49,6 +49,10 @@ class Reporter @page.addCookie(cookie) for cookie in @config.cookies @page.viewportSize = @config.viewportSize if @config.viewportSize @page.onConsoleMessage = (msg) -> console.log msg + @page.onError = (msg, traces) => + for {line, file}, index in traces + traces[index] = " #{file}:#{line}" + @fail "#{msg}\n\n#{traces.join '\n'}" @page.onInitialized = => @page.evaluate -> window.mochaPhantomJS = diff --git a/test/error.html b/test/error.html new file mode 100644 index 0000000..6f5f3c6 --- /dev/null +++ b/test/error.html @@ -0,0 +1,25 @@ + + + Tests Cookie + + + + +
+ + + + + + diff --git a/test/lib/mocha-phantomjs.js b/test/lib/mocha-phantomjs.js index 69a51a2..3d89273 100644 --- a/test/lib/mocha-phantomjs.js +++ b/test/lib/mocha-phantomjs.js @@ -77,6 +77,12 @@ return expect(stdout).to.match(/Failed to start mocha: Init timeout/); }); }); + it('returns a failure code when there is a page error', function(done) { + return this.runner(done, [fileURL('error')], function(code, stdout, stderr) { + expect(code).to.equal(1); + return expect(stdout).to.match(/ReferenceError/); + }); + }); it('does not fail when an iframe is used', function(done) { return this.runner(done, [fileURL('iframe')], function(code, stdout, stderr) { expect(stdout).to.not.match(/Failed to load the page\./m); diff --git a/test/src/mocha-phantomjs.coffee b/test/src/mocha-phantomjs.coffee index 9ec269c..bfde51d 100644 --- a/test/src/mocha-phantomjs.coffee +++ b/test/src/mocha-phantomjs.coffee @@ -55,6 +55,11 @@ describe 'mocha-phantomjs', -> expect(code).to.equal 255 expect(stdout).to.match /Failed to start mocha: Init timeout/ + it 'returns a failure code when there is a page error', (done) -> + @runner done, [fileURL('error')], (code, stdout, stderr) -> + expect(code).to.equal 1 + expect(stdout).to.match /ReferenceError/ + it 'does not fail when an iframe is used', (done) -> @runner done, [fileURL('iframe')], (code, stdout, stderr) -> expect(stdout).to.not.match /Failed to load the page\./m