Parallel: forbid beforeEach/afterEach at the top level of spec files

Each spec file is only loaded in a single worker, so top level
before/afterEach can't behave consistently.

beforeEach/afterEach are still supported in:
* Helper files
* describe() blocks
* At the top level of spec files in non-parallel mode
This commit is contained in:
Steve Gravrock
2022-10-11 19:23:48 -07:00
parent 394068f863
commit 1e7b68236b
4 changed files with 86 additions and 0 deletions

View File

@@ -46,6 +46,7 @@ getJasmineRequireObj().Env = function(j$) {
let reporter;
let topSuite;
let runner;
let parallelLodingState = null; // 'specs', 'helpers', or null for non-parallel
/**
* This represents the available options to configure Jasmine.
@@ -492,6 +493,10 @@ getJasmineRequireObj().Env = function(j$) {
reportSpecDone
});
this.setParallelLoadingState = function(state) {
parallelLodingState = state;
};
this.parallelReset = function() {
// TODO: ensure that autoCleanClosures was false
suiteBuilder.parallelReset();
@@ -659,6 +664,16 @@ getJasmineRequireObj().Env = function(j$) {
}
}
function ensureNonParallelOrInHelperOrInDescribe(method) {
if (parallelLodingState === 'specs' && !suiteBuilder.inDescribe()) {
throw new Error(
'In parallel mode, ' +
method +
' must be in a describe block or in a helper file'
);
}
}
this.describe = function(description, definitionFn) {
ensureIsNotNested('describe');
return suiteBuilder.describe(description, definitionFn).metadata;
@@ -780,6 +795,7 @@ getJasmineRequireObj().Env = function(j$) {
this.beforeEach = function(beforeEachFunction, timeout) {
ensureIsNotNested('beforeEach');
ensureNonParallelOrInHelperOrInDescribe('beforeEach');
suiteBuilder.beforeEach(beforeEachFunction, timeout);
};
@@ -790,6 +806,7 @@ getJasmineRequireObj().Env = function(j$) {
this.afterEach = function(afterEachFunction, timeout) {
ensureIsNotNested('afterEach');
ensureNonParallelOrInHelperOrInDescribe('afterEach');
suiteBuilder.afterEach(afterEachFunction, timeout);
};

View File

@@ -22,6 +22,10 @@ getJasmineRequireObj().SuiteBuilder = function(j$) {
this.focusedRunables = [];
}
inDescribe() {
return this.currentDeclarationSuite_ !== this.topSuite;
}
parallelReset() {
this.topSuite.removeChildren();
this.topSuite.reset();