Optionally enforce uniqueness of spec and suite names

This is off by default for backwards compatibility but can be enabled
by setting the forbidDuplicateNames env config property to true.

Fixes #1633.
This commit is contained in:
Steve Gravrock
2024-11-10 09:32:43 -08:00
parent 744e765d6f
commit d5e7bc9fd6
7 changed files with 233 additions and 2 deletions

View File

@@ -161,6 +161,8 @@ getJasmineRequireObj().SuiteBuilder = function(j$) {
j$.util.validateTimeout(timeout);
}
this.checkDuplicate_(description, 'spec');
const spec = this.specFactory_(description, fn, timeout, filename);
if (this.currentDeclarationSuite_.markedExcluding) {
spec.exclude();
@@ -170,7 +172,27 @@ getJasmineRequireObj().SuiteBuilder = function(j$) {
return spec;
}
checkDuplicate_(description, type) {
if (!this.env_.configuration().forbidDuplicateNames) {
return;
}
if (this.currentDeclarationSuite_.hasChildWithDescription(description)) {
const parentDesc =
this.currentDeclarationSuite_ === this.topSuite
? 'top suite'
: `"${this.currentDeclarationSuite_.getFullName()}"`;
throw new Error(
`Duplicate ${type} name "${description}" found in ${parentDesc}`
);
}
}
suiteFactory_(description, filename) {
if (this.topSuite) {
this.checkDuplicate_(description, 'suite');
}
const config = this.env_.configuration();
const parentSuite = this.currentDeclarationSuite_;
const reportedParentSuiteId =