diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 48078af9..eef18403 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -1065,6 +1065,13 @@ getJasmineRequireObj().Env = function(j$) { } }; + function ensureIsNotNested(method) { + var runnable = currentRunnable(); + if (runnable !== null && runnable !== undefined) { + throw new Error('\'' + method + '\' should only be used in \'describe\' function'); + } + } + var suiteFactory = function(description) { var suite = new j$.Suite({ env: self, @@ -1080,6 +1087,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.describe = function(description, specDefinitions) { + ensureIsNotNested('describe'); ensureIsFunction(specDefinitions, 'describe'); var suite = suiteFactory(description); if (specDefinitions.length > 0) { @@ -1093,6 +1101,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.xdescribe = function(description, specDefinitions) { + ensureIsNotNested('xdescribe'); ensureIsFunction(specDefinitions, 'xdescribe'); var suite = suiteFactory(description); suite.pend(); @@ -1103,6 +1112,7 @@ getJasmineRequireObj().Env = function(j$) { var focusedRunnables = []; this.fdescribe = function(description, specDefinitions) { + ensureIsNotNested('fdescribe'); ensureIsFunction(specDefinitions, 'fdescribe'); var suite = suiteFactory(description); suite.isFocused = true; @@ -1200,6 +1210,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.it = function(description, fn, timeout) { + ensureIsNotNested('it'); // it() sometimes doesn't have a fn argument, so only check the type if // it's given. if (arguments.length > 1 && typeof fn !== 'undefined') { @@ -1214,6 +1225,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.xit = function(description, fn, timeout) { + ensureIsNotNested('xit'); // xit(), like it(), doesn't always have a fn argument, so only check the // type when needed. if (arguments.length > 1 && typeof fn !== 'undefined') { @@ -1225,6 +1237,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.fit = function(description, fn, timeout){ + ensureIsNotNested('fit'); ensureIsFunctionOrAsync(fn, 'fit'); var spec = specFactory(description, fn, currentDeclarationSuite, timeout); currentDeclarationSuite.addChild(spec); @@ -1242,6 +1255,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.beforeEach = function(beforeEachFunction, timeout) { + ensureIsNotNested('beforeEach'); ensureIsFunctionOrAsync(beforeEachFunction, 'beforeEach'); currentDeclarationSuite.beforeEach({ fn: beforeEachFunction, @@ -1250,6 +1264,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.beforeAll = function(beforeAllFunction, timeout) { + ensureIsNotNested('beforeAll'); ensureIsFunctionOrAsync(beforeAllFunction, 'beforeAll'); currentDeclarationSuite.beforeAll({ fn: beforeAllFunction, @@ -1258,6 +1273,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.afterEach = function(afterEachFunction, timeout) { + ensureIsNotNested('afterEach'); ensureIsFunctionOrAsync(afterEachFunction, 'afterEach'); afterEachFunction.isCleanup = true; currentDeclarationSuite.afterEach({ @@ -1267,6 +1283,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.afterAll = function(afterAllFunction, timeout) { + ensureIsNotNested('afterAll'); ensureIsFunctionOrAsync(afterAllFunction, 'afterAll'); currentDeclarationSuite.afterAll({ fn: afterAllFunction, diff --git a/spec/core/integration/EnvSpec.js b/spec/core/integration/EnvSpec.js index 58ce58a6..f77b6265 100644 --- a/spec/core/integration/EnvSpec.js +++ b/spec/core/integration/EnvSpec.js @@ -1903,4 +1903,65 @@ describe("Env integration", function() { env.execute(); }); + + it('should throw on suites/specs/befores/afters nested in methods other than \'describe\'', function(done) { + var env = new jasmineUnderTest.Env(), + reporter = jasmine.createSpyObj('reporter', ['jasmineDone', 'suiteDone', 'specDone']); + + reporter.jasmineDone.and.callFake(function() { + var msg = /\'.*\' should only be used in \'describe\' function/; + + expect(reporter.specDone).toHaveFailedExpecationsForRunnable('suite describe', [msg]); + expect(reporter.specDone).toHaveFailedExpecationsForRunnable('suite xdescribe', [msg]); + expect(reporter.specDone).toHaveFailedExpecationsForRunnable('suite fdescribe', [msg]); + + expect(reporter.specDone).toHaveFailedExpecationsForRunnable('spec it', [msg]); + expect(reporter.specDone).toHaveFailedExpecationsForRunnable('spec xit', [msg]); + expect(reporter.specDone).toHaveFailedExpecationsForRunnable('spec fit', [msg]); + + expect(reporter.specDone).toHaveFailedExpecationsForRunnable('beforeAll spec', [msg]); + expect(reporter.specDone).toHaveFailedExpecationsForRunnable('beforeEach spec', [msg]); + + expect(reporter.suiteDone).toHaveFailedExpecationsForRunnable('afterAll', [msg]); + expect(reporter.specDone).toHaveFailedExpecationsForRunnable('afterEach spec', [msg]); + + done(); + }); + + env.addReporter(reporter); + + env.describe('suite', function() { + env.it('describe', function() { env.describe('inner suite', function() {}); }); + env.it('xdescribe', function() { env.xdescribe('inner suite', function() {}); }); + env.it('fdescribe', function() { env.fdescribe('inner suite', function() {}); }); + }); + + env.describe('spec', function() { + env.it('it', function() { env.it('inner spec', function() {}); }); + env.it('xit', function() { env.xit('inner spec', function() {}); }); + env.it('fit', function() { env.fit('inner spec', function() {}); }); + }); + + env.describe('beforeAll', function() { + env.beforeAll(function() { env.beforeAll(function() {}); }); + env.it('spec', function() {}); + }); + + env.describe('beforeEach', function() { + env.beforeEach(function() { env.beforeEach(function() {}); }); + env.it('spec', function() {}); + }); + + env.describe('afterAll', function() { + env.afterAll(function() { env.afterAll(function() {}); }); + env.it('spec', function() {}); + }); + + env.describe('afterEach', function() { + env.afterEach(function() { env.afterEach(function() {}); }); + env.it('spec', function() {}); + }); + + env.execute(); + }); }); diff --git a/src/core/Env.js b/src/core/Env.js index b26c7db0..9655f15d 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -384,6 +384,13 @@ getJasmineRequireObj().Env = function(j$) { } }; + function ensureIsNotNested(method) { + var runnable = currentRunnable(); + if (runnable !== null && runnable !== undefined) { + throw new Error('\'' + method + '\' should only be used in \'describe\' function'); + } + } + var suiteFactory = function(description) { var suite = new j$.Suite({ env: self, @@ -399,6 +406,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.describe = function(description, specDefinitions) { + ensureIsNotNested('describe'); ensureIsFunction(specDefinitions, 'describe'); var suite = suiteFactory(description); if (specDefinitions.length > 0) { @@ -412,6 +420,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.xdescribe = function(description, specDefinitions) { + ensureIsNotNested('xdescribe'); ensureIsFunction(specDefinitions, 'xdescribe'); var suite = suiteFactory(description); suite.pend(); @@ -422,6 +431,7 @@ getJasmineRequireObj().Env = function(j$) { var focusedRunnables = []; this.fdescribe = function(description, specDefinitions) { + ensureIsNotNested('fdescribe'); ensureIsFunction(specDefinitions, 'fdescribe'); var suite = suiteFactory(description); suite.isFocused = true; @@ -519,6 +529,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.it = function(description, fn, timeout) { + ensureIsNotNested('it'); // it() sometimes doesn't have a fn argument, so only check the type if // it's given. if (arguments.length > 1 && typeof fn !== 'undefined') { @@ -533,6 +544,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.xit = function(description, fn, timeout) { + ensureIsNotNested('xit'); // xit(), like it(), doesn't always have a fn argument, so only check the // type when needed. if (arguments.length > 1 && typeof fn !== 'undefined') { @@ -544,6 +556,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.fit = function(description, fn, timeout){ + ensureIsNotNested('fit'); ensureIsFunctionOrAsync(fn, 'fit'); var spec = specFactory(description, fn, currentDeclarationSuite, timeout); currentDeclarationSuite.addChild(spec); @@ -561,6 +574,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.beforeEach = function(beforeEachFunction, timeout) { + ensureIsNotNested('beforeEach'); ensureIsFunctionOrAsync(beforeEachFunction, 'beforeEach'); currentDeclarationSuite.beforeEach({ fn: beforeEachFunction, @@ -569,6 +583,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.beforeAll = function(beforeAllFunction, timeout) { + ensureIsNotNested('beforeAll'); ensureIsFunctionOrAsync(beforeAllFunction, 'beforeAll'); currentDeclarationSuite.beforeAll({ fn: beforeAllFunction, @@ -577,6 +592,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.afterEach = function(afterEachFunction, timeout) { + ensureIsNotNested('afterEach'); ensureIsFunctionOrAsync(afterEachFunction, 'afterEach'); afterEachFunction.isCleanup = true; currentDeclarationSuite.afterEach({ @@ -586,6 +602,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.afterAll = function(afterAllFunction, timeout) { + ensureIsNotNested('afterAll'); ensureIsFunctionOrAsync(afterAllFunction, 'afterAll'); currentDeclarationSuite.afterAll({ fn: afterAllFunction,