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]
This commit is contained in:
Sheel Choksi
2014-06-06 23:47:56 -07:00
parent 29aad761d9
commit 7d93541c09
2 changed files with 18 additions and 0 deletions

View File

@@ -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();
});
});

View File

@@ -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);
};