Merge branch 'bonkevin-spec-suite-properties-accessors'

* Merges #2072 from @bonkevin
* Adds Env#getSpecProperty
This commit is contained in:
Steve Gravrock
2025-08-30 07:58:36 -07:00
6 changed files with 139 additions and 2 deletions

View File

@@ -835,6 +835,11 @@ getJasmineRequireObj().Spec = function(j$) {
}
};
Spec.prototype.getSpecProperty = function(key) {
this.result.properties = this.result.properties || {};
return this.result.properties[key];
};
Spec.prototype.setSpecProperty = function(key, value) {
this.result.properties = this.result.properties || {};
this.result.properties[key] = value;
@@ -1906,6 +1911,26 @@ getJasmineRequireObj().Env = function(j$) {
return suiteBuilder.fit(description, fn, timeout, filename).metadata;
};
/**
* Get a user-defined property as part of the properties field of {@link SpecResult}
* @name Env#getSpecProperty
* @since 5.10.0
* @function
* @param {String} key The name of the property
* @returns {*} The value of the property
*/
this.getSpecProperty = function(key) {
if (
!runner.currentRunable() ||
runner.currentRunable() == runner.currentSuite()
) {
throw new Error(
"'getSpecProperty' was used when there was no current spec"
);
}
return runner.currentRunable().getSpecProperty(key);
};
/**
* Sets a user-defined property that will be provided to reporters as part of the properties field of {@link SpecResult}
* @name Env#setSpecProperty
@@ -8858,6 +8883,18 @@ getJasmineRequireObj().interface = function(jasmine, env) {
return env.afterAll.apply(env, arguments);
},
/**
* Get a user-defined property as part of the properties field of {@link SpecResult}
* @name getSpecProperty
* @since 5.10.0
* @function
* @param {String} key The name of the property
* @returns {*} The value of the property
*/
getSpecProperty: function(key) {
return env.getSpecProperty(key);
},
/**
* Sets a user-defined property that will be provided to reporters as part of the properties field of {@link SpecResult}
* @name setSpecProperty

View File

@@ -78,6 +78,17 @@ describe('Spec', function() {
});
});
describe('#getSpecProperty', function() {
it('get the property value', function() {
const spec = new jasmineUnderTest.Spec({
queueableFn: { fn: () => {} }
});
spec.setSpecProperty('a', 4);
expect(spec.getSpecProperty('a')).toBe(4);
});
});
describe('#setSpecProperty', function() {
it('adds the property to the result', function() {
const spec = new jasmineUnderTest.Spec({
@@ -88,6 +99,21 @@ describe('Spec', function() {
expect(spec.result.properties).toEqual({ a: 4 });
});
it('replace the property result when it was previously set', function() {
const spec = new jasmineUnderTest.Spec({
queueableFn: { fn: () => {} }
});
spec.setSpecProperty('a', 'original-value');
spec.setSpecProperty('b', 'original-value');
spec.setSpecProperty('a', 'new-value');
expect(spec.result.properties).toEqual({
a: 'new-value',
b: 'original-value'
});
});
});
it('#status returns passing by default', function() {

View File

@@ -2262,17 +2262,54 @@ describe('Env integration', function() {
);
});
it('throws an exception if you try to getSpecProperty outside of a spec', async function() {
const env = new jasmineUnderTest.Env();
let exception;
env.describe('a suite', function() {
env.it('a spec');
try {
env.getSpecProperty('a prop');
} catch (e) {
exception = e;
}
env.it('has a test', function() {});
});
await env.execute();
expect(exception.message).toBe(
"'getSpecProperty' was used when there was no current spec"
);
});
it('reports test properties on specs', async function() {
const env = new jasmineUnderTest.Env(),
reporter = jasmine.createSpyObj('reporter', ['suiteDone', 'specDone']);
reporter.specDone.and.callFake(function(e) {
expect(e.properties).toEqual({ a: 'Bee' });
expect(e.properties).toEqual({
fromBeforeEach: 'Pie',
fromSpecInnerCallback: 'Bee',
willChangeInAfterEach: 2,
fromAfterEach: 'Cheese'
});
});
env.addReporter(reporter);
env.beforeEach(function() {
env.setSpecProperty('fromBeforeEach', 'Pie');
});
env.afterEach(function() {
env.setSpecProperty(
'willChangeInAfterEach',
env.getSpecProperty('willChangeInAfterEach') + 1
);
env.setSpecProperty('fromAfterEach', 'Cheese');
});
env.it('calls setSpecProperty', function() {
env.setSpecProperty('a', 'Bee');
env.setSpecProperty('fromSpecInnerCallback', 'Bee');
env.setSpecProperty('willChangeInAfterEach', 1);
});
await env.execute();

View File

@@ -779,6 +779,26 @@ getJasmineRequireObj().Env = function(j$) {
return suiteBuilder.fit(description, fn, timeout, filename).metadata;
};
/**
* Get a user-defined property as part of the properties field of {@link SpecResult}
* @name Env#getSpecProperty
* @since 5.10.0
* @function
* @param {String} key The name of the property
* @returns {*} The value of the property
*/
this.getSpecProperty = function(key) {
if (
!runner.currentRunable() ||
runner.currentRunable() == runner.currentSuite()
) {
throw new Error(
"'getSpecProperty' was used when there was no current spec"
);
}
return runner.currentRunable().getSpecProperty(key);
};
/**
* Sets a user-defined property that will be provided to reporters as part of the properties field of {@link SpecResult}
* @name Env#setSpecProperty

View File

@@ -63,6 +63,11 @@ getJasmineRequireObj().Spec = function(j$) {
}
};
Spec.prototype.getSpecProperty = function(key) {
this.result.properties = this.result.properties || {};
return this.result.properties[key];
};
Spec.prototype.setSpecProperty = function(key, value) {
this.result.properties = this.result.properties || {};
this.result.properties[key] = value;

View File

@@ -168,6 +168,18 @@ getJasmineRequireObj().interface = function(jasmine, env) {
return env.afterAll.apply(env, arguments);
},
/**
* Get a user-defined property as part of the properties field of {@link SpecResult}
* @name getSpecProperty
* @since 5.10.0
* @function
* @param {String} key The name of the property
* @returns {*} The value of the property
*/
getSpecProperty: function(key) {
return env.getSpecProperty(key);
},
/**
* Sets a user-defined property that will be provided to reporters as part of the properties field of {@link SpecResult}
* @name setSpecProperty