From 19b83a7434ea33ac9db68ecbe22e28c9fceb7948 Mon Sep 17 00:00:00 2001 From: Gregg Van Hove Date: Tue, 11 Apr 2017 12:02:29 -0700 Subject: [PATCH] Throw a recognizable Error message when `fail` outside of a spec. Fixes #1017 --- lib/jasmine-core/jasmine.js | 4 ++++ spec/core/integration/EnvSpec.js | 17 +++++++++++++++++ src/core/Env.js | 4 ++++ 3 files changed, 25 insertions(+) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index e79b011d..86a637d3 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -1148,6 +1148,10 @@ getJasmineRequireObj().Env = function(j$) { }; this.fail = function(error) { + if (!currentRunnable()) { + throw new Error('\'fail\' was used when there was no current spec, this could be because an asynchronous test timed out'); + } + var message = 'Failed'; if (error) { message += ': '; diff --git a/spec/core/integration/EnvSpec.js b/spec/core/integration/EnvSpec.js index fccc45f5..e5543195 100644 --- a/spec/core/integration/EnvSpec.js +++ b/spec/core/integration/EnvSpec.js @@ -212,6 +212,23 @@ describe("Env integration", function() { env.execute(); }); + it("produces an understandable error message when 'fail' is used outside of a current spec", function(done) { + var env = new jasmineUnderTest.Env(), + reporter = jasmine.createSpyObj('fakeReporter', ['jasmineDone']); + + reporter.jasmineDone.and.callFake(done); + env.addReporter(reporter); + + env.describe("A Suite", function() { + env.it("an async spec that is actually synchronous", function(underTestCallback) { + underTestCallback(); + }); + expect(function() { env.fail(); }).toThrowError(/'fail' was used when there was no current spec/); + }); + + env.execute(); + }); + it("calls associated befores/specs/afters with the same 'this'", function(done) { var env = new jasmineUnderTest.Env(); diff --git a/src/core/Env.js b/src/core/Env.js index 4377b81a..0f4198e1 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -532,6 +532,10 @@ getJasmineRequireObj().Env = function(j$) { }; this.fail = function(error) { + if (!currentRunnable()) { + throw new Error('\'fail\' was used when there was no current spec, this could be because an asynchronous test timed out'); + } + var message = 'Failed'; if (error) { message += ': ';