Remove access to non-public properties of suites and specs returned by describe, it, etc.

[#179064612]
This commit is contained in:
Steve Gravrock
2021-07-30 17:36:50 -07:00
parent 6d002d22af
commit 13dfcacbb0
5 changed files with 272 additions and 169 deletions

View File

@@ -549,7 +549,7 @@ getJasmineRequireObj().Env = function(j$) {
* @return {Suite} the root suite
*/
this.topSuite = function() {
return topSuite.buildMetadata(null);
return topSuite.metadata;
};
/**
@@ -932,7 +932,7 @@ getJasmineRequireObj().Env = function(j$) {
if (suite.parentSuite && !suite.children.length) {
throw new Error('describe with no children (describe() or it())');
}
return suite;
return suite.metadata;
};
this.xdescribe = function(description, specDefinitions) {
@@ -941,7 +941,7 @@ getJasmineRequireObj().Env = function(j$) {
var suite = suiteFactory(description);
suite.pend();
addSpecsToSuite(suite, specDefinitions);
return suite;
return suite.metadata;
};
var focusedRunnables = [];
@@ -956,7 +956,7 @@ getJasmineRequireObj().Env = function(j$) {
unfocusAncestor();
addSpecsToSuite(suite, specDefinitions);
return suite;
return suite.metadata;
};
function addSpecsToSuite(suite, specDefinitions) {
@@ -1046,7 +1046,7 @@ getJasmineRequireObj().Env = function(j$) {
}
};
this.it = function(description, fn, timeout) {
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.
@@ -1062,6 +1062,11 @@ getJasmineRequireObj().Env = function(j$) {
return spec;
};
this.it = function(description, fn, timeout) {
const spec = this.it_(description, fn, timeout);
return spec.metadata;
};
this.xit = function(description, fn, timeout) {
ensureIsNotNested('xit');
// xit(), like it(), doesn't always have a fn argument, so only check the
@@ -1069,9 +1074,9 @@ getJasmineRequireObj().Env = function(j$) {
if (arguments.length > 1 && typeof fn !== 'undefined') {
ensureIsFunctionOrAsync(fn, 'xit');
}
var spec = this.it.apply(this, arguments);
var spec = this.it_.apply(this, arguments);
spec.pend('Temporarily disabled with xit');
return spec;
return spec.metadata;
};
this.fit = function(description, fn, timeout) {
@@ -1081,7 +1086,7 @@ getJasmineRequireObj().Env = function(j$) {
currentDeclarationSuite.addChild(spec);
focusedRunnables.push(spec.id);
unfocusAncestor();
return spec;
return spec.metadata;
};
/**

View File

@@ -3,12 +3,6 @@ getJasmineRequireObj().Spec = function(j$) {
this.expectationFactory = attrs.expectationFactory;
this.asyncExpectationFactory = attrs.asyncExpectationFactory;
this.resultCallback = attrs.resultCallback || function() {};
/**
* The unique ID of this spec.
* @name Spec#id
* @readonly
* @type {string}
*/
this.id = attrs.id;
this.description = attrs.description || '';
this.queueableFn = attrs.queueableFn;
@@ -238,30 +232,39 @@ getJasmineRequireObj().Spec = function(j$) {
* @interface Spec
* @see Configuration#specFilter
*/
Spec.prototype.buildMetadata = function() {
return {
/**
* The description passed to the {@link it} that created this spec.
* @name Spec#description
* @readonly
* @type {string}
*/
description: this.description,
Object.defineProperty(Spec.prototype, 'metadata', {
get: function() {
if (!this.metadata_) {
this.metadata_ = {
/**
* The unique ID of this spec.
* @name Spec#id
* @readonly
* @type {string}
*/
id: this.id,
/**
* The full description including all ancestors of this spec.
* @name Spec#getFullName
* @function
* @returns {string}
*/
getFullName: this.getFullName.bind(this)
};
};
/**
* The description passed to the {@link it} that created this spec.
* @name Spec#description
* @readonly
* @type {string}
*/
description: this.description,
/**
* The full description including all ancestors of this spec.
* @name Spec#getFullName
* @function
* @returns {string}
*/
getFullName: this.getFullName.bind(this)
};
}
return this.metadata_;
}
});
return Spec;
};
if (typeof window == void 0 && typeof exports == 'object') {
/* globals exports */
exports.Spec = jasmineRequire.Spec;
}

View File

@@ -1,12 +1,6 @@
getJasmineRequireObj().Suite = function(j$) {
function Suite(attrs) {
this.env = attrs.env;
/**
* The unique ID of this suite.
* @name Suite#id
* @readonly
* @type {string}
*/
this.id = attrs.id;
this.parentSuite = attrs.parentSuite;
this.description = attrs.description;
@@ -191,57 +185,71 @@ getJasmineRequireObj().Suite = function(j$) {
);
};
Suite.prototype.buildMetadata = function(parentMetadata) {
Object.defineProperty(Suite.prototype, 'metadata', {
get: function() {
if (!this.metadata_) {
this.metadata_ = new SuiteMetadata(this);
}
return this.metadata_;
}
});
/**
* @interface Suite
* @see Env#topSuite
*/
function SuiteMetadata(suite) {
this.suite_ = suite;
/**
* @interface Suite
* @see Env#topSuite
* The unique ID of this suite.
* @name Suite#id
* @readonly
* @type {string}
*/
var result = {
/**
* The parent of this suite, or null if this is the top suite.
* @name Suite#parentSuite
* @readonly
* @type {Suite}
*/
parentSuite: parentMetadata,
/**
* The description passed to the {@link describe} that created this suite.
* @name Suite#description
* @readonly
* @type {string}
*/
description: this.description,
/**
* The full description including all ancestors of this suite.
* @name Suite#getFullName
* @function
* @returns {string}
*/
getFullName: this.getFullName.bind(this)
};
this.id = suite.id;
/**
* The suite's children.
* @name Suite#children
* @type {Array.<(Spec|Suite)>}
* The parent of this suite, or null if this is the top suite.
* @name Suite#parentSuite
* @readonly
* @type {Suite}
*/
result.children = this.children.map(function(child) {
return child.buildMetadata(result);
});
this.parentSuite = suite.parentSuite ? suite.parentSuite.metadata : null;
return result;
/**
* The description passed to the {@link describe} that created this suite.
* @name Suite#description
* @readonly
* @type {string}
*/
this.description = suite.description;
}
/**
* The full description including all ancestors of this suite.
* @name Suite#getFullName
* @function
* @returns {string}
*/
SuiteMetadata.prototype.getFullName = function() {
return this.suite_.getFullName();
};
/**
* The suite's children.
* @name Suite#children
* @type {Array.<(Spec|Suite)>}
*/
Object.defineProperty(SuiteMetadata.prototype, 'children', {
get: function() {
return this.suite_.children.map(child => child.metadata);
}
});
function isFailure(args) {
return !args[0];
}
return Suite;
};
if (typeof window == void 0 && typeof exports == 'object') {
/* globals exports */
exports.Suite = jasmineRequire.Suite;
}