From 7d93541c09786163b1470b3212c17b6db37b7352 Mon Sep 17 00:00:00 2001 From: Sheel Choksi Date: Fri, 6 Jun 2014 23:47:56 -0700 Subject: [PATCH] Throw a more specific error when 'expect' is used without a currentSpec If an async test has timed-out, there could still be some expectations that are scheduled to run after the fact in which case curerntSpec will be null. Rather than the type error that would result, we now indicate that 'expect' was used at an unexpected time. This also helps cases where an 'expect' is being used at a top-level, showing an error message in the console for this case as well. [fixes #602] --- spec/core/integration/EnvSpec.js | 14 ++++++++++++++ src/core/Env.js | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/spec/core/integration/EnvSpec.js b/spec/core/integration/EnvSpec.js index 3aef1360..5381fddc 100644 --- a/spec/core/integration/EnvSpec.js +++ b/spec/core/integration/EnvSpec.js @@ -488,5 +488,19 @@ describe("Env integration", function() { env.execute(); }); + + it("produces an understandable error message when an 'expect' is used outside of a current spec", function(done) { + var env = new j$.Env(); + + env.describe("A Suite", function() { + env.it("an async spec that is actually synchronous", function(underTestCallback) { + underTestCallback(); + expect(function() { env.expect('a').toEqual('a'); }).toThrowError(/'expect' was used when there was no current spec/); + done(); + }); + }); + + env.execute(); + }); }); diff --git a/src/core/Env.js b/src/core/Env.js index fc2fd27f..48146ac6 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -317,6 +317,10 @@ getJasmineRequireObj().Env = function(j$) { }; this.expect = function(actual) { + if (!currentSpec) { + throw new Error('\'expect\' was used when there was no current spec, this could be because an asynchronous test timed out'); + } + return currentSpec.expect(actual); };